Jerin Raj
Jerin Raj

Reputation: 387

How to add a listview inside a viewpager?

How to add a listview inside a viewpager (without using fragments)? Could you please help me? Thanks in advance.

Upvotes: 4

Views: 1484

Answers (4)

Priyankchoudhary
Priyankchoudhary

Reputation: 826

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:

ViewPager pager = (ViewPager) findViewById(R.id.viewPager); pager.setAdapter(new PagerAdapter(getSupportFragmentManager())); Example of simle PagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {

public FrontPageAdapter(FragmentManager Fm) {
    super(Fm);
}

@Override
public Fragment getItem(int position) {
    Bundle arguments = new Bundle();
    arguments.putInt("position", position);
    FragmentPage fragment = new FragmentPage();
    fragment.setArguments(arguments);
    return fragment;
}

@Override
public int getCount() {
    // return count of pages
    return 3;
       }
 }

Example of FragmentPage:

public class FragmentPage extends Fragment {

public FragmentPage() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_frontpage, container, false);  
    // probably cast to ViewGroup and find your ListView          

    Bundle arguments = getArguments();
    int position = b.getInt("position");

    return view;
    }
}

Answered here - https://stackoverflow.com/a/28798795/7764015

Upvotes: 0

Amit Vaghela
Amit Vaghela

Reputation: 22945

It will be easy if you use Fragment in ViewPager, BUT if you dont want to use ViewPager with Fragment than ViewPager Without Fragments is example where you add Views and layouts to ViewPager and is more complex.

A good example is an image gallery, where the user can swipe between different pictures. On these types of pages, all you really want to display is a view of static content (in this case, an image), how to utilize the ViewPager with just plain-old Views and layouts.

Upvotes: 1

Punit Sharma
Punit Sharma

Reputation: 2947

You can't use ViewPager to swipe between Activities. You need to convert each of you Activities into Fragments, then combine everything in one FragmentActivity with the Adapter you use with ViewPager.

Upvotes: 0

Chaman Saini
Chaman Saini

Reputation: 365

It will be best to use fragment for each pages of viewPager. In this way you will be able to maintain individual pages easily.

In your fragment class(a java class which extends Fragment) you can work with your listview like initializing,setting adapter to listview,setting onClickListner for listview items same as you do in Activity

Upvotes: 0

Related Questions