如何在RecyclerView中实现多选? [英] How to implement multi-select in RecyclerView?
问题描述
ListView有一个选择模式,我应该使用RecyclerView来实现相同的方式吗?如何处理onItemCheckedStateChanged? 我检查了此, 但是我不能从中看出很多.任何代码片段或实现相同代码的示例项目都将是不错的选择.预先感谢.
ListView had a multiple choice mode, what should I use for implementing the same using RecyclerView? How to handle onItemCheckedStateChanged? I checked this, But i couldn't make out much out of this. Any code snippet or a sample project implementing the same would be great. Thanks in advance.
推荐答案
我知道回答这个问题有点晚了.而且我不知道它是否满足OP的要求.但这可能会对某人有所帮助.我用一个简单的技巧实现了这个多选RectyclerView.这是我的代码.
I know it's a little bit late to answer this question. And I don't know whether it meets requirements of OP or not. But this may help someone. I implemented this multi-select RectyclerView with a simple trick. Here is my code.
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEE">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="#FFF"
android:clickable="true"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
tools:text="TextView" />
</LinearLayout>
在item_row.xml
android:clickable="true"
中很重要.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Model> mModelList;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new RecyclerViewAdapter(getListData());
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setAdapter(mAdapter);
}
private List<Model> getListData() {
mModelList = new ArrayList<>();
for (int i = 1; i <= 25; i++) {
mModelList.add(new Model("TextView " + i));
}
return mModelList;
}
}
Model.java
public class Model {
private String text;
private boolean isSelected = false;
public Model(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
public boolean isSelected() {
return isSelected;
}
}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private List<Model> mModelList;
public RecyclerViewAdapter(List<Model> modelList) {
mModelList = modelList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final Model model = mModelList.get(position);
holder.textView.setText(model.getText());
holder.view.setBackgroundColor(model.isSelected() ? Color.CYAN : Color.WHITE);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.setSelected(!model.isSelected());
holder.view.setBackgroundColor(model.isSelected() ? Color.CYAN : Color.WHITE);
}
});
}
@Override
public int getItemCount() {
return mModelList == null ? 0 : mModelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private View view;
private TextView textView;
private MyViewHolder(View itemView) {
super(itemView);
view = itemView;
textView = (TextView) itemView.findViewById(R.id.text_view);
}
}
}
它如何工作?
onBindViewHolder()
方法将ArrayList中的数据绑定到View对象.因此,在将数据及时绑定到视图时,它会从ArrayList中获取当前位置为Model model = mModelList.get(position);
的单个对象.现在我们需要检查是否选择了该特定对象.像这样
How does it work?
onBindViewHolder()
method binds the data from ArrayList to View objects. So, on time binding the data to the view it gets the single object from ArrayList that is Model model = mModelList.get(position);
with the current position. Now we need to check whether that particular object is selected or not. Like this,
model.isSelected()
返回true
或false
.如果已经选择了该对象,则需要更改选择的row_item
的背景颜色.为此,这里是代码
which returns either true
or false
. If that object is already selected we need to change the background color of row_item
selected. For this here is the code
holder.view.setBackgroundColor(model.isSelected() ? Color.CYAN : Color.WHITE);
如果已选择,则将背景色更改为cyan
,否则将其更改为white
.
If it's selected change the background color to cyan
else white
.
对于选择,我们需要使用setOnClickListener()
方法. (这里我仅使用TextView
.因此,我在TextView
上执行点击事件).在这里holder.view
表示整个单个item_row
. Onclick将布尔值切换为true
或false
.
For selection we need to use setOnClickListener()
method. (here I am using only a TextView
. So I am performing a click event on TextView
). Here holder.view
means the entire single item_row
. Onclick toggle the boolean values to true
or false
.
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
model.setSelected(!model.isSelected());
holder.view.setBackgroundColor(model.isSelected() ? Color.CYAN : Color.WHITE);
}
});
在托管RecyclerView的活动"或片段"中,您可以像这样获得选定的对象/物品
In your Activity or Fragment which is hosting RecyclerView, you can get the selected objects/items like this
String text = "";
for (Model model : mModelList) {
if (model.isSelected()) {
text += model.getText();
}
}
Log.d("TAG","Output : " + text);
这是输出
限制用户只能选择一项.
Edit 1: Restricting user to select only one item.
private int lastSelectedPosition = -1; // declare this variable
...
// your code
...
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final Model model = mModelList.get(position);
holder.textView.setText(model.getText());
holder.view.setBackgroundColor(model.isSelected() ? Color.CYAN : Color.WHITE);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// check whether you selected an item
if(lastSelectedPosition > 0) {
mModelList.get(lastSelectedPosition).setSelected(false);
}
model.setSelected(!model.isSelected());
holder.view.setBackgroundColor(model.isSelected() ? Color.CYAN : Color.WHITE);
// store last selected item position
lastSelectedPosition = holder.getAdapterPosition();
}
});
}
我希望这会有所帮助.
这篇关于如何在RecyclerView中实现多选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!