更改recyclerview项目视图的背景而不影响其他项目的视图

时间:2016-09-20 08:21:12

标签: android android-viewpager android-recyclerview

我有一个包含20个项目的recyclerview。 recyclerview保存图像的thubnails。单击某个项目时,该图像将在viewpager中显示。

当一次滑动viewpager时,我获取viewpager中当前项目的位置,然后在recyclerview中获取该位置的视图并将其更改为背景,即将thubnail设置为所选的视图< /强>

viewPager.addOnPageChangeListener(this);


  @Override
  public void onPageSelected(int position) {

      recylerViewAdapter.changesBackground(position);
  }

recyclerview adapter changeBackgroundItem()

public void changesBackground(int position) {

    View v = recyclerView.getChildAt(position);
    if(v != null){
      v.setSelected(true);
    }

  }

recyclerview行/项目背景资源

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Color when the row is selected -->
    <item  android:state_selected="true">
        <shape>
            <solid android:color="@android:color/transparent"/>
            <stroke android:color="@color/md_red_A700" android:width="3dp"/>
            <corners android:radius="1dp"/>
            <padding android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"/>
        </shape>
    </item>

    <item android:state_selected="false">
        <shape>
            <solid android:color="@android:color/transparent"/>
            <stroke android:color="@color/md_black_1000" android:width="2dp"/>
            <corners android:radius="1dp"/>
            <padding android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp"/>
        </shape>
    </item>

</selector>

问题在于,不是只选择了一个项目,而是选择了多个项目。例如,当我将位置1的项目背景更改为红色时,位置8,10 e.tc的项目也会被选中。

  • 这是由项目视图的回收引起的吗?

  • 如何避免选择其他项?

1 个答案:

答案 0 :(得分:1)

我终于能够解决问题了

基本上我的问题是,如果我滚动离开RecyclerView中当前选定的项目,由于视图的回收,其他项目的视图也会显示为选中,因为我没有办法取消选中所选项目被滚动了。因此,当您滚动Recyclerview时,最终会选择多个项目。

我最终使用两个接口来完成这项工作。不需要丑陋的循环黑客。

ViewPager.OnPageChangeListenerOnChildAttachStateListener

 public class MyActiviy extends AppCompatActivity implements
    ViewPager.OnPageChangeListener,RecyclerView.OnChildAttachStateChangeListener{

    int currentPosition = 0;
    int previousPosition = 0;
    ViewPager viewpager;
    RecyclerView recylerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    viewpager = (ViewPager) findViewById(R.id.viewpager);
    recylerView = (RecyclerView) findViewById(R.id.recyclerview);

    viewpager.addOnPageChangeListener(this);
    recylerView.addOnChildAttachStateChangeListener(this);
   }

    @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  }

  @Override
  public void onPageSelected(int position) {
    previousItemPos = currentItemPos;
    currentItemPos = position;

    clearSelectionBackground(previousItemPos);
    setSelectionBackground(currentItemPos);
  }

  @Override
  public void onPageScrollStateChanged(int state) {

  }



     @Override
  public void onChildViewAttachedToWindow(View view) {
    //we have overide this method so that selected item
    //can remain selected when one scroll the recyclerview
    int childPosition = recylerView.getChildAdapterPosition(view);
    if (childPosition == currentItemPos) {
       //set selection
      view.setBackgroundColor(getResources()
     .getColor(android.R.color.RED));
    }
  }

  @Override
  public void onChildViewDetachedFromWindow(View view) {
    //remove selection
   view.setBackgroundColor(getResources()
   .getColor(android.R.color.transparent));

  }
  private void setSelectionBackground(int newItem) {
    RecyclerView.ViewHolder holder = recylerView.findViewHolderForPosition(newItem);

    holder.setBackgroundColor(getResources()
   .getColor(android.R.color.RED));
  }

  private void clearSelectionBackground(int oldItem){

    if(oldItem != currentItemPos){
      RecyclerView.ViewHolder oldHolder = recylerView.findViewHolderForPosition(oldItem);

       oldHolder.itemView.setBackgroundColor(getResources()
       .getColor(android.R.color.transparent));

    }
  }
   }