[英]How to move item to the last position when Checkbox is checked? (RecyclerView, ViewHolder)
我嘗試制作一個包含復選框的RecyclerView(如果已經購買了東西)。 我想將項目移動到CheckBox選中更改的最后位置,並將Background設置為綠色。 我試圖從ArrayList中刪除項目,然后再次添加它,但它僅適用於字符串名稱,它不保持狀態(復選框狀態或顏色)。 其他時候它拋出異常:RecyclerView在計算布局或滾動時無法調用此方法。 有人可以告訴我我做錯了嗎?
對不起我的英語。
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
private ArrayList<String> mDataset;
private ArrayList<FinalListItem> finalListItems;
public MyAdapter(ArrayList<String> mDataset) {
this.mDataset = mDataset;
[enter image description here][1]finalListItems = new ArrayList<>();
for (String a : mDataset) {
finalListItems.add(new FinalListItem(a, false));
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.final_list_TextView1.setText(finalListItems.get(position).getName());
if(finalListItems.get(position).isChecked){
holder.linearLayout.setBackgroundColor(Color.rgb(0, 225, 0));
}
else{
holder.linearLayout.setBackgroundColor(Color.rgb(255, 255, 255));
}
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FinalListItem finalItem = new FinalListItem(finalListItems.get(position).getName(),true);
if (isChecked) {
holder.linearLayout.setBackgroundColor(Color.rgb(0, 225, 0));
finalListItems.remove(finalListItems.get(position));
finalListItems.add(finalItem);
notifyDataSetChanged(); //weird behaviour of app
} else {
holder.linearLayout.setBackgroundColor(Color.rgb(255, 255, 255));
}
}
});
}
@Override
public int getItemCount() {
return finalListItems.size();
}
private class FinalListItem {
private String name;
private boolean isChecked;
public FinalListItem(String name, boolean isChecked) {
this.name = name;
this.isChecked = isChecked;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public String getName() {
return name;
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
final public TextView final_list_TextView1;
final public CheckBox checkBox;
final public LinearLayout linearLayout;
public MyViewHolder(View view) {
super(view);
final_list_TextView1 = (TextView) view.findViewById(R.id.final_list_TextView1);
checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout1);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.finallist_list_layout, parent, false);
return new MyViewHolder(view);
}
}
關於代碼,您應該更改的主要內容是僅更改數據,然后讓適配器根據更改的數據顯示列表,這一點很重要。 這意味着,您不應自己更改背景,而應更改FinalListItem的列表並通知適配器。
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.final_list_TextView1.setText(finalListItems.get(position).getName());
// remove the Listener before setting the checked state programmatically
// and set all the attributes (checked state and background color) here
holder.checkBox.setOnCheckedChangeListener(null);
if(finalListItems.get(position).isChecked){
holder.linearLayout.setBackgroundColor(Color.rgb(0, 225, 0));
holder.checkBox.setChecked(true);
}
else{
holder.linearLayout.setBackgroundColor(Color.rgb(255, 255, 255));
holder.checkBox.setChecked(false);
}
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Lint warning : don't use "final int position"
int currentPosition = holder.getAdapterPosition();
if (isChecked) {
FinalListItem finalItemBefore = finalListItems.get(currentPosition);
FinalListItem finalItemAfter = new FinalListItem(finalItemBefore.getName(),true);
finalListItems.remove(finalItemBefore);
finalListItems.add(finalItemAfter);
notifyDataSetChanged();
} else {
finalListItems.get(currentPosition).setChecked(false);
// no need to update the whole list since only one item has changed
notifyItemChanged(currentPosition);
}
}
});
}
另請注意,從getItemViewType()
返回位置不是一個好主意。 通過這樣做,您可以告訴運行時每個View
都是唯一的,並且不可能回收View
,這對RecyclerView
的性能不利。
列表的實現方式,一種View
類型就足夠了,因此您根本不需要重寫該方法,因為這是默認實現:
@Override
public int getItemViewType(int position) {
return 0;
}
@ 0X0nosugar的答案是正確的:),我發現我們也可以使用
notifyItemRemoved(currentposition); //after removing an item
notifyItemInserted(currentposition); //after inserting an item
它為我們的適配器提供了看起來不錯的平滑動畫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.