Skip to content Skip to sidebar Skip to footer

How Add A Listview Inside A Viewpager In Android

I've been trying to get a list view that can be swipe to change the its items to show the following or preceding week details. Here is what my layout file looks like '

Solution 1:

This is not how ViewPager works. You feed the pages to ViewPager with a PagerAdapter. Your ListView will be contained within a Fragment created by the PagerAdapter.

In the layout:

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

In the FragmentActivity with this layout:

ViewPagerpager= (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(newPagerAdapter(getSupportFragmentManager()));

Example of simle PagerAdapter:

publicclassPagerAdapterextendsFragmentPagerAdapter {

    publicFrontPageAdapter(FragmentManager Fm) {
        super(Fm);
    }

    @Overridepublic Fragment getItem(int position) {
        Bundlearguments=newBundle();
        arguments.putInt("position", position);
        FragmentPagefragment=newFragmentPage();
        fragment.setArguments(arguments);
        return fragment;
    }

    @OverridepublicintgetCount() {
        // return count of pagesreturn3;
    }
}

Example of FragmentPage:

publicclassFragmentPageextendsFragment {

    publicFragmentPage() {}

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Viewview= inflater.inflate(R.layout.fragment_frontpage, container, false);  
        // probably cast to ViewGroup and find your ListView          Bundlearguments= getArguments();
        intposition= b.getInt("position");

        return view;
    }
}

Post a Comment for "How Add A Listview Inside A Viewpager In Android"