Using viewpager in my application

后端 未结 1 1048
广开言路
广开言路 2020-12-06 09:07

I want to use viewpager in my application.I\'m tried to do this everyday in one month but i can\'t achieve the solution.I want to create pages with same listview concept but

相关标签:
1条回答
  • 2020-12-06 09:32

    It is hard to work through all that code you posted (most of which has not to do with your question), but as far as I can tell you have not even set up a ViewPager from your code or it is hidden in some XML file that you didn't post.

    What you need to do is create a ViewPager instance. For example in your fragment's XML layout file, like:

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

    Now, in your Fragment you create an adapter that specifies the pages that you want (similarly to how a ListAdapter specifies items to a ListView). For that, create a class (you can do that inline in the Fragment code) that inherits from PagerAdapter. For example, something like:

    private class MyPagerAdapter extends PagerAdapter implements TitleProvider {
    
        private ListView pagerListView1;
        private ListView pagerListView2;
    
        public MyPagerAdapter() {
            LayoutInflater inflater = getActivity().getLayoutInflater();
            pagerListView1 = (ListView) inflater.inflate(R.layout.fragment_pagerlist, null);
            pagerListView2 = (ListView) inflater.inflate(R.layout.fragment_pagerlist, null);
        }
    
        @Override
        public int getCount() {
            return 2;
        }
    
        @Override
        public Object instantiateItem(View container, int position) {
            switch (position) {
            case 0:
                ((ViewPager) container).addView(pagerListView1, 0);
                return pagerListView1;
            case 1:
                ((ViewPager) container).addView(pagerListView2, 0);
                return pagerListView2;
            }
            return null;
        }
    
        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == (View) object;
        }
    
        @Override
        public void finishUpdate(View container) {
        }
    
        @Override
        public Parcelable saveState() {
            return null;
        }
    
        @Override
        public void startUpdate(View container) {
        }
    
        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }
    
    }
    

    Note that I hard-coded 2 pages/lists in this pager. You can fill those as you would with any other ListView. The layout of the pages is just a simple ListView that is inflated from the fragment_pagerlist XML file, which looks something like:

    <?xml version="1.0" encoding="utf-8"?>
    <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/list"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:cacheColorHint="@color/BackgroundLight" />
    

    Finally, you bind the ViewPager's adapter somewhere in your onActivityCreated method:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        ViewPager pager = (ViewPager) getView().findViewById(R.id.pager);
        pager.setAdapter(new MyPagerAdapter());
    }
    

    Note that this does not yet give you a ViewPagerIndicator. Check Jake Wharton's excellent library for that.

    If you want the code to a fully implemented and working version, with multiple lists (and other views) in a ViewPager and a ViewPagerIndicator, take a look at the open source RateBeer for Android project; specifically http://code.google.com/p/ratebeerforandroid/source/browse/RateBeerForAndroid/src/com/ratebeer/android/gui/fragments/SearchFragment.java#486 for a real-world PagerAdapter.

    0 讨论(0)
提交回复
热议问题