1

I have 2 view pagers in a activity

 <android.support.v4.view.ViewPager
        android:id="@+id/latestProductSwipeCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

 <android.support.v4.view.ViewPager
        android:id="@+id/popularProductSwipeCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

and I created an adapter

public class ProductSwipePageAdapter extends FragmentPagerAdapter {

List<Product> products;

public ProductSwipePageAdapter(FragmentManager fm) {
    super(fm);
}

public ProductSwipePageAdapter(FragmentManager fm, List<Product> products) {
    super(fm);
    this.products = products;
}


@Override
public Fragment getItem(int position) {
    return ProductCardFragment.newInstance(products.get(position));
}

@Override
public int getCount() {
    return products.size();
}}

And i setting the pager

  productSwipePageAdapter1 = new ProductSwipePageAdapter(getFragmentManager(), allProducts);
    latestProductSwipeCard.setAdapter(productSwipePageAdapter1);

productSwipePageAdapter2 = new ProductSwipePageAdapter(getFragmentManager(), allProducts1);
    popularProductSwipeCard.setAdapter(productSwipePageAdapter2);

As a Result it showing only one view pager

enter image description here

Poovarasan
  • 11
  • 1
  • 3
  • did you add adapter to second viewPager? – Amir Aug 08 '16 at 07:03
  • please check this answer http://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts – Shanto George Aug 08 '16 at 07:05
  • I'm also experiencing this exact issue, except, I have a custom view that wraps the ViewPager and I'm placing multiple instances of this custom view in an xml layout file which renders all the views correctly for the first custom view but for all the rest of custom views the view pager is empty while other views around the view pager are loaded just fine.... – DritanX Jul 18 '17 at 08:17
  • is there anyone have solutions for this problems? I already checked many threads but found no correct solutions. – Vu Thai Duy Aug 29 '21 at 04:00

2 Answers2

2

if you not set specific height of view pager than ViewPager access all height

so if you show both ViewPager than you set Weight property in your XMl

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/latestProductSwipeCard"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/popularProductSwipeCard"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>
Divyesh Boda
  • 258
  • 2
  • 12
0

See This Example Its Working for me

activity_main.xml

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

    <android.support.v4.view.ViewPager
        android:id="@+id/latestProductSwipeCard"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/popularProductSwipeCard"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment"/>

</RelativeLayout>

MainActivity.java

package com.stackoverflowtesting;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        ViewPager mViewPager1 = (ViewPager) findViewById(R.id.latestProductSwipeCard);
        mViewPager1.setAdapter(mSectionsPagerAdapter);


        // Set up the ViewPager with the sections adapter.
        ViewPager mViewPager2 = (ViewPager) findViewById(R.id.popularProductSwipeCard);
        mViewPager2.setAdapter(mSectionsPagerAdapter);
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
            }
            return null;
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}
Divyesh Boda
  • 258
  • 2
  • 12