recyclerView 多布局

这个博客展示了如何创建一个自定义适配器`classListAdapter`,用于显示RecyclerView中的聊天列表。适配器处理两种类型的视图,ViewHolderOne和ViewHolderTwo,分别对应不同的聊天列表项布局。代码中使用了Typeface设置自定义字体,并根据数据bean设置了UI元素的文本。
摘要由CSDN通过智能技术生成
package com.boyou.live.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.boyou.live.R;
import com.boyou.live.bean.ChatListBean;

import java.util.List;


public class ListAdapter extends RecyclerView.Adapter {


    Context context;
    List<ChatListBean.DataBean> data;

    public ListAdapter(Context context, List<ChatListBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        switch (viewType) {
            case 0:
                return new ViewHolderOne(View.inflate(context, R.layout.item_chatlist_lb, null));
            case 1:
                return new ViewHolderTwo(View.inflate(context, R.layout.item_chatlist_lb_txt, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ChatListBean.DataBean dataBean = data.get(position);

        String u_name = dataBean.getU_name();
        String chat = dataBean.getChat();

        if (holder instanceof ViewHolderOne) {
            int spectator_lv = dataBean.getSpectator_lv();
            ((ViewHolderOne) holder).chatlist_dj_tv.setText(spectator_lv + "");
            ((ViewHolderOne) holder).chatlist_namer_tv.setText(u_name + ":");
            ((ViewHolderOne) holder).chatlist_nr.setText(chat);
        } else if (holder instanceof ViewHolderTwo) {
            ((ViewHolderOne) holder).chatlist_nr_tv.setText(chat);
        }

    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolderOne extends RecyclerView.ViewHolder {
        RelativeLayout chatlist_dj_tv_rl, chatlist_nr_tv_rl;
        TextView chatlist_dj_tv, chatlist_namer_tv, chatlist_nr, chatlist_nr_tv;

        public ViewHolderOne(@NonNull View itemView) {
            super(itemView);
            chatlist_dj_tv = itemView.findViewById(R.id.chatlist_dj_tv);
            chatlist_namer_tv = itemView.findViewById(R.id.chatlist_namer_tv);
            chatlist_nr = itemView.findViewById(R.id.chatlist_nr);
            chatlist_dj_tv_rl = itemView.findViewById(R.id.chatlist_dj_tv_rl);

            chatlist_nr_tv_rl = itemView.findViewById(R.id.chatlist_nr_tv_rl);
            chatlist_nr_tv = itemView.findViewById(R.id.chatlist_nr_tv);


            Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "pf_ht_z_j.ttf");
            chatlist_dj_tv.setTypeface(typeFace);
            chatlist_namer_tv.setTypeface(typeFace);
            chatlist_nr.setTypeface(typeFace);

            chatlist_nr_tv.setTypeface(typeFace);
        }
    }

    public class ViewHolderTwo extends RecyclerView.ViewHolder {
        TextView chatlist_nr_tv;

        public ViewHolderTwo(View itemView) {
            super(itemView);
            chatlist_nr_tv = itemView.findViewById(R.id.chatlist_nr_tv);

            Typeface typeFace = Typeface.createFromAsset(context.getAssets(), "pf_ht_z_j.ttf");
            chatlist_nr_tv.setTypeface(typeFace);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值