左边的listView的适配器
public class MylistAdapter extends BaseAdapter {
private Context context;
private List<FenleiBean.DataBean> list;
private int position;
public MylistAdapter(Context context, List<FenleiBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder vie;
if (view == null){
vie = new ViewHolder();
view = View.inflate(context, R.layout.fenlei_left_item,null);
vie.fenlei_left_tev = view.findViewById(R.id.fenlei_left_tev);
view.setTag(vie);
}else {
vie = (ViewHolder) view.getTag();
}
vie.fenlei_left_tev.setText(list.get(i).getName());
if (position == i){
view.setBackgroundColor(Color.TRANSPARENT);
vie.fenlei_left_tev.setTextColor(Color.RED);
}else {
view.setBackgroundColor(Color.WHITE);
vie.fenlei_left_tev.setTextColor(Color.BLACK);
}
return view;
}
public void setCurPosition(int i) {
this.position = i;
}
static class ViewHolder{
TextView fenlei_left_tev;
}
}
右边的上边的适配器
public class MyRightTevAdapter extends RecyclerView.Adapter<MyfenleitevHolder> {
private Context context;
private List<SubClassBean.DataBean> subda;
public MyRightTevAdapter(Context context, List<SubClassBean.DataBean> subda) {
this.context = context;
this.subda = subda;
}
@Override
public MyfenleitevHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.fenlei_right_item, parent, false);
MyfenleitevHolder myfenleitevHolder = new MyfenleitevHolder(view);
return myfenleitevHolder;
}
@Override
public void onBindViewHolder(MyfenleitevHolder holder, int position) {
holder.fenlei_right_tev.setText(subda.get(position).getName());
holder.fenlei_rec_img_tev.setLayoutManager(new GridLayoutManager(context,3,GridLayoutManager.VERTICAL,false));
final List<SubClassBean.DataBean.ListBean> sublist = subda.get(position).getList();
MyRightImgTevAdapter myRightImgTevAdapter = new MyRightImgTevAdapter(context,sublist);
holder.fenlei_rec_img_tev.setAdapter(myRightImgTevAdapter);
myRightImgTevAdapter.setOnItemListener(new Onitemface() {
@Override
public void OnClick(int position) {
Intent it = new Intent(context, ProductActivity.class);
String name = sublist.get(position).getName();
it.putExtra("riname",name);
context.startActivity(it);
}
@Override
public void OnLongClick(int position) {
}
});
}
@Override
public int getItemCount() {
return subda.size();
}
}
右边下边的的适配器
public class MyRightImgTevAdapter extends RecyclerView.Adapter<MyRightimgtevHolder> {
private Context context;
private List<SubClassBean.DataBean.ListBean> sublist;
private Onitemface onitemface;
public MyRightImgTevAdapter(Context context, List<SubClassBean.DataBean.ListBean> sublist) {
this.context = context;
this.sublist = sublist;
}
@Override
public MyRightimgtevHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.fenlei_right_img_tev_item,parent,false);
MyRightimgtevHolder myRightimgtevHolder = new MyRightimgtevHolder(view);
return myRightimgtevHolder;
}
@Override
public void onBindViewHolder(MyRightimgtevHolder holder, final int position) {
holder.right_tev.setText(sublist.get(position).getName());
Glide.with(context).load(sublist.get(position).getIcon()).into(holder.right_img);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onitemface.OnClick(position);
}
});
}
@Override
public int getItemCount() {
return sublist.size();
}
public void setOnItemListener(Onitemface onitemface) {
this.onitemface = onitemface;
}
}
主逻辑的传值
final MylistAdapter mylistAdapter = new MylistAdapter(getActivity(),fenleiBean.getData());
fenlei_left_list.setAdapter(mylistAdapter);
int cid = fenleiBean.getData().get(0).getCid();
scid = String.valueOf(cid);
subgetdata(scid);
fenlei_left_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mylistAdapter.setCurPosition(i);
mylistAdapter.notifyDataSetChanged();
fenlei_left_list.smoothScrollToPositionFromTop(i,(adapterView.getHeight() - view.getHeight()) / 2);
int cid = fenleiBean.getData().get(i).getCid();
scid = String.valueOf(cid);
subgetdata(scid);
}
});