简体   繁体   English

回收者视图中的多次事件触发器(onClick)

[英]Multiples event trigger(onClick) inside a recycler view

I have a recyclerView with multiples view, I already settle all diferent viewholder correctly and some viewholder are represent with the same ViewGroup, inside the viewgroup I have a button when this button is clicke it change the value in a textView until now everything work well, the problem is when I trigger the event some of the all the viewgroup in the list of same view group change when I click one button in not order specifically inside the list but is constant in the instance,I already set some breakpoint but anything out of the normal, any ide why something like this is happen? 我有一个带有倍数视图的recyclerView,我已经正确地解决了所有不同的viewholder,并且一些viewholder都用同一个ViewGroup表示,在viewgroup中,当我单击该按钮时,我有一个按钮,它可以更改textView中的值,直到现在一切正常,问题是当我触发事件时,同一视图组列表中的所有视图组中的某些发生变化时,当我未按顺序特别单击列表中的一个按钮但在实例中是恒定的时,我已经设置了一些断点,但任何其他正常情况下,为什么会发生这种情况?

my ViewHolder Hirechary look like this. 我的ViewHolder Hirechary看起来像这样。

public abstract class AbstractViewHolderR extends RecyclerView.ViewHolder{

   public Button btn;
   public TextView view; 

   ...
}

public class ChildVH1 extends AbstractViewHolderR{
  ...
}

...other ViewHolder look the same.

/// the event listener look like this.
public class Click implement View.OnClickListener{
   AbstractViewHolderR object;
   public Click(AbstractViewHolderR o){
       object = o;
   }
   @Override
   public void onClick(View v){
     ////error checking omited
     Integer ii = Integer.Parse(object.view.getText().toString);
     ii++;
     object.view.setText(ii.ToString());
   }

}

Move your TextView and Btn to View holder instead and you should add click listener to ViewHolder itself like this 而是将TextView和Btn移到View持有人,然后应将Click listener添加到ViewHolder本身,如下所示

public abstract class AbstractViewHolderR extends RecyclerView.ViewHolder{

         //UPDATE
   @Override
   public long getItemId(int position) {
       return position;
   }
   ...
}

public class ChildVH1 extends RecyclerView.ViewHolder{
   public Button btn;
   public TextView view; 

   public ChildVH1(View itemView){
      btn.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      //do your stuff
                      //you can use getAdapterPosition() to get current 
                       //position in adapter
                  }
              });
     }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM