RecyclerView中的setOnClickListener

时间:2018-02-05 04:02:52

标签: android

我正在尝试为将在RecyclerView中添加的所有视图设置ImageButtons和Buttons的OnClickListener。

我不知道怎么做...... 我正在尝试在适配器的ViewHolder中设置它 像这样:

public Todo_itemViewHolder(View itemView) {
        super(itemView);
        Title=itemView.findViewById(R.id.todo_txt_Titleitem);
        Content=itemView.findViewById(R.id.todo_txt_Contentitem);
        edit=itemView.findViewById(R.id.todo_btn_edit);
        remove=itemView.findViewById(R.id.todo_btn_remove);
        Add=itemView.findViewById(R.id.todo_btn_add);

        Add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Todo_Adapter.this, Todo_saveActivity.class);
                startActivityForResult(intent,Request_Code);
            }
        });

感谢。

5 个答案:

答案 0 :(得分:0)

第一个你喜欢的界面:

   public interface OnItemClickListener {
    void onItemClick(ContentItem item);
}

在Adpter之后:

private final List<ContentItem> items;
private final OnItemClickListener listener;

public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
    this.items = items;
    this.listener = listener;
}

并实施它:

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

    public interface OnItemClickListener {
        void onItemClick(ContentItem item);
    }

    private final List<ContentItem> items;
    private final OnItemClickListener listener;

    public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
        this.items = items;
        this.listener = listener;
    }

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

    @Override public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(items.get(position), listener);
    }

    @Override public int getItemCount() {
        return items.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            image = (ImageView) itemView.findViewById(R.id.image);
        }

        public void bind(final ContentItem item, final OnItemClickListener listener) {
            name.setText(item.name);
            Picasso.with(itemView.getContext()).load(item.imageUrl).into(image);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    listener.onItemClick(item);
                }
            });
        }
    }
}

在活动或片段中:

recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
    @Override public void onItemClick(ContentItem item) {
        Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
        Intent intent=new Intent(SomeActivity.this, Todo_saveActivity.class);
            startActivityForResult(intent,Request_Code);
    }
}));

答案 1 :(得分:0)

您无法使用适配器引用启动活动,您必须从活动/上下文开始,如下所示,

Intent intent=new Intent(MyActivity.this/myContext,Your2ndActivity.class);
startActivity(intent)

将yur适配器中的上下文作为构造函数的参数传递,或者使用视图中的上下文。

contxt=v.getContext();

答案 2 :(得分:0)

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

        public Todo_itemViewHolder(View itemView) {
            super(itemView);
            Title = itemView.findViewById(R.id.todo_txt_Titleitem);
            Content = itemView.findViewById(R.id.todo_txt_Contentitem);
            edit = itemView.findViewById(R.id.todo_btn_edit);
            remove = itemView.findViewById(R.id.todo_btn_remove);
            Add = itemView.findViewById(R.id.todo_btn_add);

            Add.setOnClickListener(this);
            remove.setOnClickListener(this);
            ... //declare listeners for other views
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {

                case R.id.todo_btn_add: {  //add button click
                    Intent intent = new Intent(Todo_Adapter.this, Todo_saveActivity.class);
                    startActivityForResult(intent, Request_Code);
                }

                case R.id.todo_btn_remove: {  // remove button click
                }

                ... // add other listener events
            }
        }
    }

这样,您可以使用最少的代码将点击侦听器添加到所有必需的视图,并轻松地为每个点击事件提供代码。 (好吧,就像第一次使用像ButterKnife这样的库一样容易!)

答案 3 :(得分:0)

public static class Todo_itemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    Title=itemView.findViewById(R.id.todo_txt_Titleitem);
    Content=itemView.findViewById(R.id.todo_txt_Contentitem);
    edit=itemView.findViewById(R.id.todo_btn_edit);
    remove=itemView.findViewById(R.id.todo_btn_remove);
    Add=itemView.findViewById(R.id.todo_btn_add);


      private Todo_itemViewHolder (View itemView) {
            super(itemView);
            add.setOnClickListener(this);

      }

      @Override
      public void onClick(View view) {
            Intent intent=new Intent(context, Todo_saveActivity.class);
            startActivityForResult(intent,Request_Code);
      }
  }

答案 4 :(得分:0)

首先,定义一个类实现RecyclerView.OnItemTouchListener;

public class ItemListeners implements RecyclerView.OnItemTouchListener {

public interface  OnItemClickListeners {
    void onClick(View view, int postion);
}

private GestureDetector  gestureDetector;
private OnItemClickListeners onItemTouchListener;

public ItemListeners(Context context, OnItemClickListeners onItemTouchListener) {
    gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return true;
    }
    });
    this.onItemTouchListener = onItemTouchListener;
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    View childView = rv.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && onItemTouchListener != null && 
       gestureDetector.onTouchEvent(e)){         onItemTouchListener.onClick(childView,rv.getChildAdapterPosition(childView));
    }
    return false;
}

然后是片段或活动,

 recyclderView.addOnItemTouchListener(new ItemListeners(getBaseContext(), 
    new ItemListeners.OnItemClickListeners() {
        @Override
        public void onClick(View view, int postion) {
            if (petLists.size() > 0){
                Intent intent = new Intent(getBaseContext(), 
                 SomeActivity.class);               
               if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);

                }
            }

        }
    }));