听取RecyclerView的点击

时间:2015-03-06 00:36:48

标签: android android-recyclerview

我一直在努力在RecyclerView上实现点击监听器。我已经看过几种方法,并且更愿意处理来自Activity的ImageView点击,因为我需要将Intent传递给另一个Activity并在主线程上做一些工作。以下是我迄今为止实施的内容:

在自定义RecyclerView适配器中注册单击

public class BasemapAdapter extends RecyclerView.Adapter<BasemapViewHolder> {

// Context to construct view
private Context mContext;
// Copy of all BasemapItems
private ArrayList<BasemapItem> items;

private int rowLayout;

public BasemapAdapter(ArrayList<BasemapItem> portalItems, int rowLayout, Context context){
    this.mContext = context;
    this.items = portalItems;
    this.rowLayout = rowLayout;
}

@Override
public BasemapViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);

    return new BasemapViewHolder(view);

}

/**
 * Fill the view with data from our adapter
 *
 * @param viewHolder
 * @param position
 */
@Override
public void onBindViewHolder(BasemapViewHolder viewHolder, final int position){
    // get the element from your dataset at this position
    // replace the contents of the view with the element
    viewHolder.basemapImage.setImageBitmap(items.get((position)).basemapImage);
    viewHolder.basemapName.setText(items.get((position)).item.getTitle());

    viewHolder.setBasemapClickListener(new BasemapViewHolder.BasemapClickListener() {
        @Override
        public void onBasemapItemClicked(View view, int position) {
            Log.d("TAG", "clicked at " + position);
        }
    });

}

@Override
public int getItemCount(){
    return items == null ? 0 : items.size();
}
}

自定义ViewHolder

public class BasemapViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

@InjectView(R.id.basemapName) TextView basemapName;
@InjectView(R.id.basemapImage) ImageView basemapImage;

private BasemapClickListener clickListener;

public BasemapViewHolder(View itemView) {
    super(itemView);

    ButterKnife.inject(this, itemView);

    // set listener to whole item view
    itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    clickListener.onBasemapItemClicked(v, getPosition());
}

/* setter for the listener */
public void setBasemapClickListener(BasemapClickListener clickListener){
    this.clickListener = clickListener;
}

/**
 * A callback interface that indicates when a basemap has been clicked
 */
public interface BasemapClickListener{
    /**
     * Callback when the view is clicked
     *
     * @param view view that is clicked
     * @param position position of the clicked basemap
     */
    public void onBasemapItemClicked(View view, int position);


}

}

在Activity

中创建适配器的实例
mBasemapAdapter = new BasemapAdapter( mBasemapList , R.layout.row_basemap, this);

mRecyclerView.setAdapter(mBasemapAdapter);

2 个答案:

答案 0 :(得分:0)

如果您只想处理ImageView点击,请按以下步骤操作:

@InjectView(R.id.basemapName) TextView basemapName;
@InjectView(R.id.basemapImage) ImageView basemapImage;

private BasemapClickListener clickListener;

public BasemapViewHolder(View itemView) {
    super(itemView);

    ButterKnife.inject(this, itemView);

    // set listener to whole item view
    //itemView.setOnClickListener(this); // comment out this line
    basemapImage.setOnClickListener(this); // <---- add this line
}

你应该将setBasemapClickListener从onBindViewHolder()移动到onCreateViewHolder()。

@Override
public BasemapViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
view.setBasemapClickListener(new BasemapViewHolder.BasemapClickListener() {
        @Override
        public void onBasemapItemClicked(View view, int position) {
            Log.d("TAG", "clicked at " + position);
        }
    });
    return new BasemapViewHolder(view);

}

答案 1 :(得分:0)

我通过在TextView上覆盖ImageView时设置onClickListener来解决这个问题:

BasemapAdapter

    @Override
public void onBindViewHolder(BasemapViewHolder viewHolder, final int position){
    final BasemapItem item = items.get(position);
    // get the element from your dataset at this position
    // replace the contents of the view with the element
    viewHolder.image.setImageBitmap(items.get(position).getImage());
    viewHolder.title.setText(items.get(position).getItem().getTitle());

    viewHolder.title.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(item != null){
                clickListener.onImageClick(items.indexOf(item));
            }
        }
    });

    viewHolder.itemView.setTag(item);
}

然后我可以从Activity

中调用onBaseMapClickListener
        mBasemapAdapter.setOnBaseMapClickListener(new BasemapClickListener() {

        @Override
        public void onImageClick(int position) {
            Toast.makeText(getApplicationContext(), "tapped" + position, Toast.LENGTH_LONG).show();
        }
    });