【问题标题】:Why are the Fragments in my ViewPager not receiving touch events?为什么我的 ViewPager 中的 Fragments 没有收到触摸事件?
【发布时间】:2016-01-12 05:24:33
【问题描述】:

我有一个 AppCompatActivity,其布局包含一个 FrameLayout。在那个FrameLayout 中,我放置了一个包含更复杂布局的片段:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/top_toolbar" />

        <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/bottom_toolbar" />

            <com.passionusnetworks.lype.plugins.SlidingTabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
                android:background="#00000000"
                android:elevation="2dp" />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF" />

            <include layout="@layout/navigation_drawer" />

        </android.support.v4.widget.DrawerLayout>

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

然后我将 2 个Fragment 放入ViewPager。使用我的SlidingTabLayout,我可以通过按下这些选项卡在我的 2 个片段之间切换。在这些片段中,有一个RecyclerView。我的问题是似乎没有触摸事件到达我的片段(RecyclerViews 无法滚动,我无法在ViewPager 的片段之间滑动。我试图在所有级别上放置一些OnTouchListener,但我永远无法获取单个MotionEvent

我不知道该怎么做才能找到我的片段中无法获得任何触摸事件的原因。有人有什么建议吗?

【问题讨论】:

  • 试试这个link。它应该工作
  • 我已经尝试过OnTouchListener,就像我说的那样,它没有用,当我点击或滑动时我没有得到MotionEvent。我也尝试了requestDisallowInterceptTouchEvent(true);,但没有成功,但我不确定我是否把它放在了正确的位置。

标签: android android-fragments android-viewpager


【解决方案1】:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.fragment_slide, container, false);
LinearLayout layout =          (LinearLayout)rootView.findViewById(R.id.layout)//      get your root  layout
  layout.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.v(null, "TOUCH EVENT"); // handle your fragment number here
        return false;
    }
});
  return rootView;
}

【讨论】:

  • 我应该尝试在哪个布局上设置OnTouchListener?在我的AppCompatActivityFrameLayout 上,在我的FragmentRelativeLayout 上或在ViewPager 中包含的片段的布局上?
【解决方案2】:

为了获得正确的行为,我最终将CoordinatorLayout 放在DrawerLayout 中,将SlidingTabLayout 更改为TabLayout(并将其移动到@layout/top_toolbarAppBarLayout 中)。我还在ViewPager 中添加了app:layout_behavior="@string/appbar_scrolling_view_behavior"

我的代码基于the great Cheesesquare demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多