gpt4 book ai didi

android - 如何从 RecyclerView 列表中获取选定的项目?

转载 作者:行者123 更新时间:2023-12-05 05:24:27 26 4
gpt4 key购买 nike

我已经创建了一个接口(interface)来扩展 Recycler View 的功能,并可以监听列表中每一项的点击,但我无法让它工作。当我点击任何项目时没有任何反应。这是我的代码:

// METHOD FOR INITIALIZATION OF RECYCLER VIEW
public void initRecyclerView() {
// SETTING ARRAY FOR CATEGORIES
final CategoryItem categoryItem[] = { new CategoryItem("Birthday", R.drawable.birthday_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image)};

// FINDING RECYCLER VIEW IN LAYOUT
recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));

adapter = new CategoriesListAdapter(categoryItem);
recyclerView.setAdapter(adapter);

// SETTING ON CLICK LISTENER ON ADAPTER
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void setOnItemClick(View view, int position) {
String clickedItem = String.valueOf(position);
if ( clickedItem.equals("Birthday") ) {
Toast.makeText(MainActivity.this, "Clicked on Birthday!", Toast.LENGTH_SHORT).show();
}
}
});
recyclerView.setItemAnimator(new DefaultItemAnimator());

}

更新:

适配器

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

private CategoryItem[] categoryItems;
private static OnItemClickListener onItemClickListener;

public CategoriesListAdapter(CategoryItem[] categoryItems) {
this.categoryItems = categoryItems;
}

@Override
public CategoriesListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}


@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

viewHolder.txtViewTitle.setText(categoryItems[position].getTitle());
viewHolder.imgViewIcon.setImageResource(categoryItems[position].getImage());

}

// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView txtViewTitle;
public ImageView imgViewIcon;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.image);
itemLayoutView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
onItemClickListener.setOnItemClick(v, getAdapterPosition());
}
}

public void setOnItemClickListener (final OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}

// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return categoryItems.length;
}

}

接口(interface):

public interface OnItemClickListener {

public void setOnItemClick(View view, int position);

}

如果选择生日,它应该显示 toast 消息,但它没有显示任何内容。我正在实例化类数组,但我不知道如何收听点击的项目。那里的答案没有帮助我解决我的问题。

最佳答案

目前您正在 OnItemClickListener 方法 setOnItemClick 中根据字符串“Birthday”检查您的位置

String clickedItem = String.valueOf(position);
if ( clickedItem.equals("Birthday") ) {
...
}

这永远不会是真的。尝试获取位置项并根据“生日”检查其第一个参数,或者从该 if 语句中取出您的 Toast 并打印出位置编号以开始了解那里发生了什么

@Override
public void setOnItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "Clicked item on position: " + position, Toast.LENGTH_SHORT).show();
}

关于android - 如何从 RecyclerView 列表中获取选定的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34591665/

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