当前位置: 代码迷 >> Android >> Tabhost 和 android.support.v4.view.ViewPager 整合的有关问题
  详细解决方案

Tabhost 和 android.support.v4.view.ViewPager 整合的有关问题

热度:99   发布时间:2016-05-01 21:56:52.0
Tabhost 和 android.support.v4.view.ViewPager 整合的问题
最近在做一个将tabhost 改成 tabcontent 部位可左右滑动的东西。使用了谷歌提供的android.support.v4 包里面的viewpager组件.

原理就是把tabhost 里原有的的tabcontent 隐藏掉,加一个viewpager,捕捉事件进而进行互相刷新的动作。

唯一有所不同的就是tabhost里的content是各个单独的Activity.

具体代码如下:


主窗口
Java code
package org.xzx;import android.app.Activity;import android.app.LocalActivityManager;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.os.Parcelable;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.View;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;import java.util.ArrayList;public class TestViewpageActivity extends Activity {    Context context = null;    LocalActivityManager manager = null;    ViewPager pager = null;    TabHost tabHost = null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        context = TestViewpageActivity.this;        manager = new LocalActivityManager(this, true);        manager.dispatchCreate(savedInstanceState);        tabHost = (TabHost) findViewById(android.R.id.tabhost);        tabHost.setup();        tabHost.setup(manager);        tabHost.addTab(tabHost.newTabSpec("A").setIndicator("A")                .setContent(new Intent(context, A.class)));        tabHost.addTab(tabHost.newTabSpec("B").setIndicator("B")                .setContent(new Intent(context, B.class)));        tabHost.addTab(tabHost.newTabSpec("C").setIndicator("C")                .setContent(new Intent(context, C.class)));        tabHost.addTab(tabHost.newTabSpec("D").setIndicator("D")                .setContent(new Intent(context, D.class)));        tabHost.setOnTabChangedListener(new OnTabChangeListener() {            @Override            public void onTabChanged(String tabId) {                if ("A".equals(tabId)) {                    pager.setCurrentItem(0);                } else if ("B".equals(tabId)) {                    pager.setCurrentItem(1);                } else if ("C".equals(tabId)) {                    pager.setCurrentItem(2);                } else {                    pager.setCurrentItem(3);                }            }        });        pager = (ViewPager) findViewById(R.id.viewpage);        final ArrayList<View> list = new ArrayList<View>();        Intent intent = new Intent(context, A.class);        list.add(getView("A", intent));        Intent intent2 = new Intent(context, B.class);        list.add(getView("B", intent2));        Intent intent3 = new Intent(context, C.class);        list.add(getView("C", intent3));        Intent intent4 = new Intent(context, D.class);        list.add(getView("D", intent4));        pager.setAdapter(new PagerAdapter() {            @Override            public void destroyItem(View arg0, int arg1, Object arg2) {                ViewPager pViewPager = ((ViewPager) arg0);                pViewPager.removeView(list.get(arg1));            }            @Override            public void finishUpdate(View arg0) {            }            @Override            public int getCount() {                return list.size();            }            @Override            public Object instantiateItem(View arg0, int arg1) {                ViewPager pViewPager = ((ViewPager) arg0);                pViewPager.addView(list.get(arg1));                return list.get(arg1);            }            @Override            public boolean isViewFromObject(View arg0, Object arg1) {                return arg0 == arg1;            }            @Override            public void restoreState(Parcelable arg0, ClassLoader arg1) {            }            @Override            public Parcelable saveState() {                return null;            }            @Override            public void startUpdate(View arg0) {            }        });        pager.setOnPageChangeListener(new OnPageChangeListener() {            @Override            public void onPageSelected(int arg0) {                tabHost.setCurrentTab(arg0);            }            @Override            public void onPageScrolled(int arg0, float arg1, int arg2) {            }            @Override            public void onPageScrollStateChanged(int arg0) {            }        });    }    private View getView(String id, Intent intent) {        return manager.startActivity(id, intent).getDecorView();    }}
  相关解决方案