当前位置:   article > 正文

recyclerView 多布局_recycleview多布局

recycleview多布局
  1. package com.boyou.live.adapter;
  2. import android.content.Context;
  3. import android.graphics.Typeface;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.RelativeLayout;
  7. import android.widget.TextView;
  8. import androidx.annotation.NonNull;
  9. import androidx.recyclerview.widget.RecyclerView;
  10. import com.boyou.live.R;
  11. import com.boyou.live.bean.ChatListBean;
  12. import java.util.List;
  13. public class ListAdapter extends RecyclerView.Adapter {
  14. Context context;
  15. List<ChatListBean.DataBean> data;
  16. public ListAdapter(Context context, List<ChatListBean.DataBean> data) {
  17. this.context = context;
  18. this.data = data;
  19. }
  20. @NonNull
  21. @Override
  22. public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  23. switch (viewType) {
  24. case 0:
  25. return new ViewHolderOne(View.inflate(context, R.layout.item_chatlist_lb, null));
  26. case 1:
  27. return new ViewHolderTwo(View.inflate(context, R.layout.item_chatlist_lb_txt, null));
  28. }
  29. return null;
  30. }
  31. @Override
  32. public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
  33. ChatListBean.DataBean dataBean = data.get(position);
  34. String u_name = dataBean.getU_name();
  35. String chat = dataBean.getChat();
  36. if (holder instanceof ViewHolderOne) {
  37. int spectator_lv = dataBean.getSpectator_lv();
  38. ((ViewHolderOne) holder).chatlist_dj_tv.setText(spectator_lv + "");
  39. ((ViewHolderOne) holder).chatlist_namer_tv.setText(u_name + ":");
  40. ((ViewHolderOne) holder).chatlist_nr.setText(chat);
  41. } else if (holder instanceof ViewHolderTwo) {
  42. ((ViewHolderOne) holder).chatlist_nr_tv.setText(chat);
  43. }
  44. }
  45. @Override
  46. public int getItemCount() {
  47. return data.size();
  48. }
  49. public class ViewHolderOne extends RecyclerView.ViewHolder {
  50. RelativeLayout chatlist_dj_tv_rl, chatlist_nr_tv_rl;
  51. TextView chatlist_dj_tv, chatlist_namer_tv, chatlist_nr, chatlist_nr_tv;
  52. public ViewHolderOne(@NonNull View itemView) {
  53. super(itemView);
  54. chatlist_dj_tv = itemView.findViewById(R.id.chatlist_dj_tv);
  55. chatlist_namer_tv = itemView.findViewById(R.id.chatlist_namer_tv);
  56. chatlist_nr = itemView.findViewById(R.id.chatlist_nr);
  57. chatlist_dj_tv_rl = itemView.findViewById(R.id.chatlist_dj_tv_rl);
  58. chatlist_nr_tv_rl = itemView.findViewById(R.id.chatlist_nr_tv_rl);
  59. chatlist_nr_tv = itemView.findViewById(R.id.chatlist_nr_tv);
  60. Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "pf_ht_z_j.ttf");
  61. chatlist_dj_tv.setTypeface(typeFace);
  62. chatlist_namer_tv.setTypeface(typeFace);
  63. chatlist_nr.setTypeface(typeFace);
  64. chatlist_nr_tv.setTypeface(typeFace);
  65. }
  66. }
  67. public class ViewHolderTwo extends RecyclerView.ViewHolder {
  68. TextView chatlist_nr_tv;
  69. public ViewHolderTwo(View itemView) {
  70. super(itemView);
  71. chatlist_nr_tv = itemView.findViewById(R.id.chatlist_nr_tv);
  72. Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "pf_ht_z_j.ttf");
  73. chatlist_nr_tv.setTypeface(typeFace);
  74. }
  75. }
  76. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/174988
推荐阅读
相关标签
  

闽ICP备14008679号