简体   繁体   English

如何像Instagram一样在Drag / Touch上突出显示Recycler视图项?

[英]How to Highlight the Recycler view Item on Drag/Touch like Instagram?

I am trying to highlight the item of my horizontal RecyclerView on Drag/Touch.Like in Instagram they implemented this for selecting thumbnail while uploading the video. 我试图在Drag / Touch上突出显示我的水平RecyclerView的项目。就像Instagram一样,他们实现了在上传视频时选择缩略图的功能。 Please suggest if you know how to do this. 如果您知道该怎么做,请提出建议。

在此处输入图片说明

You can make you custom ItemTouchHelperViewHolder Class for Item selected and clear for drag and drop: 您可以为选定的项目自定义ItemTouchHelperViewHolder类,并清除拖放操作:

public interface ItemTouchHelperViewHolder {
    void onItemSelected(int adapterPosition);
    void onItemClear(int adapterPosition, int position);
}

This is CustomSimpleTouchListner Class: 这是CustomSimpleTouchListner类:

@Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        // We only want the active item to change
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            if (viewHolder instanceof ItemTouchHelperViewHolder) {
                // Let the view holder know that this item is being moved or dragged
                ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
                itemViewHolder.onItemSelected();
            }
        }

        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);

        viewHolder.itemView.setAlpha(ALPHA_FULL);

        if (viewHolder instanceof ItemTouchHelperViewHolder) {
            // Tell the view holder it's time to restore the idle state
            ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
            itemViewHolder.onItemClear(fromPos, toPos);
        }
    }

Extend this class in your Recycleview ViewHolder 在您的Recycleview ViewHolder中扩展此类

public class ViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
         }

        @Override
        public void onItemSelected(int adapterPosition) {
            itemView.setBackgroundColor(activity.getResources().getColor(R.color.presented_color));
        }

        @Override
        public void onItemClear(int adapterPosition, int position) {
            itemView.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
        }
    }

I haven't gone to Instagram to see what you really want, but it seems like you should highlight the specific cell using the setOnTouchListener. 我还没有去Instagram查看您真正想要的东西,但是似乎您应该使用setOnTouchListener突出显示特定的单元格。 You'll get notifications of touch events for each cell. 您将收到每个单元的触摸事件的通知。 check out the link below for an example. 请查看下面的链接以获取示例。

how to highlight the selected Item of Recycler View? 如何突出显示“回收者视图”中的选定项目?

https://stackoverflow.com/a/35360331/5098486 https://stackoverflow.com/a/35360331/5098486

This answer makes it really easy to modify the selected item in your recyclerView. 通过此答案,可以很轻松地在recyclerView中修改所选项目。

@Override

public void onBindViewHolder(final ViewHolder holder, final int position) {

    Item item = items.get(position);

    if(selected_position == position){
        // Here I am just highlighting the background
        holder.itemView.setBackgroundColor(Color.GREEN);
    }else{
        holder.itemView.setBackgroundColor(Color.TRANSPARENT);
    }

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Updating old as well as new positions
            notifyItemChanged(selected_position);
            selected_position = position;
            notifyItemChanged(selected_position);

            // Do your another stuff for your onClick
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在“回收者”视图中突出显示选定的项目 - How to Highlight the selected item in Recycler view 如何突出显示所选的Recycler View项目? - how to highlight the selected Item of Recycler View? 如何通过位置android在回收器视图中突出显示项目 - How to highlight a item in recycler view by position android 回收者视图的项目触摸侦听器 - Item touch listener for recycler view 将触摸项目助手附加和分离到回收站视图 - Attach and detach touch item helper to a recycler view 如何在 RecyclerView 中选择和取消选择一个项目? 如何仅在回收站视图中突出显示所选项目? - how to select and de-select an item in RecyclerView? How to Highlight selected item only in recycler view? 如何在触摸时突出显示ListView项? - How to highlight ListView item on touch? 如何突出显示“回收者视图”的单行或项目,以及将突出显示的行滚动到屏幕顶部 - How to Highlight Single Row or Item of Recycler View and Scroll the Highlighted Row to Top of screen 从回收器视图中分离项目触摸助手回调 - Detaching item touch helper callback from recycler view 高级回收器视图-处理项目单击和长按拖动操作 - Advanced Recycler View - Handling on item click and long click drag operations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM