|
| 1 | +package com.hugh.basis.scrolltest.scroll3; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import android.util.Log; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | + |
| 10 | + |
| 11 | +import com.hugh.basis.R; |
| 12 | +import com.hugh.basis.scrolltest.test2.ScrollTest2Activity; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import androidx.recyclerview.widget.LinearLayoutManager; |
| 18 | +import androidx.recyclerview.widget.RecyclerView; |
| 19 | + |
| 20 | + |
| 21 | +public class CommonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { |
| 22 | + |
| 23 | + //item类型 |
| 24 | + public static final int ITEM_TYPE_HEADER = 0; |
| 25 | + public static final int ITEM_TYPE_CONTENT = 1; |
| 26 | + public static final int ITEM_TYPE_BOTTOM = 2; |
| 27 | + //模拟数据 |
| 28 | + public String[] texts = {"java"}; |
| 29 | + private LayoutInflater mLayoutInflater; |
| 30 | + private Context mContext; |
| 31 | + private int mHeaderCount = 1;//头部View个数 |
| 32 | + private int mBottomCount = 1;//底部View个数 |
| 33 | + List<String> stringList = new ArrayList<>(); |
| 34 | + |
| 35 | + |
| 36 | + public CommonAdapter(Context context) { |
| 37 | + mContext = context; |
| 38 | + mLayoutInflater = LayoutInflater.from(context); |
| 39 | + for (int i = 0; i < 20; i++) { |
| 40 | + stringList.add("数据--->" + i); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + //内容长度 |
| 45 | + public int getContentItemCount() { |
| 46 | + return texts.length; |
| 47 | + } |
| 48 | + |
| 49 | + //判断当前item是否是HeadView |
| 50 | + public boolean isHeaderView(int position) { |
| 51 | + return mHeaderCount != 0 && position < mHeaderCount; |
| 52 | + } |
| 53 | + |
| 54 | + //判断当前item是否是FooterView |
| 55 | + public boolean isBottomView(int position) { |
| 56 | + return mBottomCount != 0 && position >= (mHeaderCount + getContentItemCount()); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + //判断当前item类型 |
| 61 | + @Override |
| 62 | + public int getItemViewType(int position) { |
| 63 | + int dataItemCount = getContentItemCount(); |
| 64 | + if (mHeaderCount != 0 && position < mHeaderCount) { |
| 65 | + //头部View |
| 66 | + return ITEM_TYPE_HEADER; |
| 67 | + } else if (mBottomCount != 0 && position >= (mHeaderCount + dataItemCount)) { |
| 68 | + //底部View |
| 69 | + return ITEM_TYPE_BOTTOM; |
| 70 | + } else { |
| 71 | + //内容View |
| 72 | + return ITEM_TYPE_CONTENT; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + //内容 ViewHolder |
| 77 | + public static class ContentViewHolder extends RecyclerView.ViewHolder { |
| 78 | + private NestedRecyclerView rv_list; |
| 79 | + |
| 80 | + public ContentViewHolder(View itemView) { |
| 81 | + super(itemView); |
| 82 | + rv_list = (NestedRecyclerView) itemView.findViewById(R.id.rv_item_list); |
| 83 | + ScrollManager.getInstance().addSingleView(rv_list); |
| 84 | + ScrollManager.getInstance().setLastItemView(itemView); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + //头部 ViewHolder |
| 89 | + public static class HeaderViewHolder extends RecyclerView.ViewHolder { |
| 90 | + |
| 91 | + public HeaderViewHolder(View itemView) { |
| 92 | + super(itemView); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + //底部 ViewHolder |
| 97 | + public static class BottomViewHolder extends RecyclerView.ViewHolder { |
| 98 | + |
| 99 | + public BottomViewHolder(View itemView) { |
| 100 | + super(itemView); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 106 | + if (viewType == ITEM_TYPE_HEADER) { |
| 107 | + return new HeaderViewHolder(mLayoutInflater.inflate(R.layout.item_header, parent, false)); |
| 108 | + } else if (viewType == ITEM_TYPE_CONTENT) { |
| 109 | + return new ContentViewHolder(mLayoutInflater.inflate(R.layout.item_list_content2, parent, false)); |
| 110 | + } else if (viewType == ITEM_TYPE_BOTTOM) { |
| 111 | + return new BottomViewHolder(mLayoutInflater.inflate(R.layout.item_footer, parent, false)); |
| 112 | + } |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 118 | + if (holder instanceof HeaderViewHolder) { |
| 119 | + holder.itemView.setOnClickListener(new View.OnClickListener() { |
| 120 | + @Override |
| 121 | + public void onClick(View v) { |
| 122 | + Log.e("aaa", "header------------>"); |
| 123 | + } |
| 124 | + }); |
| 125 | + } else if (holder instanceof ContentViewHolder) { |
| 126 | + CustomLayoutManager layoutManager = new CustomLayoutManager(mContext, ((ContentViewHolder) holder).rv_list); |
| 127 | + layoutManager.setOrientation(LinearLayoutManager.VERTICAL); |
| 128 | + ((ContentViewHolder) holder).rv_list.setLayoutManager(layoutManager); |
| 129 | + ScrollTest3Activity.SingleTextAdapter adapter = new ScrollTest3Activity.SingleTextAdapter(); |
| 130 | + adapter.setData(stringList); |
| 131 | + ((ContentViewHolder) holder).rv_list.setAdapter(adapter); |
| 132 | + |
| 133 | + } else if (holder instanceof BottomViewHolder) { |
| 134 | + |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public int getItemCount() { |
| 140 | + return mHeaderCount + getContentItemCount() + mBottomCount; |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments