本文介绍了在RecyclerView中切换按钮位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在回收者视图中有切换按钮,我将布尔值保存在sharedpreference中,以识别是否在每次重新启动应用程序时按下切换按钮,现在的问题是当我单击一个切换按钮并关闭应用程序时,每个切换按钮都将关闭按钮置于同一位置,切换按钮和回收站视图之间没有位置连接,这是我的代码
I have toggle buttons inside recycler view i'm saving the boolean value in sharedpreference to recognize if the toggle button is pressed or not on every restart of application now the problem iswhen i click on a 1 toggle button and close the application every toggle button get on same thing for off button there is no position connection between the toggle button and recycler view here is my code
@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();
}
}
});
推荐答案
它奏效了,谢谢,这是正确的答案
it worked thank you all this is the right answer
@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();
}
}
});
这篇关于在RecyclerView中切换按钮位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!