2017-05-27 75 views
-1

我試圖實現功能,其中如果用戶點擊一個或多個項目RecyclerView這些項目被選中,如果用戶第二次點擊相同的項目/項目時,這些項目被取消選擇。爲了向用戶顯示用戶點擊的項目已被選擇或取消選擇,圖像被顯示或隱藏。Android - RecyclerView項目選擇問題

但是,如果我點擊RecyclerView中的其中一個項目,正在選擇多個項目。

這是到目前爲止我的代碼:

my_list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 


    <TextView 
     android:id="@+id/tvTest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:text="Test" 
     android:textSize="18sp" /> 

    <ImageView 
     android:id="@+id/ivItemIsSelected" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:src="@drawable/done_256" 
     android:visibility="invisible" /> 

</RelativeLayout> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.test.www.recyclerviewadaptertest.MainActivity"> 

<android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/rvTest"/> 

</RelativeLayout> 

MainActivity.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.DefaultItemAnimator; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 

import java.util.ArrayList; 


public class MainActivity extends AppCompatActivity { 

    private RecyclerView rvTest; 
    private RecyclerViewTestAdapter recyclerViewTestAdapter; 
    private ArrayList<TestModel> testModels; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     testModels = new ArrayList<>(); 

     for(int i = 0; i < 30; i++){ 
      TestModel testModel = new TestModel("Test nr." + (i+1)); 
      testModels.add(testModel); 
     } 

     recyclerViewTestAdapter = new RecyclerViewTestAdapter(testModels); 
     rvTest = (RecyclerView)findViewById(R.id.rvTest); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(MainActivity.this); 
     rvTest.setLayoutManager(mLayoutManager); 
     rvTest.setItemAnimator(new DefaultItemAnimator()); 
     rvTest.setAdapter(recyclerViewTestAdapter); 
    } 
} 

RecyclerViewTestAdapter的.java

import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.List; 

public class RecyclerViewTestAdapter extends RecyclerView.Adapter<RecyclerViewTestAdapter.MyViewHolder> { 

    private List<TestModel> testModels; 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     public ImageView ivItemIsSelected; 
     public TextView tvTest; 

     public MyViewHolder(View view) { 
      super(view); 
      ivItemIsSelected = (ImageView) view.findViewById(R.id.ivItemIsSelected); 
      tvTest = (TextView) view.findViewById(R.id.tvTest); 

      view.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if(ivItemIsSelected.getVisibility() == View.INVISIBLE){ 
         ivItemIsSelected.setVisibility(View.VISIBLE); 
        }else if(ivItemIsSelected.getVisibility() == View.VISIBLE){ 
         ivItemIsSelected.setVisibility(View.INVISIBLE); 
        } 
       } 
      }); 
     } 
    } 

    public RecyclerViewTestAdapter(List<TestModel> testModels) { 
     this.testModels = testModels; 
    } 

    @Override 
    public RecyclerViewTestAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.my_list_item, parent, false); 

     return new RecyclerViewTestAdapter.MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(final RecyclerViewTestAdapter.MyViewHolder holder, int position) { 
     TestModel testModel = testModels.get(position); 
     holder.tvTest.setText(testModel.getTestText()); 
    } 

    @Override 
    public int getItemCount() { 
     return testModels.size(); 
    } 
} 

TestModel.java

public class TestModel { 

    private String testText; 

    public TestModel() { 
    } 

    public TestModel(String testText) { 
     this.testText = testText; 
    } 

    public String getTestText() { 
     return testText; 
    } 

    public void setTestText(String testText) { 
     this.testText = testText; 
    } 
} 
+0

嘗試取消選擇ViewHolder構造函數中的項目,也許他們正在回收和他們保持他們的選擇。 –

+0

@NicolasMaltais謝謝你的回答。我試過了,但它不起作用。 – Nikolaj

回答

1

你不應該保持 「選中」 狀態在ViewHolder,因爲它得到回收。保持模型中的「選定」狀態。修改您的TestModel.java: 公共類TestModel {

private String testText; 
public boolean selected = false; 

public TestModel() { 
} 

public TestModel(String testText) { 
    this.testText = testText; 
} 

public String getTestText() { 
    return testText; 
} 

public void setTestText(String testText) { 
    this.testText = testText; 
} 

}

設置爲在onBind模型項目的引用和基於項目的選擇標誌設置可見性:

public class MyViewHolder extends RecyclerView.ViewHolder { 
      public ImageView ivItemIsSelected; 
      public TextView tvTest; 
      private TestModel testModel; 

      public MyViewHolder(View view) { 
       super(view); 
       ivItemIsSelected = (ImageView) view.findViewById(R.id.ivItemIsSelected); 
       tvTest = (TextView) view.findViewById(R.id.tvTest); 

       view.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         testModel.selected = !testModel.selected; 
         ivItemIsSelected.setVisibility(testModel.selected ? View.VISIBLE : View.INVISIBLE); 

        } 
       }); 
      } 
     } 

     public RecyclerViewTestAdapter(List<TestModel> testModels) { 
      this.testModels = testModels; 
     } 

     @Override 
     public RecyclerViewTestAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View itemView = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.my_list_item, parent, false); 

      return new RecyclerViewTestAdapter.MyViewHolder(itemView); 
     } 

     @Override 
     public void onBindViewHolder(final RecyclerViewTestAdapter.MyViewHolder holder, int position) { 
      testModel = testModels.get(position); 
      holder.tvTest.setText(testModel.getTestText()); 
      holder.ivItemIsSelected.setVisibility(testModel.selected ? View.VISIBLE : View.INVISIBLE); 
     } 

     @Override 
     public int getItemCount() { 
      return testModels.size(); 
     } 
    } 

有是實現這一目的的其他方法,但重要的是要記住,每次調用BindViewHolder時,必須更新其中的所有內容以反映指定位置處的項目。希望這可以幫助。

+0

感謝它的工作。 – Nikolaj