欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中RecyclerView實現(xiàn)商品分類功能

 更新時間:2022年02月10日 16:00:08   作者:WWWW.COM  
這篇文章主要為大家詳細(xì)介紹了Android中RecyclerView實現(xiàn)商品分類功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android中RecyclerView實現(xiàn)商品分類功能的具體代碼,供大家參考,具體內(nèi)容如下

三個個RecyclerView實現(xiàn)

//左邊的布局

?<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="50dp"
? ? android:orientation="vertical">

? ? <TextView
? ? ? ? android:id="@+id/tv_name"
? ? ? ? android:textSize="18sp"
? ? ? ? android:text="阿薩德發(fā)的"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent" />
</LinearLayout>

//右邊的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent">

? ? <TextView
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/name"/>

? ? <android.support.v7.widget.RecyclerView
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:id="@+id/right_recy"
? ? ? ? android:layout_below="@+id/name"/>
</RelativeLayout>

//子布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:orientation="vertical">

? ? <ImageView
? ? ? ? android:id="@+id/image2"
? ? ? ? android:layout_width="90dp"
? ? ? ? android:layout_height="90dp" />

? ? <TextView
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/title1" />
</LinearLayout>

//定義一個接口

public interface CallBack {
void onSuccess(List<LeftBean.DataBean> list);
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);
? ? ? ? ? ? ? ? List<LeftBean.DataBean> data = 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(List<LeftBean.DataBean> list) {
? ? ? ? ? ? ? ? leftView.onSuccess(list);
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailer(String error) {
? ? ? ? ? ? ? ? leftView.Failer(error);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

//View層

public interface LeftView {
? ?void onSuccess(List<LeftBean.DataBean> list);
? ?void Failer(String error);
}

//左邊的適配器

public class LeftRecycAdapter extends RecyclerView.Adapter<LeftRecycAdapter.LeftViewHoler>{

? ? private Context mContext;
? ? private List<LeftBean.DataBean> list;

? ? public LeftRecycAdapter(Context mContext, List<LeftBean.DataBean> 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(List<RightBean.DataBean> list);
? ? 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);
? ? ? ? ? ? ? ? ? ? ? ? List<RightBean.DataBean> data = 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(List<RightBean.DataBean> list) {
? ? ? ? ? ? ? ? rightView.onSuccess2(list);
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailer2(String error) {
? ? ? ? ? ? ? ? rightView.onFailer2(error);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

//右邊的View層

public interface RightView {
? ? void onSuccess2(List<RightBean.DataBean> list);
? ? void onFailer2(String error);
}

//右邊的適配器

public class RightRecycAdapter extends RecyclerView.Adapter<RightRecycAdapter.ViewHolder> {

? ? private Context mContext;
? ? private List<RightBean.DataBean> list;

? ? public RightRecycAdapter(Context mContext, List<RightBean.DataBean> 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<RightBean.DataBean.ListBean> 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<ChildAdapter.ViewHolder> {

? ? private Context context;
? ? private List<RightBean.DataBean.ListBean> list;

? ? public ChildAdapter(Context context, List<RightBean.DataBean.ListBean> 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 List<LeftBean.DataBean> list = (List<LeftBean.DataBean>) 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<RightBean.DataBean> list1 = (List<RightBean.DataBean>) 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<LeftBean.DataBean> list) {
? ? ? ? Message message = Message.obtain();
? ? ? ? message.obj=list;
? ? ? ? handler.sendMessage(message);
? ? }

? ? @Override
? ? public void Failer(String error) {

? ? }

? ? @Override
? ? public void onSuccess2(final List<RightBean.DataBean> 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) {

? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android服務(wù)Service教程

    Android服務(wù)Service教程

    Android的服務(wù)是開發(fā)Android應(yīng)用程序的重要組成部分。不同于活動Activity,服務(wù)是在后臺運行,服務(wù)沒有接口,生命周期也與活動Activity非常不同。通過使用服務(wù)我們可以實現(xiàn)一些后臺操作,比如想從遠(yuǎn)程服務(wù)器加載一個網(wǎng)頁等,下面來看看詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-11-11
  • Android7.0開發(fā)實現(xiàn)Launcher3去掉應(yīng)用抽屜的方法詳解

    Android7.0開發(fā)實現(xiàn)Launcher3去掉應(yīng)用抽屜的方法詳解

    這篇文章主要介紹了Android7.0開發(fā)實現(xiàn)Launcher3去掉應(yīng)用抽屜的方法,結(jié)合實例形式分析了Android7.0 Launcher3調(diào)整界面布局的相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2017-11-11
  • Android高仿京東垂直循環(huán)滾動新聞欄

    Android高仿京東垂直循環(huán)滾動新聞欄

    通過自定義的LinearLayout,并且textView能夠循環(huán)垂直滾動,而且條目可以點擊,顯示區(qū)域最多顯示2個條目,并且還有交替的屬性垂直移動的動畫效果,通過線程來控制滾動的實現(xiàn)
    2016-03-03
  • photoView實現(xiàn)圖片多點觸控效果

    photoView實現(xiàn)圖片多點觸控效果

    這篇文章主要為大家詳細(xì)介紹了photoView實現(xiàn)圖片多點觸控效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android設(shè)計模式之單例模式實例

    Android設(shè)計模式之單例模式實例

    這篇文章主要介紹了Android設(shè)計模式之單例模式實例,單例模式是運用最廣泛的設(shè)計模式之一,在應(yīng)用這個模式時,單例模式的類必須保證只有一個實例存在
    2023-04-04
  • android之datepicker控件的用法

    android之datepicker控件的用法

    下面小編就為大家?guī)硪黄猘ndroid之datepicker控件的用法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Android使用RollViewPager實現(xiàn)輪播圖

    Android使用RollViewPager實現(xiàn)輪播圖

    這篇文章主要為大家詳細(xì)介紹了Android使用RollViewPager實現(xiàn)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • android 一些工具類匯總

    android 一些工具類匯總

    本文給大家匯總介紹了一些常用的Android工具類,非常的簡單實用,有需要的小伙伴可以參考下
    2016-08-08
  • RollViewPager無限輪播使用方法詳解

    RollViewPager無限輪播使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了RollViewPager無限輪播的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android 多進(jìn)程資料總結(jié)

    Android 多進(jìn)程資料總結(jié)

    這篇文章主要介紹了Android 多進(jìn)程資料總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2016-09-09

最新評論