gpt4 book ai didi

java - 将 MotionEvents 从 RecyclerView.OnItemTouchListener 传递到 GestureDetectorCompat

转载 作者:太空狗 更新时间:2023-10-29 15:29:24 26 4
gpt4 key购买 nike

我有一个实现 RecyclerView.OnItemTouchListener 的 fragment 。如何将 仅单击和长按运动事件 从 RecyclerView 传递到 GestureDetectorCompat。也就是说,我只想处理点击和长按,其余事件应该由 RecyclerView 正常处理。我该如何设置?

public class MyFragment extends Fragment implements RecyclerView.OnItemTouchListener,
GestureDetector.OnGestureListener {

protected RecyclerView recyclerView;
protected RecyclerView.Adapter adapter;
protected LinearLayoutManager layoutManager;
private GestureDetectorCompat detector;

public MyFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.myfrag, container, false);

recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);

layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnItemTouchListener(this);

adapter = new MyAdapter(myData));
recyclerView.setAdapter(adapter);
return rootView;
}


@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 onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) {
return false;
}

@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) {

}
}

最佳答案

您必须在 onCreateView() 方法中初始化 GestureDetectorCompat:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.myfrag, container, false);

detector = new GestureDetectorCompat(getActivity(), new RecyclerViewOnGestureListener());

recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);

layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addOnItemTouchListener(this);

adapter = new MyAdapter(myData));
recyclerView.setAdapter(adapter);
return rootView;
}

RecyclerViewOnGestureListener 是您自己的扩展 SimpleOnGestureListener 的内部类(它提供了 OnGestureListener 方法的空实现)

private class RecyclerViewOnGestureListener extends SimpleOnGestureListener {

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
View view = recyclerView.findChildViewUnder(e.getX(), e.getY());
int position = recyclerView.getChildPosition(view);

// handle single tap

return super.onSingleTapConfirmed(e);
}

public void onLongPress(MotionEvent e) {
View view = recyclerView.findChildViewUnder(e.getX(), e.getY());
int position = recyclerView.getChildPosition(view);

// handle long press

super.onLongPress(e);
}
}

现在看行(来自 onCreateView() 方法):

recyclerView.addOnItemTouchListener(this);

在我们的例子中,“this”是 OnItemTouchListener,其中包含我们需要实现的两个方法:

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
detector.onTouchEvent(e);
return false;
}

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

这里解释一下这些方法的含义: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.OnItemTouchListener.html

这就是处理来自 RecyclerView 的单击和长按事件所需的一切。

关于java - 将 MotionEvents 从 RecyclerView.OnItemTouchListener 传递到 GestureDetectorCompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26524003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com