RecyclerView onClick到每个位置的不同活动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView onClick到每个位置的不同活动相关的知识,希望对你有一定的参考价值。

我是新手,想问一下关于onClick RecyclerView的问题。

我从http://wimsonevel.blogspot.co.id/2016/03/tutorial-android-recyclerview-and.html找到了这个脚本

我已通过此代码成功创建了另一个活动的意图:

    @Override

    public void onBindViewHolder(MemberViewHolder holder, int position) {
    final Member member = memberList.get(position);

    holder.memberThumb.setImageResource(member.getThumb());
    holder.memberName.setText(member.getName());
    holder.memberTeam.setText(member.getTeam());
    holder.memberCard.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent go;

                go = new Intent(view.getContext(),Main2Activity.class);
                view.getContext().startActivity(go);

        }

    });

}

但我想为每个CardView位置添加意图。如果位置0,转到Main0Activity,如果位置1,转到Main1Activity等。

我的代码(不工作):

    @Override

    public void onBindViewHolder(MemberViewHolder holder, int position) {
    final Member member = memberList.get(position);

    holder.memberThumb.setImageResource(member.getThumb());
    holder.memberName.setText(member.getName());
    holder.memberTeam.setText(member.getTeam());
    holder.memberCard.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onItemClick(View<?> parent, View view, int position, long id) {
            Intent go;

            if (position==0){
                go = new Intent(view.getContext(),Main0Activity.class);
                view.getContext().startActivity(go);
            }
            else if (position==1){
                go = new Intent(view.getContext(),Main1Activity.class);
                view.getContext().startActivity(go);
            }
            else if (position==2){
                go = new Intent(view.getContext(),Main2Activity.class);
                view.getContext().startActivity(go);
            }
            else if (position==3){
                go = new Intent(view.getContext(),Main3Activity.class);
                view.getContext().startActivity(go);
            }
            else if (position==4){
                go = new Intent(view.getContext(),Main4Activity.class);
                view.getContext().startActivity(go);
            }
        }

    });

}

错误:

Error:(74, 73) error: <anonymous com.example.wim.androidrecyclerview.adapter.MemberListAdapter$1> is not abstract and does not override abstract method onClick(View) in OnClickListener

Error:(76, 13) error: method does not override or implement a method from a supertype

如何解决这个问题?

谢谢

对不起,我的英语不好

完整代码:https://gist.github.com/anonymous/e15b222bfc20ba21eaf5c2c8d566166b

答案

覆盖onClick方法。试试这个,

 @Override

    public void onBindViewHolder(MemberViewHolder holder, final int position) {
        final Member member = memberList.get(position);

        holder.memberThumb.setImageResource(member.getThumb());
        holder.memberName.setText(member.getName());
        holder.memberTeam.setText(member.getTeam());
        holder.memberCard.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent go;

                if (position==0){
                    go = new Intent(view.getContext(),Main0Activity.class);
                    view.getContext().startActivity(go);
                }
                else if (position==1){
                    go = new Intent(view.getContext(),Main1Activity.class);
                    view.getContext().startActivity(go);
                }
                else if (position==2){
                    go = new Intent(view.getContext(),Main2Activity.class);
                    view.getContext().startActivity(go);
                }
                else if (position==3){
                    go = new Intent(view.getContext(),Main3Activity.class);
                    view.getContext().startActivity(go);
                }
                else if (position==4){
                    go = new Intent(view.getContext(),Main4Activity.class);
                    view.getContext().startActivity(go);
                }
        }

        });

    }
另一答案

您应该在第一个示例中覆盖onClick。如果你需要位置onBindViewHolder提供给你:

public void onBindViewHolder(MemberViewHolder holder,int position){

}

另一答案

尝试在itemview上添加点击监听器,就像这个holder.itemview.setOnClickListener一样

@Override

public void onBindViewHolder(MemberViewHolder holder, int position) {
final Member member = memberList.get(position);

holder.memberThumb.setImageResource(member.getThumb());
holder.memberName.setText(member.getName());
holder.memberTeam.setText(member.getTeam());
holder.itemview.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
    Intent go;

        if (position==0){
            go = new Intent(view.getContext(),Main0Activity.class);
            view.getContext().startActivity(go);
        }
        else if (position==1){
            go = new Intent(view.getContext(),Main1Activity.class);
            view.getContext().startActivity(go);
        }
        else if (position==2){
            go = new Intent(view.getContext(),Main2Activity.class);
            view.getContext().startActivity(go);
        }
        else if (position==3){
            go = new Intent(view.getContext(),Main3Activity.class);
            view.getContext().startActivity(go);
        }
        else if (position==4){
            go = new Intent(view.getContext(),Main4Activity.class);
            view.getContext().startActivity(go);
        }
    }

});

}

以上是关于RecyclerView onClick到每个位置的不同活动的主要内容,如果未能解决你的问题,请参考以下文章

为啥 RecyclerView 项目在 GridLayoutManager 中随机移动位置?

recyclerview怎么监听滑动事件

如何指向特定的 RecyclerView onClick 视图(Android Java)?

如何从 fire-base 检索数据到新活动中单击 RecyclerView

从recyclerview中删除项目(viewholder具有onClick但适配器具有数据集)

带删除选项的全视图图像显示