【问题标题】:Viewpager: problems with left-to-right swipeViewpager:从左到右滑动的问题
【发布时间】:2017-10-30 19:04:42
【问题描述】:

我在 LinearLayout 中有一个 ViewPager。当我滑动到下一个片段(从右到左)时它可以正常工作,我可以在任何地方触摸屏幕并且它可以工作。但是当我尝试回到上一个片段时,从左向右滑动,它只适用于屏幕左侧的狭窄部分(图片上的黄色区域):

我的 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/palette_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    android:windowSoftInputMode="adjustPan">

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.15"
        android:windowSoftInputMode="adjustPan"
        tools:context="com.internet_of_everything.chineesecards.MainActivity">
    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:layout_weight="0.85"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:visibility="visible"
        android:weightSum="1">

        <ImageButton
            android:id="@+id/button_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:background="?android:attr/windowBackground"
            android:contentDescription="@android:string/untitled"
            android:visibility="visible"
            app:srcCompat="@drawable/button_back" />

        <ImageButton
            android:id="@+id/button_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:adjustViewBounds="false"
            android:background="?android:attr/windowBackground"
            android:contentDescription="@android:string/untitled"
            android:elevation="0dp"
            android:visibility="visible"
            app:srcCompat="@drawable/button_next" />
    </LinearLayout>
</LinearLayout>

更新 在我的片段中,我有 2 个线性布局:使用 EditText 和使用 TextView。同时只能显示 EditText 或 TextView。 而且我有 onTouchListener:当显示 EditText 时,它会隐藏键盘并在用户单击此编辑文本之外时检查 EditText 的内容。 那么我可以使用这个 onTouchListener 并解决从左到右滑动的问题吗?

public class OneCardFragment extends Fragment {

    public OneCardFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
                //hides keyboard
                imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
                //checks edittext content
            }
        });
    }
}

【问题讨论】:

  • 我认为这不是 ViewPager 的直接问题。检查您的第二个片段的布局和单击侦听器,可能是某些视图之前消耗了触摸事件,并防止滑动手势。
  • 你说得对,我的 Fragment 中有 onTouchListener。我用一些关于它的细节更新了我的问题
  • 您是否在方法boolean onTouch(View v, MotionEvent event) 中返回true?如果是,则返回false,其他视图也可以有机会处理该事件。
  • 成功了,谢谢!!我不知道如何将您的评论标记为答案
  • 我会添加一个答案,你可以标记一下。

标签: android android-layout android-fragments android-viewpager swipe-gesture


【解决方案1】:

返回 false 表示其他视图也有机会处理该事件。

public class OneCardFragment extends Fragment {

    public OneCardFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);
                //hides keyboard
                imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
                //checks edittext content
                return false;
            }
        });
    }
}

【讨论】:

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