分組(recyclerview巢狀)
阿新 • • 發佈:2018-11-27
效果
分包方式
重要程式碼
recyclerview巢狀recyclerview
介面卡(大的介面卡巢狀子介面卡)
package com.jia.dividegroups.adapt;
import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.jia.dividegroups.R; import com.jia.dividegroups.bean.RightBean; import java.util.List; public class RightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; List<RightBean.DataBean> list; public RightAdapter(Context context, List<RightBean.DataBean> list) { this.context = context; this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(context).inflate(R.layout.right_item, viewGroup, false); RightViewHolder rightViewHolder = new RightViewHolder(view); return rightViewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { RightViewHolder rightViewHolder = (RightViewHolder) viewHolder; rightViewHolder.right_name.setText(list.get(i).getName()); List<RightBean.DataBean.ListBean> listBeans = this.list.get(i).getList(); RightChildAdapter rightChildAdapter = new RightChildAdapter(context, listBeans); rightViewHolder.child_recycler.setAdapter(rightChildAdapter); rightViewHolder.child_recycler.setLayoutManager(new GridLayoutManager(context,3)); } @Override public int getItemCount() { return list.size(); } class RightViewHolder extends RecyclerView.ViewHolder { private final TextView right_name; private final RecyclerView child_recycler; public RightViewHolder(@NonNull View itemView) { super(itemView); right_name = itemView.findViewById(R.id.right_name); child_recycler = itemView.findViewById(R.id.child_recycler); } }
}
小介面卡
package com.jia.dividegroups.adapt;
import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.jia.dividegroups.R; import com.jia.dividegroups.bean.RightBean; import com.squareup.picasso.Picasso; import java.util.List; public class RightChildAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; List<RightBean.DataBean.ListBean> list; public RightChildAdapter(Context context, List<RightBean.DataBean.ListBean> list) { this.context = context; this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(context).inflate(R.layout.rightinchild_item, viewGroup, false); ChildViewHolder childViewHolder = new ChildViewHolder(view); return childViewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { ChildViewHolder childViewHolder = (ChildViewHolder) viewHolder; childViewHolder.child_name.setText(list.get(i).getName()); Picasso.with(context).load(list.get(i).getIcon()).into(childViewHolder.image_icon); } @Override public int getItemCount() { return list.size(); } class ChildViewHolder extends RecyclerView.ViewHolder { private final ImageView image_icon; private final TextView child_name; public ChildViewHolder(@NonNull View itemView) { super(itemView); image_icon = itemView.findViewById(R.id.image_icon); child_name = itemView.findViewById(R.id.child_name); } }
}
其他還和以前一樣(M層獲取資料)
package com.jia.dividegroups.model;
import com.google.gson.Gson; import com.jia.dividegroups.bean.RightBean; import com.jia.dividegroups.callback.RightCallBack; import com.jia.dividegroups.utils.OkHttpUtils; import java.io.IOException; import java.util.List; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; public class RightModel { public void right(int cid, final RightCallBack rightCallBack){ String url = "http://www.zhaoapi.cn/product/getProductCatagory?cid="+cid; OkHttpUtils.enqueueGet(url, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String s = response.body().string(); Gson gson = new Gson(); RightBean rightBean = gson.fromJson(s, RightBean.class); List<RightBean.DataBean> data = rightBean.getData(); if(rightBean.getCode().equals("0")){ rightCallBack.onRightCallBackSuccess(data); }else{ rightCallBack.onRightCallBackFailure("展示失敗"); } } }); }
}
OkHttp請求網路
package com.jia.dividegroups.utils;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class OkHttpUtils {
private static OkHttpUtils okHttpUtils = new OkHttpUtils();
private static OkHttpClient okHttpClient;
private OkHttpUtils(){}
public static OkHttpUtils init(){
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(3000,TimeUnit.MILLISECONDS);
builder.writeTimeout(3000,TimeUnit.MILLISECONDS);
builder.readTimeout(3000,TimeUnit.MILLISECONDS);
okHttpClient = builder.build();
return okHttpUtils;
}
private static Request createRequest(String url,String method){
Request.Builder builder = new Request.Builder().url(url);
Request request = builder.build();
return request;
}
public static void enqueueGet(String url, Callback callback){
Request request = createRequest(url,"GET");
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
}