我们可以一次将onItemLongClick,OnClick和OnTouch提供给RecyclerView吗?

时间:2017-04-11 09:33:13

标签: android android-recyclerview

我们可以一次给onItemLongClick,OnClick和OnTouch到RecyclerView吗?我给了所有这三个但是onTouchListener只能工作。如果我删除ontouch上的itemclick和longitemclick都是worrking.if我只给onTouch onTouch正在工作剩下的onClick,你可以告诉我任何我必须一次工作这三个。请让我知道。谢谢!!! I saw description here i didn't get solution

这是我的ListAdapter.java

类ListAdapter扩展了RecyclerView.Adapter {

private List<String> mList,mList1,mList2,mList3,mList4,mList5;
private Listener mListener;
Context mcontext;


ListAdapter(List<String> list,List<String> list_1,List<String> list_2,List<String> list_3,List<String> list_4,List<String> list_5,Listener listener,Context context)
{
    this.mList = list;
    this.mList1 = list_1;
    this.mList2 = list_2;
    this.mList3 = list_3;
    this.mList4 = list_4;
    this.mList5 = list_5;
    this.mListener = listener;
    this.mcontext=context;

}

@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(
            parent.getContext()).inflate(R.layout.list_row, parent, false);
    return new ListViewHolder(view);
}

@Override
public void onBindViewHolder(final ListViewHolder holder, final int position)
{



    holder.text.setText("Destination Is:"+mList.get(position));
    holder.text_1.setText("Item Title:"+mList1.get(position));
    holder.text_2.setText("Item Date:"+mList2.get(position));
    holder.text_3.setText("Item Time:"+mList3.get(position));
    holder.text_4.setText("Item Id:"+mList4.get(position));
    holder.text_5.setText("Assigned Person:"+mList5.get(position));

    holder.frameLayout.setTag(position);
           holder.frameLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                            ClipData data = ClipData.newPlainText("", "");
                            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                view.startDragAndDrop(data, shadowBuilder, view, 0);
                            } else {
                                view.startDrag(data, shadowBuilder, view, 0);
                            }
                            return true;
                    }
                    return false;
                }
            });
    holder.frameLayout.setOnDragListener(new DragListener(mListener));
   holder.itemView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Toast.makeText(mcontext, ""+mList.get(position), Toast.LENGTH_SHORT).show();
            mList1.get(position);
            mList2.get(position);
            mList3.get(position);
            mList4.get(position);
            mList5.get(position);
        }
    });
    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            Toast.makeText(mcontext, "You Clicked So Much Time Dude", Toast.LENGTH_SHORT).show();
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                view.startDragAndDrop(data, shadowBuilder, view, 0);
            } else {
                view.startDrag(data, shadowBuilder, view, 0);
            }
            return true;
        }
    });

}

DragListener getDragInstance() {
    if (mListener != null) {
        return new DragListener(mListener);
    } else {
        Log.e("ListAdapter", "Listener wasn't initialized!");
        return null;
    }
}

@Override
public int getItemCount() {
    return mList.size();
}

List<String> getList()
{
    return mList;
}

void updateList(List<String> list) {
    mList = list;
}

}

3 个答案:

答案 0 :(得分:0)

请发一下:

public interface ClickListener {
            void onClick(View view, int position);

            void onLongClick(View view, int position);
        }

        public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

            private GestureDetector gestureDetector;
            private MainActivity.ClickListener clickListener;

            public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final MainActivity.ClickListener clickListener) {
                this.clickListener = clickListener;
                gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        return true;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                        if (child != null && clickListener != null) {
                            clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                        }
                    }
                });
            }

            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

                View child = rv.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                    clickListener.onClick(child, rv.getChildPosition(child));
                }
                return false;
            }

            @Override
            public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        }

最后添加回收者视图项单击侦听器,如下所述。

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {
                Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));

答案 1 :(得分:0)

在容器视图或根视图中使用OnLongClick Listener,并在其子视图上设置单击Listener。

并通过get view v .get id方法处理列表器,以区分它们,如下所示

public void Onclick(View v){

if (v.getId()==(Integer)R.id.container_root){
}

 if (v.getId()==(Integer)R.id.child_element){
}

}

使用这样的东西。这可能是你的解决方案

答案 2 :(得分:0)

我没有尝试过根据我的理论知识编写代码。

创建您自己的OnTouchListener:

public class RecyclerViewOnTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx, ListAdapter listAdapter){
        gestureDetector = new GestureDetector(ctx, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                return false;
            }
        });
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
}

在上面的单击代码onSingleTapUp(...){}中,系统会调用onLongPress(...){},因为长按onTouch(...){}会被调用,因为将会调用触摸private long hourDifference(long millisFirst, long millisSecond) { return TimeUnit.MILLISECONDS.toHours(millisSecond - millisFirst); }

在你的适配器中更改:

holder.frameLayout.setOnTouchListener(new View.OnTouchListener()....}

对此:

holder.frameLayout.setOnTouchListener(new RecyclerViewOnTouchListener(getContext(),this)...}

在适配器内部编写3个公共方法,在点击,长按,触摸时执行某些操作,并在RecyclerViewOnTouchListener类的相应方法中调用它们。

希望这有效!让我知道! :)