gpt4 book ai didi

java - 如何修复RecyclerView OnClick间隙显示bug?

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:30 24 4
gpt4 key购买 nike

我正在设置一个包含多个 ImageButtons 的 RecyclerView,每个 ImageButton 都有一个 OnClick 操作(启动 Activity)我的问题是,OnClick 操作不适用于 ImageButtons,但在每个 ImageButton 之间有一点空间(如果我想单击第一个按钮,我必须单击第一个和第二个按钮之间的空间)

我尝试过更改图像大小

RecyclerView 适配器:

   public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recycler_view, parent, false);
return new ViewHolder(view);
}

// binds the data to the ImageButton in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
int currentCategory = mData.get(position).getImage();
holder.categoriesButton.setImageResource(currentCategory);
}

// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageButton categoriesButton;

ViewHolder(View itemView) {
super(itemView);
categoriesButton = itemView.findViewById(R.id.image_Button_Category_id);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}

// convenience method for getting data at click position
public int getItem(int id) {
return mData.get(id).getImage();
}

// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}

回收器布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/image_Button_Category_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

这是在我的 MainActivity 中:

  private void setRecyclerViewOn(ArrayList<Category> list) {
RecyclerView recyclerView = findViewById(R.id.recyclerView_id);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new RecyclerViewAdapter(this, list);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}

@Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
startActivity(createIntent(position));
}

预期结果:点击监听器必须位于每个 ImageButton 上

最佳答案

尝试在 onBindView() 上添加点击监听器

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
int currentCategory = mData.get(position).getImage();
holder.categoriesButton.setImageResource(currentCategory);
holder.vategoriesButton.setOnClickListener(...)
}

关于java - 如何修复RecyclerView OnClick间隙显示bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55592218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com