我有一个RecyclerView。每行都有一个开关按钮,我点击开关改变它的状态。 当我向下滚动并回来时,recyclerview忽略了我在切换时所做的更改。 所有的Recyclerview都会重置。
我希望在滚动期间对recyclerview进行任何更改。
这是我的适配器
public class LightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<Light> mList;
private LightItemClickListener mListener;
private MainActivity mMainActivity;
public LightAdapter(List<Light> mList, LightItemClickListener mListener, MainActivity mMainActivity) {
this.mList = mList;
this.mListener = mListener;
this.mMainActivity = mMainActivity;
}
@Override
public LightViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
return new LightViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
Light light = mList.get(position);
((LightViewHolder)holder).getmSwitch().setChecked(light.isLightIsActivated());
((LightViewHolder)holder).getmSeekBar().setProgress(light.getLightingValue());
if(light.isLightIsActivated())
{
((LightViewHolder)holder).getmSwitchOnTextView().setVisibility(View.VISIBLE);
((LightViewHolder)holder).getmSwitchOffTextView().setVisibility(View.GONE);
} else
{
((LightViewHolder)holder).getmSwitchOnTextView().setVisibility(View.GONE);
((LightViewHolder)holder).getmSwitchOffTextView().setVisibility(View.VISIBLE);
}
((LightViewHolder)holder).getmLightNameTextView().setText(light.getLightName());
((LightViewHolder)holder).populateItemView(light, mListener,mMainActivity);
}
@Override
public int getItemCount() {
return mList.size();
}
public void add(List<Light> list) {
mList.clear();
mList.addAll(list);
notifyDataSetChanged();
}
}
和我的ViewHolder
public class LightViewHolder extends RecyclerView.ViewHolder {
private Context mContext;
private SeekBar mSeekBar;
private Switch mSwitch;
private TextView mSwitchOffTextView;
private TextView mSwitchOnTextView;
private TextView mLightNameTextView;
public LightViewHolder(View itemView) {
super(itemView);
mSeekBar = itemView.findViewById(R.id.seekBar1);
mSwitch = itemView.findViewById(R.id.switch1);
mSwitchOffTextView = itemView.findViewById(R.id.switch_off_textview);
mSwitchOnTextView = itemView.findViewById(R.id.switch_on_textview);
mLightNameTextView = itemView.findViewById(R.id.light_name_text_view);
}
public void populateItemView(final Light lightData, final LightItemClickListener onLightClickListener, MainActivity mMainActivity) {
mContext =mMainActivity.getApplicationContext();
switchClick(lightData,onLightClickListener);
seekBarChangeValue(lightData,onLightClickListener);
}
public SeekBar getmSeekBar() {
return mSeekBar;
}
public TextView getmSwitchOnTextView() {
return mSwitchOnTextView;
}
public TextView getmSwitchOffTextView() {
return mSwitchOffTextView;
}
public TextView getmLightNameTextView() {
return mLightNameTextView;
}
public Switch getmSwitch() {
return mSwitch;
}
public void switchClick(final Light light, final LightItemClickListener onLightClickListener) {
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(mSwitch.isChecked())
{
mSwitchOffTextView.setVisibility(View.GONE);
mSwitchOnTextView.setVisibility(View.VISIBLE);
} else {
mSwitchOnTextView.setVisibility(View.GONE);
mSwitchOffTextView.setVisibility(View.VISIBLE);
}
onLightClickListener.onSwitchClickListener(light);
}
});
}
private void seekBarChangeValue(final Light light, final LightItemClickListener onLightClickListener) {
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.d("test","changed");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d("test","start");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
onLightClickListener.onSeekBarStopListener(light);
}
});
}
}
提前致谢。
答案 0 :(得分:1)
将此添加到recycleview适配器,你应该没问题
@Override
public int getItemViewType(int position) {
return position;
}
答案 1 :(得分:0)
这是因为在您的checkedChange侦听器和搜索栏侦听器上,您没有改变Light对象的状态。为此,当您的视图被回收时,它们会获得旧数据,因此您会遇到这种奇怪的行为 你应该写
light.setLightIsActivated(mSwitch.isChecked())
答案 2 :(得分:0)
没有任何作品按预期工作,直到你制作一个记住更改项目的列表。
我可能稍后发布一个例子。但是想想你是否可以拥有将点击项目存储为是或否的数据列表。然后Recyclerview将从列表中获取项目并记住位置。