[英]RecyclerView with different ItemTouchHelper for each item on the list
我正在嘗試為列表中的每個項目實現一個具有不同 ItemTouchHelper 的 Recycleview。
我知道的唯一方法是將 ItemTouchHelper 直接添加到 RecycleView 而不是項目。
我正在嘗試做的示例:
我有一個包含 4 個項目的列表,我可以向左滑動所有項目。
第一項將顯示一個刪除按鈕。
第二項將顯示刪除按鈕和編輯按鈕。
第三項顯示刪除按鈕。
第四個項目顯示復制、刪除和編輯按鈕。
*列表可以有很多項目。
有人知道該怎么做嗎?
所以基本上你的問題是關於如何根據項目類型為每個RecyclerView
項目添加一個唯一的ItemTouchHelper
。
無需深入了解您希望每個項目如何在ItemTouchHelper
滑動操作中有所不同的細節,就像您所說的添加一些按鈕功能,如復制、編輯和刪除。 我將直接指出如何區分不同項目的ItemTouchHelper
滑動。
第 1 步:使用 POJO 字段區分RecyclerView
項目
因此,首先您需要在 POJO(通常是int
或enum
)中創建一個區分不同項目的字段。
第 2 步:實現自定義ItemTouchHelper.SimpleCallback
創建一個自定義ItemTouchHelper.SimpleCallback
class ,它將RecyclerView
項目的列表帶入其構造函數。
接下來,覆蓋由onChildDraw()
在RecyclerView
的onDraw()
回調中調用的ItemTouchHelper
(); 這是在 RecyclerView 繪制其單個項目時調用的正確位置。
因此,在此方法中,您可以實現滑動時每個項目的外觀。 由於它需要一個 ViewHolder 實例,因此您可以使用ViewHolder.getAdapterPosition()
獲取滑動項目 position ,並且從提供的項目列表中,您可以獲得此特定位置的滑動項目。
這是一個簡單的示例,它是 colors 的列表,當您滑動某個項目時,它會反映背景顏色。
這是它的樣子:
POJO
對於上述第 1 步,我將值存儲到colorValue
字段中
class ColorItem {
String colorName;
int colorValue;
public ColorItem(String colorName, int colorValue) {
this.colorName = colorName;
this.colorValue = colorValue;
}
public String getColorName() {
return colorName;
}
public void setColorName(String colorName) {
this.colorName = colorName;
}
public int getColorValue() {
return colorValue;
}
public void setColorValue(int colorValue) {
this.colorValue = colorValue;
}
}
RecyclerView 適配器(沒有花哨的代碼)
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.CustomViewHolder> {
List<ColorItem> mColors;
// Constructor
RecyclerAdapter(List<ColorItem> colors) {
this.mColors = colors;
}
@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View listItem = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new CustomViewHolder(listItem);
}
@Override
public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
holder.tvColorName.setText(mColors.get(position).getColorName());
}
@Override
public int getItemCount() {
return mColors.size();
}
class CustomViewHolder extends RecyclerView.ViewHolder implements {
TextView tvColorName;
CustomViewHolder(@NonNull View listItem) {
super(listItem);
tvColorName = listItem.findViewById(R.id.tvColorName);
}
}
}
自定義 ItemTouchHelper.SimpleCallback
public class ItemSwipeCallback extends ItemTouchHelper.SimpleCallback {
private final List<ColorItem> mColorItems;
private Context mContext;
public interface OnTouchListener {
void onSwiped(RecyclerView.ViewHolder viewHolder, int direction);
}
private OnTouchListener mOnTouchListener;
public ItemSwipeCallback(Context context, List<ColorItem> items, int dragDirs, int swipeDirs, OnTouchListener onTouchListener) {
super(dragDirs, swipeDirs);
mContext = context;
mColorItems = items;
mOnTouchListener = onTouchListener;
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mOnTouchListener.onSwiped(viewHolder, direction);
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
// Getting the swiped item
ColorItem item = mColorItems.get(viewHolder.getAdapterPosition());
// Get the color of the swiped item (the thing that differentiates among items)
ColorDrawable background = new ColorDrawable(mContext.getResources().getColor(item.getColorValue()));
// Changing the color of the background item
View itemView = viewHolder.itemView;
int backgroundCornerOffset = 25; //so mBackground is behind the rounded corners of itemView
if (dX > 0) { // Swiping to the right
background.setBounds(itemView.getLeft(), itemView.getTop(),
itemView.getLeft() + ((int) dX) + backgroundCornerOffset, itemView.getBottom());
} else if (dX < 0) { // Swiping to the left
background.setBounds(itemView.getRight() + ((int) dX) - backgroundCornerOffset,
itemView.getTop(), itemView.getRight(), itemView.getBottom());
} else { // view is unSwiped
background.setBounds(0, 0, 0, 0);
}
background.draw(c);
}
}
活動
public class MainActivity extends AppCompatActivity {
ArrayList<ColorItem> mColors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mColors = new ArrayList<>();
populateColors();
setupRecyclerView();
}
private void setupRecyclerView() {
RecyclerAdapter adapter = new RecyclerAdapter(this, mColors);
RecyclerView recyclerview = findViewById(R.id.recyclerview);
RecyclerView.LayoutManager layoutMgr = new LinearLayoutManager(getApplicationContext());
recyclerview.setLayoutManager(layoutMgr);
recyclerview.setAdapter(adapter);
ItemTouchHelper helper = new ItemTouchHelper(new ItemSwipeCallback(this, mColors,
0, ItemTouchHelper.RIGHT, new ItemSwipeCallback.OnTouchListener() {
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Do something here
}
}));
helper.attachToRecyclerView(recyclerview);
}
private void populateColors() {
mColors.add(new ColorItem("Red", R.color.red));
mColors.add(new ColorItem("White", R.color.white));
mColors.add(new ColorItem("Green", R.color.green));
mColors.add(new ColorItem("Yellow", R.color.yellow));
mColors.add(new ColorItem("Black", R.color.black));
mColors.add(new ColorItem("Red", R.color.red));
mColors.add(new ColorItem("White", R.color.white));
mColors.add(new ColorItem("Green", R.color.green));
mColors.add(new ColorItem("Yellow", R.color.yellow));
mColors.add(new ColorItem("Black", R.color.black));
mColors.add(new ColorItem("Red", R.color.red));
mColors.add(new ColorItem("White", R.color.white));
mColors.add(new ColorItem("Green", R.color.green));
mColors.add(new ColorItem("Yellow", R.color.yellow));
mColors.add(new ColorItem("Black", R.color.black));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.