2017-10-05 167 views
0

大家好當我添加notifyItemChanged(i)時有一些奇怪的行爲;交換機需要點擊2次申請在視圖中的變化,如果我刪除它工作正常,但沒有執行的動畫行,謝謝大家的幫助:)notifyItemChanged動畫執行多次?

here is the example of the behaviour

適配器文件

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{ 

    public static class PersonViewHolder extends RecyclerView.ViewHolder { 
     CardView cv; 
     TextView personName; 
     TextView personAge; 
     ImageView personPhoto; 
     Switch personSwitch; 
     TextView personInvisible; 

     PersonViewHolder(View itemView) { 
      super(itemView); 
      cv = (CardView)itemView.findViewById(R.id.cv); 
      personName = (TextView)itemView.findViewById(R.id.title); 
      personAge = (TextView)itemView.findViewById(R.id.subtitle); 
      personPhoto = (ImageView)itemView.findViewById(R.id.image); 
      personSwitch = (Switch)itemView.findViewById(R.id.switch1); 
      personInvisible = (TextView)itemView.findViewById(R.id.invisible); 

     } 
    } 
    List<Card> persons; 

    RVAdapter(List<Card> persons){ 
     this.persons = persons; 
    } 
    @Override 
    public int getItemCount() { 
     return persons.size(); 
    } 
    @Override 
    public int getItemViewType(int position) { 
     return position; 
    } 
    @Override 
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View v; 
     Log.d("myTag", "Position " + i); 
     if(i%2 == 0 || true) { 
      v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false); 
     } 
     else { 
      v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview2, viewGroup, false); 
     } 
     PersonViewHolder pvh = new PersonViewHolder(v); 
     return pvh; 
    } 
    @Override 
    public void onBindViewHolder(final PersonViewHolder personViewHolder, final int i) { 
     Log.d("myTag", "Position2 " + i); 
     personViewHolder.personName.setText(persons.get(i).name); 
     personViewHolder.personAge.setText(persons.get(i).age); 
     personViewHolder.personPhoto.setImageResource(persons.get(i).photoId); 
     personViewHolder.personSwitch.setOnCheckedChangeListener(
       new CompoundButton.OnCheckedChangeListener() { 
        @Override 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         if(isChecked) 
         { 
          Log.d("myTag", "ACTIVATED " + i); 

          personViewHolder.personInvisible.setVisibility(View.VISIBLE); 
         } 
         else 
         { 
          personViewHolder.personInvisible.setVisibility(View.GONE); 
          Log.d("myTag", "DISABLE " + i); 

         } 
         Log.d("myTag", "CHANGED " + i); 
         //RVAdapter.this.notifyItemChanged(i); 

        } 
       } 
     ); 
    } 



    @Override 
    public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
     super.onAttachedToRecyclerView(recyclerView); 
    } 
} 

主要活動

public class MainActivity extends AppCompatActivity { 

    private List<Card> cards; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     initializeData(); 

     RecyclerView rv = (RecyclerView)findViewById(R.id.rv); 
     rv.setHasFixedSize(true); 
     LinearLayoutManager llm = new LinearLayoutManager(this); 
     rv.setLayoutManager(llm); 
     RVAdapter adapter = new RVAdapter(cards); 
     rv.setAdapter(adapter); 
    } 

    // This method creates an ArrayList that has three Person objects 
// Checkout the project associated with this tutorial on Github if 
// you want to use the same images. 
    private void initializeData(){ 
     cards = new ArrayList<>(); 
     cards.add(new Card("Emma Wilson", "23 years old", R.mipmap.ic_launcher,false)); 
     cards.add(new Card("Lavery Maiss", "25 years old", R.mipmap.ic_launcher,false)); 
     cards.add(new Card("Lillie Watts", "35 years old", R.mipmap.ic_launcher,false)); 
     cards.add(new Card("ANOTER Watts", "35 years old", R.mipmap.ic_launcher,false)); 

    } 

} 

佈局文件

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 

    > 

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/cv" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     card_view:layout_constraintHorizontal_bias="0.0" 
     card_view:layout_constraintLeft_toLeftOf="parent" 
     card_view:layout_constraintRight_toRightOf="parent" 
     card_view:layout_constraintTop_toTopOf="parent" 
     app:cardUseCompatPadding="true" 
     > 

     <android.support.constraint.ConstraintLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:id="@+id/invisible" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="8dp" 
       android:layout_marginEnd="16dp" 
       android:layout_marginLeft="8dp" 
       android:layout_marginRight="8dp" 
       android:layout_marginTop="32dp" 
       android:text="Invisible TEXT" 
       android:textStyle="bold" 
       android:visibility="gone" 
       card_view:layout_constraintBottom_toBottomOf="parent" 
       card_view:layout_constraintLeft_toLeftOf="parent" 
       card_view:layout_constraintRight_toRightOf="parent" 
       card_view:layout_constraintTop_toBottomOf="@+id/switch1" 

       /> 

      <Switch 
       android:id="@+id/switch1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="8dp" 
       android:layout_marginEnd="8dp" 
       android:layout_marginRight="8dp" 
       android:layout_marginTop="8dp" 
       android:text="Show Text" 
       card_view:layout_constraintBottom_toBottomOf="@+id/image" 
       card_view:layout_constraintRight_toRightOf="parent" 
       card_view:layout_constraintTop_toTopOf="@+id/image" /> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="0dp" 
       android:layout_marginLeft="8dp" 
       android:layout_marginStart="8dp" 
       android:text="TextView" 
       card_view:layout_constraintBottom_toBottomOf="@+id/image" 
       card_view:layout_constraintLeft_toRightOf="@+id/image" /> 

      <TextView 
       android:id="@+id/subtitle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="8dp" 
       android:layout_marginStart="8dp" 
       android:layout_marginTop="8dp" 
       android:text="Title" 
       android:textSize="20sp" 
       android:textStyle="bold" 
       app:layout_constraintTop_toTopOf="parent" 
       card_view:layout_constraintLeft_toRightOf="@+id/image" 


       /> 

      <ImageView 
       android:id="@+id/image" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="8dp" 
       android:layout_marginLeft="8dp" 
       android:layout_marginStart="8dp" 
       android:layout_marginTop="8dp" 
       app:layout_constraintLeft_toLeftOf="parent" 
       app:layout_constraintTop_toTopOf="parent" 
       app:layout_goneMarginBottom="0dp" 
       app:srcCompat="@mipmap/ic_launcher" 
       card_view:layout_constraintBottom_toTopOf="@+id/guideline3" /> 

      <android.support.constraint.Guideline 
       android:id="@+id/guideline3" 
       android:layout_width="wrap_content" 
       android:layout_height="8dp" 
       android:orientation="horizontal" 
       card_view:layout_constraintGuide_begin="57dp" /> 


     </android.support.constraint.ConstraintLayout> 

    </android.support.v7.widget.CardView> 



</android.support.constraint.ConstraintLayout> 

回答

0

您不應該從適配器調用notifyItemChanged。在適配器外部更改某些數據並且當前用戶界面不表示此數據時應調用此方法。在你的情況下,你改變適配器內的數據,所以不需要重畫項目