这是我的代码
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
final Songs songs = arrayList.get(position);
holder.favorite.setChecked(songs.getFavorite()); // this line makes me confusing.
holder.favorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int r = db.update_favorite(b,songs.getTitle(),songs.getArtist());
compoundButton.setChecked(b);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是我是否包含此代码
holder.favorite.setChecked(songs.getFavorite());
到onBindViewHolder,当我更改切换按钮的状态并向下滚动然后向上滚动时,切换按钮的状态将自身更改为原始状态。澄清一下,当切换按钮状态从 false 变为 true 并且用户向下和向上滚动时,切换按钮会自动变为原始状态,即为 false。
即使用户向下滚动,如何使切换按钮保持状态变化?请帮我。