【问题标题】:RecyclerView OnItemclick not working properlyRecyclerView OnItemclick 无法正常工作
【发布时间】:2016-11-09 17:41:54
【问题描述】:

我有一个回收器视图,我正在其中实现 OnClicklistener。基本上,我有一个名为 Actressadapter 的适配器类和一个 Viewholder 类 MyViewHolder。我在 Viewholder 类中实现 OnClickListener 以通过 Intent 方法启动另一个活动。我的基本数据在一个名为女演员的类中,该类具有三个变量 name、country(both String) 和一个 Id(UUID)。 我正在向我的适配器提供女演员的这些数据。下一个活动是只显示它检索到的女演员的名字,如 abc、def 等。问题是点击后只显示一位女演员的名字。例如,如果它显示 abc,那么每次点击它都会显示 abc。不知道为什么会这样,因为根据代码我将女演员名作为额外的传递。

public class actressadapter extends RecyclerView.Adapter <actressadapter.MyViewHolder> {
private List<Actress>al;
private  Actress actress,mActress;
public static final String str="shivam.panwar.actressdetails.actressadapter.str";


public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView naam, desh;

    public MyViewHolder(View view) {
        super(view);

        view.setOnClickListener(this);
        naam = (TextView) view.findViewById(R.id.name);
        desh= (TextView) view.findViewById(R.id.country);


    }
    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
    Intent intent=new Intent(v.getContext(),Actressview.class);

        intent.putExtra(str,actress.getName());
        v.getContext().startActivity(intent);
    }

    public void bindactress(Actress mActress) {
        naam.setText(mActress.getName());
        desh.setText(mActress.getCountry());
    }
}


public actressadapter(List<Actress> al) {
    this.al = al;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.actress_list_row, parent, false);


    return new MyViewHolder(itemView);


}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {


    actress =al.get(position);


    holder.bindactress(actress);
}



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

}

如果需要有关代码的任何进一步帮助,请发表评论。

【问题讨论】:

    标签: android android-intent android-recyclerview onclicklistener


    【解决方案1】:

    试试这个方法希望它有效...

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        actress =al.get(position);
        holder.bindactress(actress);
        holder.naam.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,holder.naam.getText().toString(), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(context,Actressview.class);
                        intent.putExtra(str,holder.naam.getText().toString());
                        context.startActivity(intent);
                    }
                });
    
    }
    

    【讨论】:

    • 可以通过将变量 actress 声明为本地 final 并在整个方法中使用相同的变量而不是查询 getText 并转换为字符串等来进一步优化。
    【解决方案2】:

    使用适配器类的getLayoutPosition()方法获取选中项(位置)的最佳方式。

    您必须更新代码以获得更好的结果。用它替换你现有的代码。

    public class actressadapter extends RecyclerView.Adapter <actressadapter.MyViewHolder> {
    private List<Actress>al;
    private  Actress actress,mActress;
    private int itemPosition;  // change
    public static final String str="shivam.panwar.actressdetails.actressadapter.str";
    
    
        public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            public TextView naam, desh;
    
            public MyViewHolder(View view) {
                super(view);
    
                view.setOnClickListener(this);
                naam = (TextView) view.findViewById(R.id.name);
                desh= (TextView) view.findViewById(R.id.country);
    
    
            }
            @Override
            public void onClick(View v) {
                itemPosition = getLayoutPosition();   // change
                Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(v.getContext(),Actressview.class);
    
                intent.putExtra(str,al.get(itemPosition).getName());  // change
                v.getContext().startActivity(intent);
            }
    
            public void bindactress(Actress mActress) {
                naam.setText(mActress.getName());
                desh.setText(mActress.getCountry());
            }
        }
    
    
        public actressadapter(List<Actress> al) {
            this.al = al;
        }
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.actress_list_row, parent, false);
    
    
            return new MyViewHolder(itemView);
    
    
        }
    
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
    
    
            actress =al.get(position);
    
    
            holder.bindactress(actress);
        }
    
    
    
        @Override
        public int getItemCount() {
            return al.size();
        }
    }
    

    希望它会起作用..

    【讨论】:

      【解决方案3】:

      您的问题是您将actress 设置为 Adapte 的变量,它应该是 Holder。

      修复它:

      删除该行:

       private  Actress actress,mActress;
      

      将onBindViewHolder 方法更改为:

      holder.bindactress(al.get(position))
      

      并用那个替换你的持有者类:

      // holder HAVE TO be static. It's WRONG not doing it static
      public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
          TextView naam, desh;
          Actress actress;
      
          public MyViewHolder(View view) {
              super(view);
              view.setOnClickListener(this);
              naam = (TextView) view.findViewById(R.id.name);
              desh= (TextView) view.findViewById(R.id.country);
      
          }
      
          @Override
          public void onClick(View v) {
              Toast.makeText(v.getContext(),"Clicked",Toast.LENGTH_SHORT).show();
              Intent intent=new Intent(v.getContext(),Actressview.class);
              intent.putExtra(str,actress.getName());
              v.getContext().startActivity(intent);
          }
      
          public void bindactress(Actress mActress) {
              this.actress = mActress;
              naam.setText(mActress.getName());
              desh.setText(mActress.getCountry());
          }
      }
      

      【讨论】:

      • 它工作正常,但我们无法删除私有女演员女演员行,因为在 onBindViewHolder 中使用了女演员,并且如果您将查看器声明为静态,这本身意味着您无法访问女演员适配器类。所以将不得不为viewhloder创建女演员女演员。
      • 我编辑了答案。将onBindViewHolder 方法更改为holder.bindactress(al.get(position))。如果您从我的回答中仔细阅读 ViewHolder 类,您会发现每个视图持有者都包含它自己的actress。这就是让它正常工作的原因。
      猜你喜欢
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多