RecyclerView基本使用和巢狀使用方法
阿新 • • 發佈:2019-01-03
1.背景介紹
最近在做一個專案,需要展示的效果如下:
按照型別去分組展示,但是針對特定的型別,又有不同的來源,單擊來源進行載入相應的資料。
總共有三種類型,分三種標籤,標題one,水平滾動recyclerview和標題three,在第二標籤下需要分不同資料來源,使用者單擊不同的來源進行載入。
2.準備工作
在build.gradle中新增下面的依賴:
implementation 'com.android.support:recyclerview-v7:28.0.0'
3.技術簡介
整體採用recyclerview實現分組的展示,設定兩種型別,一種標題型別,一種是item展示型別,為了能夠更好的展示,我新增了一個型別,這個型別為了展示資料來源的。
在MultiItemViewHolder新增一個recyclerview 檢視,展示資料來源的資料,並監聽click事件,來實現不同資料來源切換和重新整理。
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<CommonData> commonDataList;
private Context context;
public MyAdapter(Context context, List<CommonData> commonDataList) {
this.commonDataList = commonDataList;
this .context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
switch (viewType) {
case CommonData.TYPE_ONE:
case CommonData.TYPE_THR:
return new CommonViewHolder (LayoutInflater.from(context).inflate(R.layout.title_layout_main, viewGroup, false));
case CommonData.TYPE_CONTENT:
return new CommonViewHolder(LayoutInflater.from(context).inflate(R.layout.content_layout_main, viewGroup, false));
case CommonData.TYPE_TWO:
return new MultiItemViewHolder(LayoutInflater.from(context).inflate(R.layout.multi_item_layout, viewGroup, false));
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
int type = getItemViewType(position);
switch (type) {
case CommonData.TYPE_ONE:
case CommonData.TYPE_THR:
case CommonData.TYPE_CONTENT://內容和標題展示,只是內容和標題採用的是一個佈局。
((CommonViewHolder) viewHolder).bindData(commonDataList.get(position));
break;
case CommonData.TYPE_TWO://巢狀recyclerview的地方
((MultiItemViewHolder) viewHolder).bindData(commonDataList.get(position));
break;
}
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
GridLayoutManager linearLayoutManager = new GridLayoutManager(context, 4);
linearLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int i) {
switch (getItemViewType(i)) {
case CommonData.TYPE_ONE:
case CommonData.TYPE_TWO:
case CommonData.TYPE_THR:
return 4;
case CommonData.TYPE_CONTENT:
return 1;
}
return 1;
}
});
recyclerView.setLayoutManager(linearLayoutManager);
}
/**
* 重新整理資料
*
* @param list
*/
public void refresh(List<CommonData> list) {
commonDataList.clear();
commonDataList.addAll(list);
}
@Override
public int getItemCount() {
return commonDataList.size();
}
@Override
public int getItemViewType(int position) {
return commonDataList.get(position).getType();
}
}
MultiItemViewHolder.class
public class MultiItemViewHolder extends BaseViewHolder<CommonData> {
private RecyclerView recyclerView;
private TextView textView;
public MultiItemViewHolder(@NonNull View itemView) {
super(itemView);
recyclerView = itemView.findViewById(R.id.multi_item_recylerview);
textView = itemView.findViewById(R.id.title);
}
@Override
public void bindData(CommonData commonData) {
textView.setText((String) commonData.getData());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
SelfAdapter selfAdapter = new SelfAdapter(getData(), itemView.getContext());
recyclerView.setAdapter(selfAdapter);
}
/**
* 資料來源,為了方便,在這裡寫的測試資料
*
* @return
*/
private List<CommonData> getData() {
List<CommonData> temp = new ArrayList<>();
for (int index = 0; index < 10; index++) {
temp.add(new CommonData("資料來源" + index, CommonData.TYPE_TWO));
}
return temp;
}
/**
* 資料來源的adapter
*/
class SelfAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<CommonData> list;
Context context;
public SelfAdapter(List<CommonData> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new SelfViewHolder(LayoutInflater.from(context).inflate(R.layout.new_title_layout_main, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
((SelfViewHolder) viewHolder).bindData(list.get(i));
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(true);
}
}
class SelfViewHolder extends BaseViewHolder<CommonData> {
TextView textView;
public SelfViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_title);
}
@Override
public void bindData(final CommonData commonData) {
textView.setText((String) commonData.getData());
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemView.setEnabled(false);
if (v.getContext() instanceof MainActivity) {//更新資料
Message msg = Message.obtain();
msg.what = 1;
msg.obj = getAdapterPosition();
((MainActivity) v.getContext()).handler.sendMessage(msg);
}
}
});
}
}
}