我試圖實現功能,其中如果用戶點擊一個或多個項目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;
}
}
嘗試取消選擇ViewHolder構造函數中的項目,也許他們正在回收和他們保持他們的選擇。 –
@NicolasMaltais謝謝你的回答。我試過了,但它不起作用。 – Nikolaj