Android 如何获得端到端的滑动浏览页面?

Android 如何获得端到端的滑动浏览页面?,android,android-viewpager,android-view,swipe,Android,Android Viewpager,Android View,Swipe,我正在使用一个扩展名为PagerAdapter的ViewPager。没有涉及片段。我将ViewPager用于常规视图。我注意到,在浏览视图时,视图不会保持端到端的连接。这就像一个无限的边界。(开始或结束情况没有问题,只是刷卡时的动态情况。) 如何在下一页中立即滑动? 说清楚一点:我看到了 相反,我希望看到: 我注意到,使用带有选项卡的操作栏、FragmentPagerAdapter,然后使用带有片段的ViewPager,确实具有这种正确的行为。尽管有时也会留下一些空白 这是我的密码: Hel

我正在使用一个扩展名为
PagerAdapter
ViewPager。没有涉及
片段
。我将
ViewPager
用于常规
视图
。我注意到,在浏览视图时,视图不会保持端到端的连接。这就像一个无限的边界。(开始或结束情况没有问题,只是刷卡时的动态情况。)

如何在下一页中立即滑动?

说清楚一点:我看到了

相反,我希望看到:

我注意到,使用带有选项卡的操作栏、FragmentPagerAdapter,然后使用带有片段的ViewPager,确实具有这种正确的行为。尽管有时也会留下一些空白

这是我的密码:

HelloTabWidget.java

package com.example.hellotabwidget;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

public class HelloTabWidget extends Activity {

    private TabHost mTabHost;

