我在回收器视图中切换按钮我在共享首选项中保存布尔值,以识别是否在每次重新启动应用程序时按下切换按钮现在问题是 当我点击一个1切换按钮并关闭应用程序时,每个切换按钮对于关闭按钮都是相同的事情,切换按钮和回收器视图之间没有位置连接这里是我的代码
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyHolder holder=new MyHolder(v);
SharedPreferences sharedPrefs = c.getSharedPreferences("lol", MODE_PRIVATE);
Boolean a = sharedPrefs.getBoolean("abc" , false);
if(a) {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_light));
holder.fav.setChecked(true);
} else {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
holder.fav.setChecked(false);
}
return holder;
}
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
holder.nameTxt.setText(players[position]);
holder.posTxt.setText(positions[position]);
holder.img.setImageResource(images[position]);
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light));
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
editor.putBoolean("abc", true);
editor.commit();
} else {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
editor.putBoolean("abc", false);
editor.commit();
}
}
});
答案 0 :(得分:0)
您的实施是错误的。
您应该只使用onCreateViewHolder()
来扩充布局。
绑定操作应该在onBindViewHolder()
中。在onBindViewHolder()
答案 1 :(得分:0)
我做了一个快速编辑。请详细检查。我在position
中使用了SharedPreferences
。因此每个toggle button
值保存在不同的密钥中。这可能有用。
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyHolder holder=new MyHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
holder.nameTxt.setText(players[position]);
holder.posTxt.setText(positions[position]);
holder.img.setImageResource(images[position]);
SharedPreferences sharedPrefs = c.getSharedPreferences("lol", MODE_PRIVATE);
Boolean a = sharedPrefs.getBoolean("abc"+position , false);
if (a){
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_light));
holder.fav.setChecked(true);
}else {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
holder.fav.setChecked(false);
}
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light));
editor.putBoolean("abc"+position, true);
editor.commit();
}
else{
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
editor.putBoolean("abc"+position, false);
editor.commit();
}
}
});
答案 2 :(得分:0)
它有效,谢谢你,这是正确的答案
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);
MyHolder holder=new MyHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
holder.nameTxt.setText(players[position]);
holder.posTxt.setText(positions[position]);
holder.img.setImageResource(images[position]);
SharedPreferences sharedPrefs = c.getSharedPreferences("lol", MODE_PRIVATE);
Boolean a = sharedPrefs.getBoolean("abc" + position, false);
if (a){
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_light));
holder.fav.setChecked(true);
}else {
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
holder.fav.setChecked(false);
}
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light));
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
editor.putBoolean("abc" + position, true);
editor.commit();
}
else{
holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off));
SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit();
editor.putBoolean("abc" + position, false);
editor.commit();
}
}
});