Android fragment tab host + viewpager swipe?

后端 未结 4 911
一向
一向 2020-12-30 17:38

I currently have a fragment tab host, made by this tutorial http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

<
相关标签:
4条回答
  • TabHost doesn't provide ViewPager support.
    Also, I suggest you to not use TabHost – this is old style.
    Use PagerTabStrip

    Look this Gist.

    UPD: I add some code, for case, if this Gist be deleted;

    MainActivity.java

    package ch.pboos.android.sample.viewpager;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    public class MainActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
            viewPager.setAdapter(new SampleFragmentPagerAdapter());
        }
    
        public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
            final int PAGE_COUNT = 5;
    
            public SampleFragmentPagerAdapter() {
                super(getSupportFragmentManager());
            }
    
            @Override
            public int getCount() {
                return PAGE_COUNT;
            }
    
            @Override
            public Fragment getItem(int position) {
                return PageFragment.create(position + 1);
            }
        }
    
        public static class PageFragment extends Fragment {
            public static final String ARG_PAGE = "ARG_PAGE";
    
            private int mPage;
    
            public static PageFragment create(int page) {
                Bundle args = new Bundle();
                args.putInt(ARG_PAGE, page);
                PageFragment fragment = new PageFragment();
                fragment.setArguments(args);
                return fragment;
            }
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                mPage = getArguments().getInt(ARG_PAGE);
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment_page, container, false);
                TextView textView = (TextView) view;
                textView.setText("Fragment #" + mPage);
                return view;
            }
        }
    }
    

    activity_main.xml (without PagerTabStrip)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>
    

    activity_main_2.xml (with PagerTabStrip)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#000"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:textColor="#fff" />
    
    </android.support.v4.view.ViewPager>
    

    fragment_page.xml

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

    getPageTitle

    public CharSequence getPageTitle(int position) {
        return "Page " + (position + 1);
    }
    

    UPD 2 With Fragment:

    public class MainFragment extends Fragment{
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.activity_main_2, container, false);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
            viewPager.setAdapter(new SampleFragmentPagerAdapter());
        }
    
        public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
            final int PAGE_COUNT = 5;
    
            public SampleFragmentPagerAdapter() {
                super(getSupportFragmentManager());
            }
    
            @Override
            public int getCount() {
                return PAGE_COUNT;
            }
    
            @Override
            public Fragment getItem(int position) {
                return PageFragment.create(position + 1);
            }
        }
    
        public static class PageFragment extends Fragment {
            public static final String ARG_PAGE = "ARG_PAGE";
    
            private int mPage;
    
            public static PageFragment create(int page) {
                Bundle args = new Bundle();
                args.putInt(ARG_PAGE, page);
                PageFragment fragment = new PageFragment();
                fragment.setArguments(args);
                return fragment;
            }
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                mPage = getArguments().getInt(ARG_PAGE);
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment_page, container, false);
                TextView textView = (TextView) view;
                textView.setText("Fragment #" + mPage);
                return view;
            }
        }
    }
    

    Hope it helps

    0 讨论(0)
  • 2020-12-30 18:09

    Inorder to include viewpager library to the project ,you should import the library
    "Existing Project” to your current one.

    Step 1:-Download source code from GitHub.(https://github.com/JakeWharton/Android-ViewPagerIndicator)

    Step2 :-In your Android Studio Project: File -> Project Structure -> add (+ symbol) -> Import Existing Project. Import just the folder called ”library”, not the entire project (leave the import options as Android Studio suggests).

    step 3:-If the "compileSdkVersion" specified in your build.gradle doesn’t match with the one specified in the Android-ViewPagerIndicator project, change the second one. The same apply with any other property, such as "minSdkVersion" or even the current support library.

    Step 4:-Add Android-ViewPagerIndicator project as a dependency to your build.gradle module: dependencies { compile project(':library') }

    Step 5:- Sync project with gradle files.

    0 讨论(0)
  • 2020-12-30 18:20

    use the following solution i have put the view page like below.. and my work done..

    <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"
        tools:context=".MainActivity" >
    
    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />
    
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:overScrollMode="never" />
        </LinearLayout>
    </TabHost>
    

    0 讨论(0)
  • 2020-12-30 18:24

    You can implement the viewpager as a library.

    First thing you need to import viewpager project to the eclipse workspace. [ I think you are modifying the project :Tabhost example]

    so right click your Tabhost example project thn build path -> configure build path.. thn click Android ...when you click android you can see 2 section Project build Target & Library.So in Library you can add the viewpager Library.

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