How to add listview inside viewpager in android

I am trying to get a list view that can be scrolled to change its elements to show next or previous information. This is what my layout file looks like

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

    <ListView
        android:id="@+id/week_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</android.support.v4.view.ViewPager>

But I did not understand what I want. could you help me? Thanks in advance.

0
source share
1 answer

This does not work ViewPager. You load ViewPager pages with the PagerAdapter . Your ListView will be contained in the Snippet 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 FragmentActivity with this layout:

ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));

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

FragmentPage example:

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;
    }
}
+2
source

Source: https://habr.com/ru/post/1626829/


All Articles