您好,登錄后才能下訂單哦!
Android中怎么利用RecyclerView實現列表嵌套效果,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1. 定義多布局對象的基類:
public class BaseMulDataModel { protected int type; public int getType() { return type; } public void setType(int type) { this.type = type; } }
type是該對象對應的布局類型。
2. recyclerview數據的顯示放在ViewHolder中,定義Holder基類
public abstract class BaseMulViewHolder<T extends BaseMulDataModel> extends RecyclerView.ViewHolder { public BaseMulViewHolder(View itemView) { super(itemView); } protected abstract void bindData(T dataModel); }
這里面多布局中可能涉及到的多個對象,所以基類中的對象類型使用泛型定義,必須是多布局對象基類的子類,這樣在后面數據和控件綁定的時候比較方便。
3. 開始創建多布局適配器
public class MullayoutAdapter extends RecyclerView.Adapter<BaseMulViewHolder> { /** * 定義三種布局類型 */ public static final int TYPE_ONE = 1; public static final int TYPE_TWO = 2; public static final int TYPE_THREE = 3; /** * 數據集合 */ private List<BaseMulDataModel> mList; @Override public BaseMulViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //根據不同的布局類型,設置創建相關的holder switch (viewType) { case TYPE_ONE: return new ViewHolderOne(LayoutInflater.from(parent.getContext()) .inflate(R.layout.layout_holder1, parent, false)); case TYPE_TWO: return new ViewHolderTwo(LayoutInflater.from(parent.getContext()) .inflate(R.layout.layout_holder2, parent, false)); case TYPE_THREE: return new ViewHolderThree(LayoutInflater.from(parent.getContext()) .inflate(R.layout.layout_holder3, parent, false)); } return null; } @Override public void onBindViewHolder(BaseMulViewHolder holder, int position) { //綁定數據 holder.bindData(mList.get(position)); } @Override public int getItemCount() { return mList.size(); } @Override public int getItemViewType(int position) { return mList.get(position).getType(); } /** * 設置數據 * * @param list */ public void setDatas(List<BaseMulDataModel> list) { mList = list; notifyDataSetChanged(); } public List<BaseMulDataModel> getDatas() { return mList; } /** * 設置第一個布局的數據 */ class ViewHolderOne extends BaseMulViewHolder<OneModel> { TextView textView; public ViewHolderOne(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.holder1_tv); } @Override protected void bindData(OneModel dataModel) { textView.setText(dataModel.getTitle()); } } /** * 設置第二個布局的數據 */ class ViewHolderTwo extends BaseMulViewHolder<TwoModel> { ImageView imageView; public ViewHolderTwo(View itemView) { super(itemView); imageView = (ImageView) itemView.findViewById(R.id.holder2_iv); } @Override protected void bindData(TwoModel dataModel) { imageView.setImageResource(dataModel.getRes()); } } /** * 設置第三個布局的數據 */ class ViewHolderThree extends BaseMulViewHolder<ThreeModel> { TextView textView; public ViewHolderThree(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.holder3_tv); } @Override protected void bindData(ThreeModel dataModel) { textView.setText(dataModel.getNote()); } } }
首先這邊涉及到布局類型:頭部、內容列表、底部。定義三種類型
/** * 定義三種布局類型 */ public static final int TYPE_ONE = 1; public static final int TYPE_TWO = 2; public static final int TYPE_THREE = 3;
根據布局類型來創建對應的ViewHolder對象
public BaseMulViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //根據不同的布局類型,設置創建相關的holder switch (viewType) { case TYPE_ONE: return new ViewHolderOne(LayoutInflater.from(parent.getContext()) .inflate(R.layout.layout_holder1, parent, false)); case TYPE_TWO: return new ViewHolderTwo(LayoutInflater.from(parent.getContext()) .inflate(R.layout.layout_holder2, parent, false)); case TYPE_THREE: return new ViewHolderThree(LayoutInflater.from(parent.getContext()) .inflate(R.layout.layout_holder3, parent, false)); } return null; }
當然事先創建對應的Holder類:
/** * 設置第一個布局的數據 */ class ViewHolderOne extends BaseMulViewHolder<OneModel> { TextView textView; public ViewHolderOne(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.holder1_tv); } @Override protected void bindData(OneModel dataModel) { textView.setText(dataModel.getTitle()); } }
這邊把泛型對象擦除,使用具體對象OneModel來作為當前的數據對象。OneModel是BaseMulDataModel的基類。
OneModel的定義:
public class OneModel extends BaseMulDataModel { private String title; public OneModel(String title, int type) { this.title = title; this.type = type; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
4. 進行數據處理
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recycler.setLayoutManager(layoutManager); final MullayoutAdapter adapter = new MullayoutAdapter(); recycler.setAdapter(adapter); //數據處理 List<BaseMulDataModel> mList = new ArrayList<>(); for (int i = 0; i < 5; i++) { mList.add(new OneModel("頭部" + i, MullayoutAdapter.TYPE_ONE)); for (int j = 0; j < 3; j++) { mList.add(new TwoModel(R.mipmap.ic_launcher, MullayoutAdapter.TYPE_TWO)); } mList.add(new ThreeModel("底部" + i, MullayoutAdapter.TYPE_THREE)); } adapter.setDatas(mList); } }
關于Android中怎么利用RecyclerView實現列表嵌套效果問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。