2
votes

I have a ViewPager inside LinearLayout. It works correct when I swipe to the next fragment (from right to left), I can touch screen on any place and it works. But when I try to back to previous fragment, swipping from left to right, it works only on a narrow part on the left side of screen (yellow area on picture):

enter image description here

My 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>

UPDATE In my fragment I have 2 linear layouts: with EditText and with TextView. On the same time only EditText or TextView can be shown. And I have onTouchListener: when EditText is shown it hides keyboard and checks the content of EditText when user clicks outside this edittext. So am I able to use this onTouchListener and solve problem with left-to-right swipe?

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
            }
        });
    }
}
1
I think its not a ViewPager problem directly. Check your second fragment's layout and click listeners, may be some views consume the touch event before, and prevent swipe gestures.Oğuzhan Döngül
You are right, I have onTouchListener in my Fragment. I updated my question with some details about itOlga
Are you returning true inside of the method boolean onTouch(View v, MotionEvent event)? If yes return false that the other view can have a chance to handle the event too.Alirio Mendes
It works, thank you!! I dont know how can I mark your comment as an answerOlga
I will add an answer, so you can mark it.Alirio Mendes

1 Answers

0
votes

Return false that the other view can have a chance to handle the event too.

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;
            }
        });
    }
}