Hello all当我添加notifyItemChanged(i)时,有一些奇怪的行为;需要单击该开关2次以在视图中应用更改,如果我删除该行它工作正常但没有执行动画,谢谢大家的任何帮助:)
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 :(得分:0)
您不应该从适配器调用notifyItemChanged。在适配器外部更改某些数据并且当前UI不代表此数据时,应调用此方法。在您的情况下,您更改适配器内的数据,因此无需重绘项目