RecyclerView嵌套+搜索框

第一个适配器

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dome8.R;
import com.example.dome8.bean.ShidingBean;

import java.util.ArrayList;
import java.util.List;
public class OneAdapter extends RecyclerView.Adapter {
private Context context ;
private List resultBeans = new ArrayList<>();
private int mColor=0;
//构造函数
public OneAdapter(Context context, List resultBeans) {
this.context = context;
this.resultBeans = resultBeans;
}

@NonNull
@Override
public OneHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = View.inflate(context, R.layout.one_item, null);
    OneHolder oneHolder = new OneHolder(view);
    return oneHolder;
}

@Override
public void onBindViewHolder(@NonNull OneHolder holder, int position) {
    //获取名字
    holder.name.setText(resultBeans.get(position).getName()+"");
    //判断颜色
    if (mColor==position){
        holder.name.setTextColor(Color.RED);
    }else {
        holder.name.setTextColor(Color.BLACK);
    }
    //设置单击事件
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (click != null) {
                click.setIndex(position);
            }
        }
    });

}

@Override
public int getItemCount() {
    return resultBeans.size();
}

public class OneHolder extends RecyclerView.ViewHolder {

    private final TextView name;

    public OneHolder(@NonNull View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.one_name);
    }
}

//实现点击接口
public interface ItemClick{
    void setIndex(int position);
}
private ItemClick click;
//点击方法
public void setItemClick(ItemClick click){
    this.click=click;
}
//点击切换颜色
public void setColor(int position){
    this.mColor=position;
    //每次点击切换的时候刷新适配器
    notifyDataSetChanged();
}

}

第二个适配器

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dome8.R;
import com.example.dome8.bean.ShidingBean;

import java.util.ArrayList;
import java.util.List;
public class OneAdapter extends RecyclerView.Adapter {
private Context context ;
private List resultBeans = new ArrayList<>();
private int mColor=0;
//构造函数
public OneAdapter(Context context, List resultBeans) {
this.context = context;
this.resultBeans = resultBeans;
}

@NonNull
@Override
public OneHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = View.inflate(context, R.layout.one_item, null);
    OneHolder oneHolder = new OneHolder(view);
    return oneHolder;
}

@Override
public void onBindViewHolder(@NonNull OneHolder holder, int position) {
    //获取名字
    holder.name.setText(resultBeans.get(position).getName()+"");
    //判断颜色
    if (mColor==position){
        holder.name.setTextColor(Color.RED);
    }else {
        holder.name.setTextColor(Color.BLACK);
    }
    //设置单击事件
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (click != null) {
                click.setIndex(position);
            }
        }
    });

}

@Override
public int getItemCount() {
    return resultBeans.size();
}

public class OneHolder extends RecyclerView.ViewHolder {

    private final TextView name;

    public OneHolder(@NonNull View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.one_name);
    }
}

//实现点击接口
public interface ItemClick{
    void setIndex(int position);
}
private ItemClick click;
//点击方法
public void setItemClick(ItemClick click){
    this.click=click;
}
//点击切换颜色
public void setColor(int position){
    this.mColor=position;
    //每次点击切换的时候刷新适配器
    notifyDataSetChanged();
}

}

Activity中

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dome8.adapter.OneAdapter;
import com.example.dome8.adapter.TowAdapter;
import com.example.dome8.base.BasePresenter;
import com.example.dome8.base.FragmentActivity;
import com.example.dome8.bean.ShidingBean;
import com.example.dome8.mvp.IPresneterInpl;
import com.example.dome8.netutlis.NetUtlis;
import com.example.dome8.url.BaseUrl;

import java.util.ArrayList;
import java.util.List;

/**

  • A simple {@link Fragment} subclass.
    */
    public class ShouFragment extends FragmentActivity {

    private RecyclerView tow_recy;
    private RecyclerView one_recy;
    //商品分类
    private List resultBeans = new ArrayList<>();
    //具体商品
    private List beanList = new ArrayList<>();
    private OneAdapter oneAdapter;
    private TowAdapter towAdapter;

    @Override
    protected void initViews(View inflate) {
    one_recy = inflate.findViewById(R.id.one_recy);
    tow_recy = inflate.findViewById(R.id.tow_recy);
    one_recy.setLayoutManager(new LinearLayoutManager(getActivity()));
    tow_recy.setLayoutManager(new GridLayoutManager(getActivity(),3));
    }

    @Override
    protected int inityout() {
    return R.layout.fragment_shou;
    }

    @Override
    protected void initData() {
    oneAdapter = new OneAdapter(getActivity(),resultBeans);
    one_recy.setAdapter(oneAdapter);
    towAdapter = new TowAdapter(getActivity(),beanList);
    tow_recy.setAdapter(towAdapter);
    //设置点击事件
    oneAdapter.setItemClick(new OneAdapter.ItemClick() {
    @Override
    public void setIndex(int position) {
    oneAdapter.setColor(position);
    beanList.clear();
    //添加旧数据
    beanList.addAll(resultBeans.get(position).getSecondCategoryVo());
    //刷新适配器
    towAdapter.notifyDataSetChanged();
    }
    });
    NetUtlis.getInstance().GetPramsInfo(BaseUrl.SHINGURL, ShidingBean.class, new NetUtlis.NetCallback() {
    @Override
    public void onSuccess(Object o) {
    if (o instanceof ShidingBean){
    //添加数据
    resultBeans.addAll(((ShidingBean) o).getResult());
    oneAdapter.notifyDataSetChanged();
    beanList.addAll(resultBeans.get(0).getSecondCategoryVo());
    towAdapter.notifyDataSetChanged();
    }
    }

         @Override
         public void onError(String error) {
    
         }
     });
    

    }

    @Override
    protected BasePresenter initPresenter() {
    return new IPresneterInpl();
    }

    @Override
    public void onSuccess(Object o) {

    }

    @Override
    public void onError(String error) {

    }
    }

你可能感兴趣的:(RecyclerView嵌套+搜索框)