区分Recyclerview点击中的列

时间:2017-07-01 18:44:41

标签: java android onclick android-recyclerview

开始我会感谢那些帮助noobi dev喜欢我的人:p!假设我有一个Recyclerview,其中每个单元元素都有两个图像。实际上,由于MyAdapter中的这个小技巧,我能够找到被点击的单位元素的指示以解雇好的事件:

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,     parent, false);
    final MyHolder holder = new MyHolder(v);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onItemClick(v, holder.getAdapterPosition());
        }
    });
    return holder;
}

然后我在活动中以这种方式开始活动:

MyAdapter adapter = new MyAdapter(this, countryCollection.getCountryArray(), new CustomItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            //Launch the events
            ArrayList<String> languageCodeArray = countryCollection.getLanguageCodeArray();
            ArrayList<String> countryIdArray = countryCollection.getCountryIdArray();
            if(position >0){
                String languageCode = languageCodeArray.get(position-1);
                String countryId = countryIdArray.get(position-1);
                Intent intent = new Intent(context, LetsSpeakActivity.class);
                intent.putExtra("country_id", countryId);
                intent.putExtra("language_code", languageCode);
                startActivity(intent);
            }
        }
    });
    rv.setAdapter(adapter);

但是如何才能将我的RecyclerView的两个图像进行差异化以激发好事件?我一直在寻找如何区分屏幕左侧或右侧的点击,但显然这不是正确的方法!任何帮助都很可爱,我真的被卡住了......

1 个答案:

答案 0 :(得分:0)

如果您已在ImageView课程中定义了MyHolder,则可以在持有人构造函数中为其设置OnClickListener

public class MyHolder extends RecyclerView.ViewHolder {

    public ImageView imageLeft;

    public ImageView imageRight;

    public CustomItemClickListener listenerLeft;

    public CustomItemClickListener listenerRight;

    public MyHolder(View v, CustomItemClickListener listenerLeft, CustomItemClickListener listenerRight) {
        super(v);
        this.listenerLeft = listenerLeft;
        this.listenerRight = listenerRight;
        imageLeft = v.findViewById(R.id.image_left);
        imageRight = v.findViewById(R.id.image_right);

        imageLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listenerLeft.onItemClick(v, getPosition());
            }
        });

        imageRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listenerRight.onItemClick(v, getPosition());
            }
        });
    }
}

同时在onCreateViewHolder中注入两个侦听器:

@Override
public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
    return new MyHolder(v, mImageLeftListener, mImageRightListener);
}

您可以在Adapter构造函数中引入这两个侦听器,就像使用listener

一样

或者,如果您只想传递一个侦听器,则可以使用2种方法定义CustomItemClickListener接口(或者只使用一个额外的参数来区分单击的图像):

public interface CustomItemClickListener {
    void onLeftImageClick(View view, int position);
    void onRightImageClick(View view, int position);
}