当我按下复选框时在 ViewHolder 之间切换

我的片段中有 RecycleView 适配器和复选框。我希望当我按下复选框时,旧视图会发生变化,并且绑定和过滤会有所不同。但是当我运行该应用程序时,它无法正常工作并保持原始模式。你能帮我找出问题吗?

小唯快跑啊
浏览 76回答 1
1回答

杨__羊羊

您的代码中有两个问题。第一个很简单:当您单击复选框时,您会更新 的值,flag但您没有告诉RecyclerView它需要更新其内容:checkDistance.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; if (!checkDistance.isChecked()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = false;//the flag to recycle view&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (checkDistance.isChecked()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = true;//the flag to recycle view&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }});您可以通过在更新标志后通知适配器来解决此问题。您还可以简化布尔逻辑:checkDistance.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; flag = checkDistance.isChecked();&nbsp; &nbsp; &nbsp; &nbsp; adapter.notifyDataSetChanged();&nbsp; &nbsp; }});第二个问题更复杂。问题是您使用两个不同ViewHolder的子类,具体取决于flag. 如果你想使用不同种类的 ViewHolders,你需要getItemViewType()在你的适配器中实现这个方法。您应该确保flag 只检查inside getItemViewType(),并在其他任何地方使用此方法的结果(视图类型)。public class DrivesRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemViewType(int position) {&nbsp; &nbsp; &nbsp; &nbsp; return flag ? 0 : 1;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; View view = LayoutInflater.from(parent.getContext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .inflate(R.layout.item_drive, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; if (viewType == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new DriveViewHolder1(view);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new DriveViewHolder2(view);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(@NonNull ViewHolder holder, int position) {&nbsp; &nbsp; &nbsp; &nbsp; Drive drive = drives.get(position);&nbsp; &nbsp; &nbsp; &nbsp; int viewType = getItemViewType(position);&nbsp; &nbsp; &nbsp; &nbsp; if (viewType == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DriveViewHolder1 vaultItemHolder1 = (DriveViewHolder1) holder;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vaultItemHolder1.nameTextView.setText(drive.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vaultItemHolder1.phoneTextView.setText(drive.getStartAddress());//the change&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DriveViewHolder2 vaultItemHolder2 = (DriveViewHolder2) holder;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vaultItemHolder2.nameTextView.setText(drive.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vaultItemHolder2.phoneTextView.setText(drive.getEmail());//the change&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java