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

时间:2018-02-28 10:19:03

标签: android android-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

3 个答案:

答案 0 :(得分:0)

覆盖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);
                }
        }

        });

    }

答案 1 :(得分:0)

尝试在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);
        }
    }

});

}

答案 2 :(得分:0)

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

  

public void onBindViewHolder(MemberViewHolder holder,int position ){

     

}