本文实例为大家分享了Android中RecyclerView实现商品分类功能的具体代码,供大家参考,具体内容如下
三个个RecyclerView实现
//左边的布局
//右边的布局
//子布局
//定义一个接口
public interface CallBack { void onSuccess(Listlist); void onFailer(String error); }
//左边的Model层
public class LeftModel { private String path="http://www.zhaoapi.cn/product/getCatagory"; public void getData(final CallBack callBack){ OkHttp okHttp=new OkHttp(); okHttp.get(path).getDataLiserner(new OkHttp.GetData() { @Override public void Data(String s) { Gson gson=new Gson(); LeftBean json = gson.fromJson(s, LeftBean.class); Listdata = json.getData(); if (data!=null){ callBack.onSuccess(data); }else { callBack.onFailer("失败"); } } }); } }
//左边的Presenter层
public class LeftPresenter { private LeftView leftView; private final LeftModel leftModel; public LeftPresenter(LeftView leftView) { this.leftView = leftView; leftModel = new LeftModel(); } public void showLeft(){ leftModel.getData(new CallBack() { @Override public void onSuccess(Listlist) { leftView.onSuccess(list); } @Override public void onFailer(String error) { leftView.Failer(error); } }); } }
//View层
public interface LeftView { void onSuccess(Listlist); void Failer(String error); }
//左边的适配器
public class LeftRecycAdapter extends RecyclerView.Adapter{ private Context mContext; private List list; public LeftRecycAdapter(Context mContext, List list) { this.mContext = mContext; this.list = list; } @NonNull @Override public LeftViewHoler onCreateViewHolder(@NonNull ViewGroup viewGroup, int ViewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.left_item, viewGroup,false); LeftViewHoler leftViewHoler=new LeftViewHoler(view); return leftViewHoler; } @Override public void onBindViewHolder(@NonNull LeftViewHoler leftViewHoler, int position) { leftViewHoler.textView.setText(list.get(position).getName()); } @Override public int getItemCount() { return list.size(); } public class LeftViewHoler extends RecyclerView.ViewHolder { private TextView textView; public LeftViewHoler(@NonNull View itemView) { super(itemView); textView=itemView.findViewById(R.id.tv_name); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onClickListener.onclick(v,getAdapterPosition()); } }); } } public interface OnClickListener{ void onclick(View view,int position); } OnClickListener onClickListener; public void setOnclickListener(OnClickListener onclickListener){ this.onClickListener=onclickListener; } }
开始右边的了
//右边的接口
public interface CallBackRight { void onSuccess2(Listlist); void onFailer2(String error); }
//右边的Model层
public class RightModel { // private String path1="http://www.zhaoapi.cn/product/getProductCatagory?cid=3&tdsourcetag=s_pcqq_aiomsg"; public void showright(final String cid2, final CallBackRight callBackRight){ OkHttp okHttp=new OkHttp(); okHttp.get(cid2).getDataLiserner(new OkHttp.GetData() { @Override public void Data(String s) { Gson gson=new Gson(); RightBean json = gson.fromJson(s, RightBean.class); Listdata = json.getData(); if (data!=null){ callBackRight.onSuccess2(data); }else { callBackRight.onFailer2("错误"); } } }); } }
//右边的Presenter层
public class RightPresenter { private final RightModel rightModel; private RightView rightView; public RightPresenter(RightView rightView) { this.rightView = rightView; rightModel = new RightModel(); } public void showright(String id){ rightModel.showright(id, new CallBackRight() { @Override public void onSuccess2(Listlist) { rightView.onSuccess2(list); } @Override public void onFailer2(String error) { rightView.onFailer2(error); } }); } }
//右边的View层
public interface RightView { void onSuccess2(Listlist); void onFailer2(String error); }
//右边的适配器
public class RightRecycAdapter extends RecyclerView.Adapter{ private Context mContext; private List list; public RightRecycAdapter(Context mContext, List list) { this.mContext = mContext; this.list = list; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int ViewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.right_item, viewGroup, false); ViewHolder viewHolder=new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) { viewHolder.textView.setText(list.get(position).getName()); List list = this.list.get(position).getList(); GridLayoutManager gridLayoutManager=new GridLayoutManager(mContext,3); viewHolder.recyclerView.setLayoutManager(gridLayoutManager); ChildAdapter childAdapter=new ChildAdapter(mContext,list); viewHolder.recyclerView.setAdapter(childAdapter); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private TextView textView; private RecyclerView recyclerView; public ViewHolder(@NonNull View itemView) { super(itemView); textView=itemView.findViewById(R.id.name); recyclerView=itemView.findViewById(R.id.right_recy); } } }
//子类适配器
public class ChildAdapter extends RecyclerView.Adapter{ private Context context; private List list; public ChildAdapter(Context context, List list) { this.context = context; this.list = list; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int ViewType) { View view = LayoutInflater.from(context).inflate(R.layout.child, viewGroup, false); ViewHolder viewHolder=new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) { viewHolder.textView.setText(list.get(position).getName()); Picasso.with(context).load(list.get(position).getIcon()).into(viewHolder.imageView); } @Override public int getItemCount() { return list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private ImageView imageView; private TextView textView; public ViewHolder(@NonNull View itemView) { super(itemView); imageView=itemView.findViewById(R.id.image2); textView=itemView.findViewById(R.id.title1); } } }
//开始使用
public class Fragment1 extends Fragment implements LeftView,RightView { private View view; private RecyclerView left; private RecyclerView right; private RightPresenter rightPresenter; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); final Listlist = (List ) msg.obj; LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false); left.setLayoutManager(linearLayoutManager); LeftRecycAdapter leftRecycAdapter=new LeftRecycAdapter(getActivity(),list); left.setAdapter(leftRecycAdapter); leftRecycAdapter.setOnclickListener(new LeftRecycAdapter.OnClickListener() { @Override public void onclick(View view, int position) { int cid = list.get(position).getCid(); rightPresenter.showright("http://www.zhaoapi.cn/product/getProductCatagory?cid="+cid); } }); //List list1 = (List ) msg.obj; } }; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_fragment1, container, false); initView(); return view; } private void initView() { LeftPresenter leftPresenter=new LeftPresenter(this); leftPresenter.showLeft(); left = (RecyclerView) view.findViewById(R.id.left_recy); right = (RecyclerView) view.findViewById(R.id.right_recy); rightPresenter = new RightPresenter(this); } @Override public void onSuccess(List list) { Message message = Message.obtain(); message.obj=list; handler.sendMessage(message); } @Override public void Failer(String error) { } @Override public void onSuccess2(final List list) { /*Message message = Message.obtain(); message.obj=list; handler.sendMessage(message);*/ handler.post(new Runnable() { @Override public void run() { LinearLayoutManager linear= new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false); right.setLayoutManager(linear); RightRecycAdapter rightRecycAdapter=new RightRecycAdapter(getActivity(),list); right.setAdapter(rightRecycAdapter); } }); } @Override public void onFailer2(String error) { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。