自定义显示子项
在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();