试图在回收视图中保存切换按钮的状态

问题描述:

我有一个回收视图,当用户通过共享首选项存在应用程序时,我想保存它的状态。我的问题,我无法解决它4天,当我小鸡的切换按钮,并退出应用程序<重新打开时,我发现在recycleview所有的Togglebuttons得到检查,不仅是特定的一个,我检查,反之亦然在XML取消选中试图在回收视图中保存切换按钮的状态

切换按钮

<ToggleButton 

       android:gravity="start" 
       android:layout_width="24dp" 
       android:layout_height="24dp" 
       android:background="@drawable/check" 
       android:textOn="" 
       android:textOff="" 
       android:id="@+id/ToggleHeartID" 
       android:paddingLeft="20dp" 
       android:onClick="Toggle" 
       android:layout_margin="20dp" 
       android:foreground="?android:attr/selectableItemBackground" 

       /> 

这是我的适配器类,我在它处理切换按钮:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { 

ItemClickListener mItemClickListener; 
ArrayList<StoryModelM> arrayList = new ArrayList<>(); 
public Context context; 




MyAdapter(ArrayList<StoryModelM> arrayList){ 
    this.arrayList=arrayList; 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false); 

    return new MyViewHolder(view); 

} 

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

    holder.Listindex.setText(arrayList.get(position).getTitleModel()); 
    holder.ListContent.setText(arrayList.get(position).getContentModel()); 

    Context context =holder.itemView.getContext(); 

    SharedPreferences sharedPreferences = context.getSharedPreferences("tgpref1",Context.MODE_PRIVATE); 
    boolean tgpref = sharedPreferences.getBoolean("tgpref",true); 


    if (tgpref){ 
     holder.toggleButton.setChecked(true); 
    }else { 
     holder.toggleButton.setChecked(false); 
    } 


} 

@Override 
public int getItemCount() { 
    return arrayList.size(); 
} 
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
    TextView Listindex; 
    TextView ListContent; 
    ToggleButton toggleButton; 




    public MyViewHolder(final View itemView) { 
     super(itemView); 

     final Context context = itemView.getContext(); 




     Listindex =(TextView) itemView.findViewById(R.id.TextViewTitleID); 
     ListContent = (TextView)itemView.findViewById(R.id.TextViewContentID); 
     toggleButton =(ToggleButton)itemView.findViewById(R.id.ToggleHeartID); 



     itemView.setOnClickListener(this); 
     toggleButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       int position = getAdapterPosition(); 

        if (toggleButton.isChecked()) { 



         Animation plus = AnimationUtils.loadAnimation(v.getContext(), R.anim.pulse); 
         toggleButton.startAnimation(plus); 
         SharedPreferences sharedPreferences = context.getSharedPreferences("tgpref1", Context.MODE_PRIVATE); 
         SharedPreferences.Editor editor = sharedPreferences.edit(); 
         editor.putBoolean("tgpref", true); 

         editor.commit(); 



        } 


        else { 
         SharedPreferences sharedPreferences = itemView.getContext().getSharedPreferences("tgpref1", Context.MODE_PRIVATE); 
         SharedPreferences.Editor editor = sharedPreferences.edit(); 

         editor.putBoolean("tgpref", false); 
         editor.commit(); 



        } 


      } 
     }); 





    } 

    @Override 
    public void onClick(View v) { 
     if (mItemClickListener!=null) 
      mItemClickListener.onItemClick(itemView, getAdapterPosition(), getItemId()); 


     } 




} 

public void setFilter(ArrayList<StoryModelM> newList) { 

    arrayList= new ArrayList<>(); 
    arrayList.addAll(newList); 
    notifyDataSetChanged(); 
} 
public void setmItemClickListener(ItemClickListener itemClickListener){ 
    this.mItemClickListener= itemClickListener; 
} 




public interface ItemClickListener { 
    void onItemClick(View view, int position , long id); 

} 

public interface ToggleInterface { 
    void toggleonClick(View view); 
} 


@Override 
public int getItemViewType(int position) { 
    return position; 
} 

}

是的,因为所有的行具有从SharedPreferences相同的值:boolean tgpref = sharedPreferences.getBoolean("tgpref",true);

如果你想节省每一行的值,你应该保存布尔值与键+位置,如: editor.putBoolean("tgprefAtPosition0", true);

后那么,您可以获得每个位置的布尔值,例如: boolean tgpref = sharedPreferences.getBoolean("tgprefAtPosition"+ position ,true);