我有一个适配器(Adapter),RecyclerView和ItemTouchHelper。
我想创建占位符,以便在移动的结果中可以放置项目。
占位符现在是空元素。但我不知道如何直接将元素放在这个占位符上。
我的适配器:
public class DateTimeAdapter extends RecyclerView.Adapter<MainHolder> implements ItemTouchHelperAdapter {
private List<MainItem> values;
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
private Activity context;
private final OnStartDragListener mDragStartListener;
private boolean initEmpty = false;
public DateTimeAdapter(Activity context, List<MainItem> values, OnStartDragListener dragListener) {
this.values = values;
this.context = context;
mDragStartListener = dragListener;
}
@NonNull
@Override
public MainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
switch (viewType){
case (Constants.ITEM_HEADER_TEXT_VIEWTYPE):
view = LayoutInflater.from(context).inflate(R.layout.list_group_item, parent, false);
return new HeaderHolder(view);
case (Constants.ITEM_EVENT_TEXT_VIEWTYPE):
view = LayoutInflater.from(context).inflate(R.layout.item_wo_items, parent, false);
return new ItemHolder(view);
case (Constants.ITEM_PLACEHOLDER_VIEWTYPE):
view = LayoutInflater.from(context).inflate(R.layout.item_placeholder, parent, false);
return new PlaceHolder(view);
default: throw new IllegalArgumentException();
}
}
@Override
public void onBindViewHolder(@NonNull final MainHolder holder, int position) {
holder.setData(values.get(position));
if (holder.getItemViewType() == Constants.ITEM_PLACEHOLDER_VIEWTYPE){
}
}
@Override
public int getItemCount() {
return values.size();
}
@Override
public int getItemViewType(int position) {
return values.get(position).getViewType();
}
protected LocalTime getStartTimebyHeader(int position){
ListIterator<MainItem> listIterator = values.listIterator(position);
while (listIterator.hasPrevious()){
MainItem t = listIterator.previous();
if (t.getHeaderItem() != null){
DateTimeFormatter parse = new DateTimeFormatterBuilder().appendPattern("HH:mm").toFormatter();
LocalTime localTime = LocalTime.parse(t.getHeaderItem().getHeaderText(), parse);
return localTime;
}
}
return null;
}
protected LocalTime getEndTimebyHeader(int position){
ListIterator<MainItem> listIterator = values.listIterator(position);
while (listIterator.hasNext()){
MainItem t = listIterator.next();
if (t.getHeaderItem() != null){
DateTimeFormatter parse = new DateTimeFormatterBuilder().appendPattern("HH:mm").toFormatter();
LocalTime localTime = LocalTime.parse(t.getHeaderItem().getHeaderText(), parse);
return localTime;
}
}
return null;
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(values, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(values, i, i - 1);
}
}
notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onItemDismiss(int position) {
values.remove(position);
notifyItemRemoved(position);
}
}
我的ItemTouchHelper
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, 0) {
@Override
public boolean canDropOver(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder current, @NonNull RecyclerView.ViewHolder target) {
if (target.getAdapterPosition() != 0 && target.getAdapterPosition() != dta.getItemCount()-1)
return true;
return false;
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
Log.d("onMove", Integer.toString(viewHolder.getAdapterPosition()) + " - " + Integer.toString(target.getAdapterPosition()));
dta.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
int dragFlags = viewHolder.getItemViewType() == Constants.ITEM_EVENT_TEXT_VIEWTYPE ? ItemTouchHelper.UP | ItemTouchHelper.DOWN : 0;
int swipeFlags = viewHolder.getItemViewType() == Constants.ITEM_EVENT_TEXT_VIEWTYPE ? ItemTouchHelper.START | ItemTouchHelper.END : 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
showingDatas.get(viewHolder.getAdapterPosition()).getEventItem().setTimeS(dta.getStartTimebyHeader(viewHolder.getAdapterPosition()));
showingDatas.get(viewHolder.getAdapterPosition()).getEventItem().setTimeE(dta.getEndTimebyHeader(viewHolder.getAdapterPosition()));
dta.notifyItemChanged(viewHolder.getAdapterPosition());
super.clearView(recyclerView, viewHolder);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(rvPlan);
我该怎么做?