    TabsAdapter mTabsAdapter;

    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_tab_widget);

        findViews();
    }

    /**
     *
     */
    protected void findViews() {

        mViewPager = (ViewPager) findViewById(R.id.realtabcontent);

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        HorizontalScrollView mScrollView = (HorizontalScrollView) findViewById(R.id.sv);

        TabWidget mTabWidget = (TabWidget) findViewById(android.R.id.tabs);

        mTabsAdapter = new TabsAdapter(this, mViewPager, mTabHost, mTabWidget, mScrollView);

        TabHost.TabSpec tab;
        tab = mTabHost.newTabSpec("tab_test1")
                .setIndicator("TABLATURE_1_IT_IS").setContent(R.id.textview1);
        mTabsAdapter.addTab(tab, R.id.textview1, "tab_test1", null);

        tab = mTabHost.newTabSpec("tab_test2")
                .setIndicator("TABLATURE_2_IT_IS").setContent(R.id.textview2);
        mTabsAdapter.addTab(tab, R.id.textview2, "tab_test2", null);

        tab = mTabHost.newTabSpec("tab_test3")
                .setIndicator("TABLATURE_3_IT_IS").setContent(R.id.textview3);
        mTabsAdapter.addTab(tab, R.id.textview3, "tab_test3", null);

        tab = mTabHost.newTabSpec("tab_test4")
                .setIndicator("TABLATURE_4_IT_IS").setContent(R.id.textview4);
        mTabsAdapter.addTab(tab, R.id.textview4, "tab_test4", null);

        tab = mTabHost.newTabSpec("tab_test5")
                .setIndicator("TABLATURE_5_IT_IS").setContent(R.id.textview5);
        mTabsAdapter.addTab(tab, R.id.textview5, "tab_test5", null);

        mTabsAdapter.finalizeTabs();

        mTabHost.setCurrentTab(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_hello_tab_widget, menu);
        return true;
    }

    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost. It relies on a
     * trick. Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show. This is not sufficient for switching
     * between pages. So instead we make the content part of the tab host 0dp
     * high (it is not shown) and the TabsAdapter supplies its own dummy view to
     * show as the tab content. It listens to changes in tabs, and takes care of
     * switch to the correct paged in the ViewPager whenever the selected tab
     * changes.
     */
    public static class TabsAdapter extends PagerAdapter implements
            ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener {
        public static final class TabInfo {
            private final Bundle args;
            private final String tag;
            private final int tabContentViewId;

            TabInfo(Bundle args, String tag, int tabContentViewId) {
                this.args = args;
                this.tag = tag;
                this.tabContentViewId = tabContentViewId;
            }

            public String getTag() {
                return tag;
            }

            public int getTabContentViewId() {
                return tabContentViewId;
            }
        }

        private Activity mContext;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        private final ViewPager mViewPager;

        private final TabHost mTabHost;

        private final TabWidget mTabWidget;

        private final HorizontalScrollView mScrollView;

        private Runnable mTabSelector;

        public TabsAdapter(Activity context, ViewPager pager, TabHost tabHost, TabWidget tabWidget, HorizontalScrollView scrollView) {
            super();
            mContext = context;
            mTabHost = tabHost;
            mTabHost.setOnTabChangedListener(this);
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
            mTabWidget = tabWidget;
            mScrollView = scrollView;
        }

        public void finalizeTabs() {
            for (int i = 0; i < getCount(); i++) {
                //mTabWidget.getChildAt(i).setFocusableInTouchMode(true);//TODO
            }

            //Keep everything in memory.
            mViewPager.setOffscreenPageLimit(getCount());
        }

        public void addTab(TabHost.TabSpec tab, int tabContentViewId,
                String tag, Bundle args) {
            TabInfo info = new TabInfo(args, tag, tabContentViewId);
            mTabs.add(info);
            mTabHost.addTab(tab);
            notifyDataSetChanged();
        }

        /**
         * Determines whether a page View is associated with a specific key
         * object as returned by instantiateItem(ViewGroup, int). This method is
         * required for a PagerAdapter to function properly.
         * 
         * @param view
         *            Page View to check for association with object
         * @param object
         *            Object to check for association with view
         * @return
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return ((object instanceof View) && (view.getId() == ((View)object).getId()))
                    || (view == object);
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onPageSelected(int position) {
            mTabHost.setCurrentTab(position);
            animateToTab(position);
        }

        @Override
        public void onTabChanged(String tabId) {
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i).getTag().equals(tabId)) {
                    mViewPager.setCurrentItem(i);
                    animateToTab(i);
                }
            }
        }

        public void destruct() {
            mContext = null;
        }

        /**
         * Remove a page for the given position. The adapter is responsible for
         * removing the view from its container, although it only must ensure
         * this is done by the time it returns from
         * {@link #finishUpdate(android.view.ViewGroup)}.
         * 
         * @param collection
         *            The containing View from which the page will be removed.
         * @param position
         *            The page position to be removed.
         * @param view
         *            The same object that was returned by
         *            {@link #instantiateItem(android.view.View, int)}.
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object view) {
            container.removeView((TextView) view);
        }

        /**
         * Create the page for the given position. The adapter is responsible
         * for adding the view to the container given here, although it only
         * must ensure this is done by the time it returns from
         * {@link #finishUpdate(android.view.ViewGroup)}.
         * 
         * @param collection
         *            The containing View in which the page will be shown.
         * @param position
         *            The page position to be instantiated.
         * @return Returns an Object representing the new page. This does not
         *         need to be a View, but can be some other container of the
         *         page.
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mContext.findViewById(mTabs.get(position)
                    .getTabContentViewId());
            if(view==null) return null;//Might happen if ViewPager.setOffscreenPageLimit() is not big enough
            ViewGroup formerParent = (ViewGroup)view.getParent();
            if(formerParent!=null && formerParent.getId()==container.getId()){
                return view;
            }
            if(formerParent!=null){
                formerParent.removeView(view);
            }
            container.addView(view, 0, new ViewPager.LayoutParams());
            return view;
        }

        /**
         * Called when the a change in the shown pages has been completed. At
         * this point you must ensure that all of the pages have actually been
         * added or removed from the container as appropriate.
         * 
         * @param arg0
         *            The containing View which is displaying this adapter's
         *            page views.
         */
        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
        }

        private void animateToTab(final int position) {
            final View tabView = mTabWidget.getChildAt(position);
            if (mTabSelector != null) {
                mScrollView.removeCallbacks(mTabSelector);
            }
            mTabSelector = new Runnable() {
                public void run() {
                    final int scrollPos = tabView.getLeft()
                            - (mScrollView.getWidth() - tabView.getWidth()) / 2;
                    mScrollView.smoothScrollTo(scrollPos, 0);
                    mTabSelector = null;
                }
            };
            mScrollView.post(mTabSelector);
        }

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview50"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TextView
        android:id="@+id/textview60"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TextView
        android:id="@+id/textview70"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|fill_vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            android:orientation="vertical" >

            <HorizontalScrollView
                android:id="@+id/sv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:fadingEdge="horizontal"
                android:fillViewport="true"
                android:scrollbars="none" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" />
            </HorizontalScrollView>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" >

                <TextView
                    android:id="@+id/textview1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a tab" />

                <TextView
                    android:id="@+id/textview2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is another tab" />

                <TextView
                    android:id="@+id/textview3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a third tab" />

                <TextView
                    android:id="@+id/textview4"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a fourth tab" />

                <TextView
                    android:id="@+id/textview5"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a fifth tab" />
            </FrameLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1" >
            </android.support.v4.view.ViewPager>
        </LinearLayout>
    </TabHost>

