RecyclerView 设置默认选中项,以View的形式保存,为什么在滑动过程中selectedView 会变化?
((MightyViewHolder) viewHolder).setText(R.id.tv_Category, mDatas.get(position));
TextView textView = ((MightyViewHolder) viewHolder).getView(R.id.tv_Category);
Log.d(TAG, "onBindViewHolder: " + textView.getText());
textView.setTag(position);
// 获取选中的资源资源文件
Drawable selectDrawable = getContext().getResources().getDrawable(R.drawable.home_page_white_point);
selectDrawable.setBounds(0, 0,
selectDrawable.getMinimumWidth(), selectDrawable.getMinimumHeight());
// 获取未选中的资源文件
Drawable unSelectedDrawable = getContext().getResources().getDrawable(R.drawable.home_page_jdred_point);
unSelectedDrawable.setBounds(0, 0,
unSelectedDrawable.getMinimumWidth(), unSelectedDrawable.getMinimumHeight());
if (selectPosition == -1) {
Log.d(TAG, "transform: 此时selectedView为null,position:" + position + " text为:" + textView.getText());
// this.selectedView = textView;
this.selectPosition = position;
textView.setCompoundDrawables(null, null, null, selectDrawable);
return;
}
Log.d(TAG, "transform: selectedPosition: " + selectPosition + "------position: " + position);
if (position == selectPosition) {
textView.setCompoundDrawables(null, null, null, selectDrawable);
return;
}
w我使用选中项 的位置的形式,就不会变化,注释掉的是使用SelectedView’的形势,
对于状态的,处理了if,还要处理else,否则就是复用的状态。因为RecyclerView的Item是复用的。