如何从RecyclerView列表中获取所选项目?

时间:2016-01-04 12:54:51

标签: android onclicklistener android-recyclerview

我创建了一个界面,用于扩展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消息,但它不显示任何内容。我正在实例化类的数组,我不知道如何听点击项目。那里的答案并没有帮助我解决我的问题。

2 个答案:

答案 0 :(得分:2)

目前,您正在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();
}

答案 1 :(得分:0)

我的解决方案: 涉及步骤

  1. 创建界面RecyclerviewClickListener
  2. 在活动/片段上实现接口
  3. 将接口传递给适配器
  4. 对点击的项目调用接口方法onBindViewHolder

RecyclerviewClickListener 接口

   //Created an interface RecyclerviewClickListener
    public interface RecyclerviewClickListener
    {
        void recyclerviewClickListener(int position);
    }

新片段

//Implemented interface on an activity/fragment
public class NewFragment extends Fragment implements RecyclerviewClickListener
{
   //Passed the interface to the adapter
   adapter = new Adapter(getContext(), dataList, this);

    @Override
    public void recyclerviewClickListener(int position)
    {
      //Do something here with the position from a recyclerview
      //e.g dataList.get(position);
    }
}

适配器

public class UniversityAdapter extends RecyclerView.Adapter<UniversityAdapter.ViewHolder>
{
    private final ArrayList<User> dataList;
    private final Context context;
    private  RecyclerviewClickListener listener;

    public Adapter(Context context, ArrayList<User> dataList, RecyclerviewClickListener itemListener)
    {
        context = context;
        dataList = universityArrayList;
        listener = itemListener;
    }
    
        @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position)
    {
       holder.userName.setOnclickListener(view -> listener.recyclerviewClickListener(position));

    }
}