我正在尝试制作一个RecyclerView
,可以拖动它以交换和滑动以使用3种不同的视图进行删除。然后switch-case
对它们进行分类。交换和拖动部分很好。
但是,我想实现add
功能,当我分别单击三个不同的按钮并且View本身添加得很好时,但是,当我向上或向下滚动时,或当我添加了新按钮。
此代码已简化为看起来更简洁,但是我有三种不同的视图,并且将其分类为哪种视图。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements OnItemMoveListener {
private final static String TAG = MyAdapter.class.getSimpleName();
ArrayList<MyItem> items;
private static final int VIEW_MESSAGE = 0;
/**
* A listener for Move & Swipe. It detects dragging from the user.
*/
public MyAdapter(ArrayList<MyItem> items) {
this.items = items;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = null;
switch (viewType) {
default: {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_edit_message, parent, false);
break;
}
}
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case VIEW_MESSAGE: {
MyItem item = items.get(position);
holder.messageTV.setText(item.getMessage());
break;
}
}
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
Log.d(TAG, "fromPosition: " + fromPosition + ", toPosition: " + toPosition);
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(items, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(items, i, i - 1);
}
}
notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onItemDissmiss(int position) {
items.remove(position);
notifyItemRemoved(position);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView messageTV;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
messageTV = itemView.findViewById(R.id.message);
}
}
@Override
public int getItemViewType(int position) {
Log.d(TAG, "getItemViewType() = position: " + position);
switch (items.get(position).getType()) {
default:
return VIEW_MESSAGE;
}
}
public void setItems(ArrayList<MyItem> items) {
Log.d(TAG, items.size() + "");
this.items = items;
notifyDataSetChanged();
}
public ArrayList<MyItem> getItems() {
return items;
}
}
这是单击“按钮”后来自“活动”代码的一部分,它可以运行。
public void addNewMassageView(int type) {
Items = myAdapter.getMessageItems();
Items.add(new MessageItem(type));;
Log.d(TAG, Items.size() + "");
myAdapter.setItems(items);
recycleView.scrollTo(0, recycleView.getBottom());
}
我试图getChildViews并提取像这样的值:
public ArrayList<MyItem> getItemsFromView() {
ArrayList<MyItem> viewItems = new ArrayList<>();
recycleView.postDelayed(() -> {
for (int i = 0; i < items.size(); i++) {
Log.d(TAG, "i: " + i);
final int idx = i;
RecyclerView.ViewHolder holder = recycleView.findViewHolderForAdapterPosition(idx);
switch (items.get(idx).getType()) {
case MESSAGE: {
EditText messageET = holder.itemView.findViewById(R.id.message);
String message = messageET.getText().toString();
viewItems.add(new MyItem(MESSAGE, message));
break;
}
}
}
}, 50);
return viewItems;
}
但是我得到了空引用错误...
出什么问题了?