适用于RecyclerView的Child's Child的Android OnClickListener

时间:2015-07-02 02:30:18

标签: android

我想要做的是为Recyclerview中项目中的ImageView设置onClickListener。我只想点击那个ImageView来做CODE A并点击其他任何地方来运行CODE B.我怎么能这样做?我正在使用:

    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

    LayoutInflater inflater;
    Context context;

    ClickListener clickListener;

    List<PostContent> posts = Collections.emptyList();

    public RecyclerAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setPosts(List<PostContent> posts) {
        this.posts = posts;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.post_row, viewGroup, false);
        RecyclerViewHolder viewHolder = new RecyclerViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder viewHolder, int i) {
        viewHolder.name.setText(posts.get(i).name);
        viewHolder.username.setText("@" + posts.get(i).username);
        viewHolder.time_post.setText(posts.get(i).time_post);
        viewHolder.content.setText(posts.get(i).content);
    }

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

    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface ClickListener {
        public void ItemClicked(View view, int pos);
    }

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

        TextView name;
        TextView username;
        TextView time_post;
        TextView content;
        ImageView profile;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            username = (TextView) itemView.findViewById(R.id.username);
            time_post = (TextView) itemView.findViewById(R.id.time);
            content = (TextView) itemView.findViewById(R.id.content);
            profile = (ImageView) itemView.findViewById(R.id.profile);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (clickListener != null) {
                clickListener.ItemClicked(v, getPosition());
            }
        }

    }

}

我的适配器和视图持有者。

1 个答案:

答案 0 :(得分:3)

好的,这可能不是正确的方法,但这就是我开始工作的方式。

首先在ViewHolder中创建一个点击界面:

private TaskListRecyclerviewClickInterface clickListener;

...

this.clickListener = clickListener;

...

public interface TaskListRecyclerviewClickInterface {
    public void onItemClicked(int position, String tag);
}

然后将onclicklistener添加到您想要的视图中,并在单击这些项目时调用此界面,如下所示:

holder_view.setTag("holder");
holder_view.setOnClickListener(this);
imageView.setTag("ImageView");
imageView.setOnclickListener(this);

onClickListener:

@Override
public void onClick(View v) {
    if (clickListener != null) {
        clickListener.onItemClicked(getPosition(), v.getTag().toString());
    }
}

现在从您的活动中,将此接口传递给构造函数中的Adapter,然后将接口传递给此viewholder,如下所示:

public InboxTaskListAdapter(List<Items> items, InboxTasksViewHolder.TaskListRecyclerviewClickInterface clickListener) {
        this.items = items;
        this.clickListener = clickListener;
    }

在onCreateViewHolder中,您应该将界面传递给视图。

现在,在您的Activity或片段中,只需实现此侦听器并覆盖onItemClicked方法。您现在将获得单击的视图位置和标记。