以下是一个示例解决方案,展示了在Android中使用多个ViewHolder的RecyclerView,以及拖放和持久项目位置的功能。
首先,我们需要在build.gradle文件中添加RecyclerView的依赖项:
implementation 'androidx.recyclerview:recyclerview:1.1.0'
接下来,我们需要创建两个ViewHolder类:HeaderViewHolder和ItemViewHolder。HeaderViewHolder用于显示标题,ItemViewHolder用于显示项目。
public class HeaderViewHolder extends RecyclerView.ViewHolder {
TextView titleTextView;
public HeaderViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.titleTextView);
}
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView itemTextView;
public ItemViewHolder(View itemView) {
super(itemView);
itemTextView = itemView.findViewById(R.id.itemTextView);
}
}
然后,我们需要创建一个RecyclerView.Adapter类来管理RecyclerView中的项目。在该类中,我们需要实现创建不同类型的ViewHolder的逻辑,并处理拖放和持久项目位置的逻辑。
public class MyAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_HEADER = 0;
private static final int VIEW_TYPE_ITEM = 1;
private List headerList;
private List itemList;
public MyAdapter(List headerList, List itemList) {
this.headerList = headerList;
this.itemList = itemList;
}
@Override
public int getItemViewType(int position) {
if (position < headerList.size()) {
return VIEW_TYPE_HEADER;
} else {
return VIEW_TYPE_ITEM;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == VIEW_TYPE_HEADER) {
View view = inflater.inflate(R.layout.header_layout, parent, false);
return new HeaderViewHolder(view);
} else if (viewType == VIEW_TYPE_ITEM) {
View view = inflater.inflate(R.layout.item_layout, parent, false);
return new ItemViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HeaderViewHolder) {
HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
headerViewHolder.titleTextView.setText(headerList.get(position));
} else if (holder instanceof ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.itemTextView.setText(itemList.get(position - headerList.size()));
}
}
@Override
public int getItemCount() {
return headerList.size() + itemList.size();
}
public void onItemMove(int fromPosition, int toPosition) {
if (fromPosition < headerList.size() || toPosition < headerList.size()) {
return; // 不允许移动标题行
}
String item = itemList.remove(fromPosition - headerList.size());
itemList.add(toPosition - headerList.size(), item);
notifyItemMoved(fromPosition, toPosition);
}
public void onItemSwiped(int position) {
if (position < headerList.size()) {
return; // 不允许滑动删除标题行
}
itemList.remove(position - headerList.size());
notifyItemRemoved(position);
}
}
最后,在Activity或Fragment中,我们需要实例化RecyclerView,并在需要的时候更新adapter的数据集。
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List headerList = new ArrayList<>();
headerList.add("Header 1");
headerList.add("Header 2");
List itemList = new ArrayList<>();
itemList.add("Item 1");
itemList.add("Item 2");
itemList.add("Item 3");
itemList.add("Item 4");
adapter = new MyAdapter(headerList, itemList);
recyclerView.setAdapter(adapter);
ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(adapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(recyclerView);
}
}
在这个示例中,我们使用了ItemTouchHelper来处理拖放和滑动删除的逻辑。我们需要创建一个ItemTouchHelper.Callback类来处理这些操作。
下一篇:android多线程操作数据库