Monday, 8 April 2019

How to Scroll Vertically Viewpager

I am unable to scroll view pager vertically. when I use NestedScrollView in the Child layout, pages become blank. I tried many solutions.but failed.

This is my main fragment nav_swipe.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
    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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >


        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:textColor="#fff" />

    </android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>

This is Child fragment layout : fragment_a.xml. where I used nestedscrollview but get no solution.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimaryDark"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1" />

</android.support.constraint.ConstraintLayout>

This is SwipeNav.java

public class SwipeNav extends Fragment {

private MyPagerAdapter myPagerAdapter;
private ViewPager viewPager;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.nav_swipe, container, false);
        myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(),getContext());
        viewPager = (ViewPager) rootView.findViewById(R.id.pager);
        viewPager.setAdapter(myPagerAdapter);
        return rootView;
    }


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

This is MyPagerAdapter.java

public class MyPagerAdapter extends FragmentStatePagerAdapter {
    private Context context;
    private String[] tabTitlesArray = null;
    public MyPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        tabTitlesArray = context.getResources().getStringArray(R.array.tab_titles);
        this.context= context;
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new AFragment();
        Bundle args = new Bundle();
        args.putString(AFragment.ARG_OBJECT, tabTitlesArray[i]);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return tabTitlesArray.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        //return tabTitleArray[position];
        return "OBJECT " + (position + 1);
    }
}

This is AFragment.java

public class AFragment extends Fragment {
    public static final String ARG_OBJECT = "object";
    public AFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_a, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(R.id.text1)).setText(args.getString(ARG_OBJECT));
        return rootView;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}



from How to Scroll Vertically Viewpager

No comments:

Post a Comment