自定义RecyclerView.Adapter

自定义显示子项

在ViewHolder中定义recyclerView列表中每个显示子项的具体样式
布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_10"
    android:id="@+id/clItemLayout"
    android:background="@drawable/new_broad_list_item_style">

    <TextView
        android:id="@+id/tvDeviceName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dp_8"
        android:layout_marginLeft="@dimen/dp_8"
        android:layout_marginTop="@dimen/dp_8"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="设备名称"
        android:textColor="@color/c_ffffff"
        android:textSize="@dimen/dp_18" />
  ...

</androidx.constraintlayout.widget.ConstraintLayout>

示例代码如下:

class ViewHolder extends RecyclerView.ViewHolder {

        ConstraintLayout clItemLayout;
        TextView tvDeviceState, tvDeviceName, tvDeviceMac, tvDeviceIP,
                tvDeviceArea, tvIssue, tvAmend, tvDelete, tvDeviceBroadState, tvDeviceTransfer;

        public ViewHolder(View itemView) {
            super(itemView);
            clItemLayout = itemView.findViewById(R.id.clItemLayout);
            tvDeviceName = itemView.findViewById(R.id.tvDeviceName);
            ...

        }
    }

重写方法

以下三个方法必须重写

public int getItemCount()
// 返回显示子项的布局
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
// 绑定view,注册点击事件
public void onBindViewHolder(@NonNull ViewHolder holder, int position)

示例代码如下:

public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.ViewHolder> {

    private List<DeviceEntity.ReturnDataBean> beanList;
    private Context context;

    public DeviceListAdapter() {
        beanList = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
                R.layout.fragment_device_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DeviceEntity.ReturnDataBean bean = beanList.get(position);
        holder.tvDeviceName.setText(bean.getName());
        holder.tvDeviceMac.setText(bean.getMac());
        holder.tvDeviceIP.setText(bean.getIp());
        holder.tvDeviceArea.setText(bean.getLocation());
        holder.tvDeviceState.setText(bean.getDeviceStatusPlay());

        holder.clItemLayout.setOnClickListener(v -> {
            if (Utils.isFastClick()) return;
            new DeviceDetailsDialog(context, bean).setOnDismissListener(dialog -> {
            });
        });
        holder.tvDeviceTransfer.setOnClickListener(v -> {
            new DeviceTransferDialog(context);
        });
        ...

    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }
    
    class ViewHolder extends RecyclerView.ViewHolder {

        ConstraintLayout clItemLayout;
        TextView tvDeviceState, tvDeviceName, tvDeviceMac, tvDeviceIP,
                tvDeviceArea, tvIssue, tvAmend, tvDelete, tvDeviceBroadState, tvDeviceTransfer;

        public ViewHolder(View itemView) {
            super(itemView);
            clItemLayout = itemView.findViewById(R.id.clItemLayout);
            tvDeviceName = itemView.findViewById(R.id.tvDeviceName);
            ...

        }
    }

    private int getColor(int colorId) {
        return context.getResources().getColor(colorId);
    }

}

在界面中的调用

public class DeviceFragment extends BaseFragment {

 private RecyclerView rvDeviceAllList;
    private DeviceListAdapter allAdapter;

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListView();
    }
}

private void initListView() {
        rvDeviceAllList = View.findViewById(R.id.rvDeviceAllList);
        allAdapter = new DeviceListAdapter();
        rvDeviceAllList.setAdapter(allAdapter);
    }

// 每次adapter中的list数据改变时,调用以下方法
allAdapter.notifyDataSetChanged();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值