</LinearLayout>
package com.example.hellotabwidget;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

public class HelloTabWidget extends Activity {

    private TabHost mTabHost;

    TabsAdapter mTabsAdapter;

    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_tab_widget);

        findViews();
    }

    /**
     *
     */
    protected void findViews() {

        mViewPager = (ViewPager) findViewById(R.id.realtabcontent);

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        HorizontalScrollView mScrollView = (HorizontalScrollView) findViewById(R.id.sv);

        TabWidget mTabWidget = (TabWidget) findViewById(android.R.id.tabs);

        mTabsAdapter = new TabsAdapter(this, mViewPager, mTabHost, mTabWidget, mScrollView);

        TabHost.TabSpec tab;
        tab = mTabHost.newTabSpec("tab_test1")
                .setIndicator("TABLATURE_1_IT_IS").setContent(R.id.dummy_textview);
        mTabsAdapter.addTab(tab, R.id.textview1, "tab_test1", null);

        tab = mTabHost.newTabSpec("tab_test2")
                .setIndicator("TABLATURE_2_IT_IS").setContent(R.id.textview2);
        mTabsAdapter.addTab(tab, R.id.textview2, "tab_test2", null);

        tab = mTabHost.newTabSpec("tab_test3")
                .setIndicator("TABLATURE_3_IT_IS").setContent(R.id.textview3);
        mTabsAdapter.addTab(tab, R.id.textview3, "tab_test3", null);

        tab = mTabHost.newTabSpec("tab_test4")
                .setIndicator("TABLATURE_4_IT_IS").setContent(R.id.textview4);
        mTabsAdapter.addTab(tab, R.id.textview4, "tab_test4", null);

        tab = mTabHost.newTabSpec("tab_test5")
                .setIndicator("TABLATURE_5_IT_IS").setContent(R.id.textview5);
        mTabsAdapter.addTab(tab, R.id.textview5, "tab_test5", null);

        mTabsAdapter.finalizeTabs();

        mTabHost.setCurrentTab(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_hello_tab_widget, menu);
        return true;
    }

    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost. It relies on a
     * trick. Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show. This is not sufficient for switching
     * between pages. So instead we make the content part of the tab host 0dp
     * high (it is not shown) and the TabsAdapter supplies its own dummy view to
     * show as the tab content. It listens to changes in tabs, and takes care of
     * switch to the correct paged in the ViewPager whenever the selected tab
     * changes.
     */
    public static class TabsAdapter extends PagerAdapter implements
            ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener {
        public static final class TabInfo {
            private final Bundle args;
            private final String tag;
            private final int tabContentViewId;

            TabInfo(Bundle args, String tag, int tabContentViewId) {
                this.args = args;
                this.tag = tag;
                this.tabContentViewId = tabContentViewId;
            }

            public String getTag() {
                return tag;
            }

            public int getTabContentViewId() {
                return tabContentViewId;
            }
        }

        private Activity mContext;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        private final ViewPager mViewPager;

        private final TabHost mTabHost;

        private final TabWidget mTabWidget;

        private final HorizontalScrollView mScrollView;

        private Runnable mTabSelector;

        public TabsAdapter(Activity context, ViewPager pager, TabHost tabHost, TabWidget tabWidget, HorizontalScrollView scrollView) {
            super();
            mContext = context;
            mTabHost = tabHost;
            mTabHost.setOnTabChangedListener(this);
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
            mTabWidget = tabWidget;
            mScrollView = scrollView;
        }

        public void finalizeTabs() {
            for (int i = 0; i < getCount(); i++) {
                //mTabWidget.getChildAt(i).setFocusableInTouchMode(true);//TODO
            }

            //Keep everything in memory.
            mViewPager.setOffscreenPageLimit(getCount());
        }

        public void addTab(TabHost.TabSpec tab, int tabContentViewId,
                String tag, Bundle args) {
            TabInfo info = new TabInfo(args, tag, tabContentViewId);
            mTabs.add(info);
            mTabHost.addTab(tab);
            notifyDataSetChanged();
        }

        /**
         * Determines whether a page View is associated with a specific key
         * object as returned by instantiateItem(ViewGroup, int). This method is
         * required for a PagerAdapter to function properly.
         * 
         * @param view
         *            Page View to check for association with object
         * @param object
         *            Object to check for association with view
         * @return
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return ((object instanceof View) && (view.getId() == ((View)object).getId()))
                    || (view == object);
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onPageSelected(int position) {
            mTabHost.setCurrentTab(position);
            animateToTab(position);
        }

        @Override
        public void onTabChanged(String tabId) {
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i).getTag().equals(tabId)) {
                    mViewPager.setCurrentItem(i);
                    animateToTab(i);
                }
            }
        }

        public void destruct() {
            mContext = null;
        }

        /**
         * Remove a page for the given position. The adapter is responsible for
         * removing the view from its container, although it only must ensure
         * this is done by the time it returns from
         * {@link #finishUpdate(android.view.ViewGroup)}.
         * 
         * @param collection
         *            The containing View from which the page will be removed.
         * @param position
         *            The page position to be removed.
         * @param view
         *            The same object that was returned by
         *            {@link #instantiateItem(android.view.View, int)}.
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object view) {
            container.removeView((TextView) view);
        }

        /**
         * Create the page for the given position. The adapter is responsible
         * for adding the view to the container given here, although it only
         * must ensure this is done by the time it returns from
         * {@link #finishUpdate(android.view.ViewGroup)}.
         * 
         * @param collection
         *            The containing View in which the page will be shown.
         * @param position
         *            The page position to be instantiated.
         * @return Returns an Object representing the new page. This does not
         *         need to be a View, but can be some other container of the
         *         page.
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mContext.findViewById(mTabs.get(position)
                    .getTabContentViewId());
            container.addView(view, 0, new ViewPager.LayoutParams());
            return view;
        }

        /**
         * Called when the a change in the shown pages has been completed. At
         * this point you must ensure that all of the pages have actually been
         * added or removed from the container as appropriate.
         * 
         * @param arg0
         *            The containing View which is displaying this adapter's
         *            page views.
         */
        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
        }

        private void animateToTab(final int position) {
            final View tabView = mTabWidget.getChildAt(position);
            if (mTabSelector != null) {
                mScrollView.removeCallbacks(mTabSelector);
            }
            mTabSelector = new Runnable() {
                public void run() {
                    final int scrollPos = tabView.getLeft()
                            - (mScrollView.getWidth() - tabView.getWidth()) / 2;
                    mScrollView.smoothScrollTo(scrollPos, 0);
                    mTabSelector = null;
                }
            };
            mScrollView.post(mTabSelector);
        }

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview50"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TextView
        android:id="@+id/textview60"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TextView
        android:id="@+id/textview70"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|fill_vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            android:orientation="vertical" >

            <HorizontalScrollView
                android:id="@+id/sv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:fadingEdge="horizontal"
                android:fillViewport="true"
                android:scrollbars="none" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" />
            </HorizontalScrollView>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" >

                <TextView
                    android:id="@+id/dummy_textview"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="dummy" />
            </FrameLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/textview1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a tab" />

                <TextView
                    android:id="@+id/textview2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is another tab" />

                <TextView
                    android:id="@+id/textview3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a third tab" />

                <TextView
                    android:id="@+id/textview4"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a fourth tab" />

                <TextView
                    android:id="@+id/textview5"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a fifth tab" />
            </android.support.v4.view.ViewPager>
        </LinearLayout>
    </TabHost>

