GestureDetector + ItemClick RecyclerView无法一起使用

时间:2017-08-31 17:38:05

标签: java android

我已多次寻找,但我认为没有人面临我现在面临的同样问题。我有以下片段:

FRAGMENT XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />


</RelativeLayout>

RecyclerView XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:id="@+id/icon"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textAlignment="center"
    />

</LinearLayout>

FRAGMENT JAVA CODE SNIPPET

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View viewRoot = inflater.inflate(R.layout.fragment_xml, container, false);
    recyclerView = (RecyclerView) viewRoot.findViewById(R.id.recyclerview_id);

    recyclerView.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
        @Override
        public void onSwipeRight() {
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
            ft.replace(R.id.frameLayout, new Fragment2()).commit();
        }
    });

所以,我在上面的代码中做的是添加一个触摸侦听器,所以当用户从右向左滑动时,片段会转到另一个片段(Fragment2)

这完全没问题!但是......我需要在我的recyclerAdapter中添加onClickListener,这样我就可以捕获每个项目的点击监听器。问题就出现了......添加点击监听器时,滑动监听器不起作用。似乎点击的听众覆盖了&#34;我的滑动倾听者。问题是。如何在recyclerview上添加两者并使它们协同工作?

0 个答案:

没有答案