如何在Android上创建半可拖动的recyclerview?

时间:2019-01-08 17:01:19

标签: android android-layout android-recyclerview

我的问题很简单,但是在Android上实现起来确实很棘手。我想知道如何只拖动标题下方的“可拖动部分”。

recyclerview table with categories

我已经遵循this tutorial来实现拖动功能。但是如何在拖动时区分Header ViewHolder和Item ViewHolder?

适配器类在下面列出

public class ColorRecyclerViewAdapter extends
        RecyclerView.Adapter<ColorRecyclerViewAdapter.ItemViewHolderSelector> implements TouchCallbackHelperAdapter {

    private Context context;
    private ArrayList<String> mItems;
    private final HelperOnStartDragListener mDragStartListener;
    private ItemViewHolderSelector oldHolder;

    public ColorRecyclerViewAdapter(Context context, ArrayList<String> data, HelperOnStartDragListener dragStartListener) {
        this.context = context;
        this.mItems = data;
        this.mDragStartListener = dragStartListener;
    }

    @Override
    public void onItemDismiss(int position) {
        mItems.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public boolean onItemMove(final int fromPosition, final int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(mItems, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(mItems, i, i - 1);
            }
        }
        mDragStartListener.onFinishDrag(null);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    public ArrayList<String> getData() {
        return mItems;
    }

    public void addItem(String item) {
        mItems.add(item);
        notifyItemInserted(mItems.size() - 1);
    }

    @Override
    public ItemViewHolderSelector onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(context).inflate(R.layout.color_recycler_view_item_drag_mode, parent, false);
        return new ItemViewHolderSelector(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ItemViewHolderSelector holder, final int position) {
        final String item = mItems.get(position);
        holder.name.setText(item);
        holder.drag.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mDragStartListener.onStartDrag(holder);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mItems != null ? mItems.size() : 0;
    }

    class ItemViewHolderSelector extends RecyclerView.ViewHolder implements HelperViewHolderSelector {

        private TextView name;
        private ImageView drag;
        private View itemView;

        ItemViewHolderSelector(View itemView) {
            super(itemView);
            this.itemView = itemView;
            drag = itemView.findViewById(R.id.color_recycler_view_drag);
            name = itemView.findViewById(R.id.color_recycler_view_name);
        }

        @Override
        public void onItemSelected() {
            itemView.setBackgroundColor(Color.LTGRAY);
        }

        @Override
        public void onItemClear() {
            itemView.setBackgroundColor(0);
        }
    }
}

编辑:

基于@fernandospr答案,我用最终代码创建了一个存储库。您可以access it here

1 个答案:

答案 0 :(得分:2)

RecyclerView仅显示您在onCreateViewHolder上创建的视图。

根据您的位置,您需要创建不同的视图。这意味着当位置对应于标题时,您将创建一个HeaderViewHolder,而当位置对应于一个项目时,您将创建一个ItemViewHolder

因此,您必须将ColorRecyclerViewAdapter重构为:

private static final int HEADER_VIEW = 0;
private static final int ITEM_VIEW = 1;

@Override
public RecyclerView.ViewHolderSelector onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if (viewType == ITEM_VIEW) {
       ...
       return new ItemViewHolderSelector(view);
    } else {
       ...
       return new HeaderViewHolderSelector(view);
    }
}

@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
   if (ITEM_VIEW == holder.getItemViewType()) {
      ...
   } else {
      ...
   }
}

@Override
public int getItemCount() {
   return itemsCount + headersCount;
}

onBindViewHolder内,您只能通过设置TouchListener来允许拖动项目。

此外,在onMoveonSwiped方法中,您应该使用该位置来知道它是标题还是项目,并允许执行:

@Override
public void onItemDismiss(int position) {
   if (isItemPosition(position)) { 
      ...
   }
}

@Override
public boolean onItemMove(final int fromPosition, final int toPosition) {
   if (isItemPosition(position)) { 
      ...
   }
}

isItemPosition()是一种您需要编写的方法,具体取决于适配器保存项目和标头数据的方式。