</LinearLayout>
package com.example.hellotabwidget;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.os.Bundle;
导入android.support.v4.view.PagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.view.Menu;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.HorizontalScrollView;
导入android.widget.TabHost;
导入android.widget.TabWidget;
导入android.widget.TextView;
公共类HelloTabWidget扩展活动{
私有TabHost-mTabHost;
TabsAdapter MTABASADAPTER;
ViewPager mViewPager;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u hello\u tab\u小部件);
FindView();
}
/**
*
*/
受保护的void findViews(){
mViewPager=(ViewPager)findViewById(R.id.realtabcontent);
mTabHost=(TabHost)findViewById(android.R.id.TabHost);
mTabHost.setup();
HorizontalScrollView mScrollView=(HorizontalScrollView)findViewById(R.id.sv);
TabWidget mTabWidget=(TabWidget)findViewById(android.R.id.tabs);
MTABAdapter=new TabsAdapter(this、mviewpage、mTabHost、mTabWidget、mScrollView);
TabHost.TabSpec选项卡;
tab=mTabHost.newTabSpec(“tab_test1”)
.setIndicator(“Tablography_1_IT_IS”).setContent(R.id.textview1);
mtabAdapter.addTab(tab,R.id.textview1,“tab_test1”,null);
tab=mTabHost.newTabSpec(“tab_test2”)
.setIndicator(“Tablography_2_IT_IS”).setContent(R.id.textview2);
mtabAdapter.addTab(tab,R.id.textview2,“tab_test2”,null);
tab=mTabHost.newTabSpec(“tab_test3”)
.setIndicator(“Tablography_3_IT_IS”).setContent(R.id.textview3);
mtabAdapter.addTab(tab,R.id.textview3,“tab_test3”,null);
tab=mTabHost.newTabSpec(“tab_test4”)
.setIndicator(“Tablography_4_IT_IS”).setContent(R.id.textview4);
mtabAdapter.addTab(tab,R.id.textview4,“tab_test4”,null);
tab=mTabHost.newTabSpec(“tab_test5”)
.setIndicator(“Tablography_5_IT_IS”).setContent(R.id.textview5);
mtabAdapter.addTab(tab,R.id.textview5,“tab_test5”,null);
mtabAdapter.finalizeTabs();
mTabHost.setCurrentTab(0);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u hello\u tab\u小部件,菜单);
返回true;
}
/**
*这是一个帮助器类,用于实现选项卡和所有
*将ViewPager与关联的TabHost连接的详细信息。它依赖于
*通常,选项卡主机有一个简单的API来提供视图或
*每个选项卡将显示的意图。这不足以进行切换
*在页面之间。因此,我们将选项卡的内容部分设为主机0dp
*high(未显示),TabsAdapter将自己的虚拟视图提供给
*显示为选项卡内容。它侦听选项卡中的更改,并处理
*每当选中选项卡时,切换到ViewPager中的正确页面
*变化。
*/
公共静态类TabsAdapter扩展了PagerAdapter实现
ViewPager.OnPageChangeListener,TabHost.OnTabChangeListener{
公共静态最终类TabInfo{
私有最终包args;
私有最终字符串标签;
私有final int tabContentViewId;
TabInfo(绑定参数、字符串标记、int-tabContentViewId){
this.args=args;
this.tag=tag;
this.tabContentViewId=tabContentViewId;
}
公共字符串getTag(){
返回标签;
}
public int getTabContentViewId(){
返回tabContentViewId;
}
}
私人活动mContext;
private final ArrayList mtab=new ArrayList();
专用最终查看页面mViewPager;
私有最终选项卡主机mTabHost;
私有最终TabWidget mTabWidget;
私有最终水平滚动视图mScrollView;
私人可运行mTabSelector;
公共TabsAdapter(活动上下文、ViewPager寻呼机、TabHost TabHost、TabWidget TabWidget、HorizontalScrollView scrollView){
超级();
mContext=上下文;
mTabHost=tabHost;
mTabHost.setOnTabChangedListener(此);
mViewPager=寻呼机;
mViewPager.setAdapter(此);
mViewPager.setOnPageChangeListener(此);
mTabWidget=tabWidget;
mScrollView=滚动视图;
}
公共图书馆{
对于(int i=0;ipackage com.example.hellotabwidget;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

public class HelloTabWidget extends Activity {

    private TabHost mTabHost;

    TabsAdapter mTabsAdapter;

    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_tab_widget);

        findViews();
    }

    /**
     *
     */
    protected void findViews() {

        mViewPager = (ViewPager) findViewById(R.id.realtabcontent);

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        HorizontalScrollView mScrollView = (HorizontalScrollView) findViewById(R.id.sv);

        TabWidget mTabWidget = (TabWidget) findViewById(android.R.id.tabs);

        mTabsAdapter = new TabsAdapter(this, mViewPager, mTabHost, mTabWidget, mScrollView);

        TabHost.TabSpec tab;
        tab = mTabHost.newTabSpec("tab_test1")
                .setIndicator("TABLATURE_1_IT_IS").setContent(R.id.dummy_textview);
        mTabsAdapter.addTab(tab, R.id.textview1, "tab_test1", null);

        tab = mTabHost.newTabSpec("tab_test2")
                .setIndicator("TABLATURE_2_IT_IS").setContent(R.id.textview2);
        mTabsAdapter.addTab(tab, R.id.textview2, "tab_test2", null);

        tab = mTabHost.newTabSpec("tab_test3")
                .setIndicator("TABLATURE_3_IT_IS").setContent(R.id.textview3);
        mTabsAdapter.addTab(tab, R.id.textview3, "tab_test3", null);

        tab = mTabHost.newTabSpec("tab_test4")
                .setIndicator("TABLATURE_4_IT_IS").setContent(R.id.textview4);
        mTabsAdapter.addTab(tab, R.id.textview4, "tab_test4", null);

        tab = mTabHost.newTabSpec("tab_test5")
                .setIndicator("TABLATURE_5_IT_IS").setContent(R.id.textview5);
        mTabsAdapter.addTab(tab, R.id.textview5, "tab_test5", null);

        mTabsAdapter.finalizeTabs();

        mTabHost.setCurrentTab(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_hello_tab_widget, menu);
        return true;
    }

    /**
     * This is a helper class that implements the management of tabs and all
     * details of connecting a ViewPager with associated TabHost. It relies on a
     * trick. Normally a tab host has a simple API for supplying a View or
     * Intent that each tab will show. This is not sufficient for switching
     * between pages. So instead we make the content part of the tab host 0dp
     * high (it is not shown) and the TabsAdapter supplies its own dummy view to
     * show as the tab content. It listens to changes in tabs, and takes care of
     * switch to the correct paged in the ViewPager whenever the selected tab
     * changes.
     */
    public static class TabsAdapter extends PagerAdapter implements
            ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener {
        public static final class TabInfo {
            private final Bundle args;
            private final String tag;
            private final int tabContentViewId;

            TabInfo(Bundle args, String tag, int tabContentViewId) {
                this.args = args;
                this.tag = tag;
                this.tabContentViewId = tabContentViewId;
            }

            public String getTag() {
                return tag;
            }

            public int getTabContentViewId() {
                return tabContentViewId;
            }
        }

        private Activity mContext;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        private final ViewPager mViewPager;

        private final TabHost mTabHost;

        private final TabWidget mTabWidget;

        private final HorizontalScrollView mScrollView;

        private Runnable mTabSelector;

        public TabsAdapter(Activity context, ViewPager pager, TabHost tabHost, TabWidget tabWidget, HorizontalScrollView scrollView) {
            super();
            mContext = context;
            mTabHost = tabHost;
            mTabHost.setOnTabChangedListener(this);
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
            mTabWidget = tabWidget;
            mScrollView = scrollView;
        }

        public void finalizeTabs() {
            for (int i = 0; i < getCount(); i++) {
                //mTabWidget.getChildAt(i).setFocusableInTouchMode(true);//TODO
            }

            //Keep everything in memory.
            mViewPager.setOffscreenPageLimit(getCount());
        }

        public void addTab(TabHost.TabSpec tab, int tabContentViewId,
                String tag, Bundle args) {
            TabInfo info = new TabInfo(args, tag, tabContentViewId);
            mTabs.add(info);
            mTabHost.addTab(tab);
            notifyDataSetChanged();
        }

        /**
         * Determines whether a page View is associated with a specific key
         * object as returned by instantiateItem(ViewGroup, int). This method is
         * required for a PagerAdapter to function properly.
         * 
         * @param view
         *            Page View to check for association with object
         * @param object
         *            Object to check for association with view
         * @return
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return ((object instanceof View) && (view.getId() == ((View)object).getId()))
                    || (view == object);
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onPageSelected(int position) {
            mTabHost.setCurrentTab(position);
            animateToTab(position);
        }

        @Override
        public void onTabChanged(String tabId) {
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i).getTag().equals(tabId)) {
                    mViewPager.setCurrentItem(i);
                    animateToTab(i);
                }
            }
        }

        public void destruct() {
            mContext = null;
        }

        /**
         * Remove a page for the given position. The adapter is responsible for
         * removing the view from its container, although it only must ensure
         * this is done by the time it returns from
         * {@link #finishUpdate(android.view.ViewGroup)}.
         * 
         * @param collection
         *            The containing View from which the page will be removed.
         * @param position
         *            The page position to be removed.
         * @param view
         *            The same object that was returned by
         *            {@link #instantiateItem(android.view.View, int)}.
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object view) {
            container.removeView((TextView) view);
        }

        /**
         * Create the page for the given position. The adapter is responsible
         * for adding the view to the container given here, although it only
         * must ensure this is done by the time it returns from
         * {@link #finishUpdate(android.view.ViewGroup)}.
         * 
         * @param collection
         *            The containing View in which the page will be shown.
         * @param position
         *            The page position to be instantiated.
         * @return Returns an Object representing the new page. This does not
         *         need to be a View, but can be some other container of the
         *         page.
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mContext.findViewById(mTabs.get(position)
                    .getTabContentViewId());
            container.addView(view, 0, new ViewPager.LayoutParams());
            return view;
        }

        /**
         * Called when the a change in the shown pages has been completed. At
         * this point you must ensure that all of the pages have actually been
         * added or removed from the container as appropriate.
         * 
         * @param arg0
         *            The containing View which is displaying this adapter's
         *            page views.
         */
        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
        }

        private void animateToTab(final int position) {
            final View tabView = mTabWidget.getChildAt(position);
            if (mTabSelector != null) {
                mScrollView.removeCallbacks(mTabSelector);
            }
            mTabSelector = new Runnable() {
                public void run() {
                    final int scrollPos = tabView.getLeft()
                            - (mScrollView.getWidth() - tabView.getWidth()) / 2;
                    mScrollView.smoothScrollTo(scrollPos, 0);
                    mTabSelector = null;
                }
            };
            mScrollView.post(mTabSelector);
        }

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview50"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TextView
        android:id="@+id/textview60"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TextView
        android:id="@+id/textview70"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="some text" />

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left|fill_vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            android:orientation="vertical" >

            <HorizontalScrollView
                android:id="@+id/sv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:fadingEdge="horizontal"
                android:fillViewport="true"
                android:scrollbars="none" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" />
            </HorizontalScrollView>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" >

                <TextView
                    android:id="@+id/dummy_textview"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="dummy" />
            </FrameLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/textview1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a tab" />

                <TextView
                    android:id="@+id/textview2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is another tab" />

                <TextView
                    android:id="@+id/textview3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a third tab" />

                <TextView
                    android:id="@+id/textview4"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a fourth tab" />

                <TextView
                    android:id="@+id/textview5"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="this is a fifth tab" />
            </android.support.v4.view.ViewPager>
        </LinearLayout>
    </TabHost>

</LinearLayout>