zoukankan      html  css  js  c++  java
  • Tabhost 和 viewpager的结合使用

    一:布局

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <android.support.v4.view.ViewPager
            android:id="@+id/center_body_view_pagers"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </android.support.v4.view.ViewPager>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <!-- 隐藏的TabWidget,visibility="gone" -->
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />
    
            <!-- 标签内容 -->
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1" />
    
            <RelativeLayout
                android:id="@+id/relative_bottom"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="fill_horizontal" >
    
                <RadioGroup
                    android:id="@+id/tab_group"
                    android:layout_width="fill_parent"
                    android:layout_height="60dip"
                    android:background="@drawable/list_bottombar_bg"
                    android:gravity="bottom|center_horizontal"
                    android:orientation="horizontal" >
    
                    <RadioButton
                        android:id="@+id/local"
                        android:layout_width="105dip"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:background="@drawable/local_bottom"
                        android:button="@null"
                        android:clickable="true"
                        android:focusable="true" >
                    </RadioButton>
    
                   
                      <RadioButton
                        android:id="@+id/online"
                        android:layout_width="105dip"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:background="@drawable/net_buttom"
                        android:button="@null" />
                </RadioGroup>
            </RelativeLayout>
        </LinearLayout>
    
        
    
    </TabHost>

    二:activity

    package com.ct.app.qqmusicplayer;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.LocalActivityManager;
    import android.app.TabActivity;
    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.view.ViewGroup;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TabHost;
    
    public class TabHostMainActivity extends TabActivity implements
        OnCheckedChangeListener{
        TabHost tabhost;
    
        private RadioGroup maintab;
    
        // 页卡内容
        private ViewPager mPager;
        // Tab页面列表
        private List<View> listViews;
        // 当前页卡编号
        private LocalActivityManager manager = null;
         
        private MyPagerAdapter mpAdapter = null;
          int index;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tabhost_list);
            maintab = (RadioGroup) findViewById(R.id.tab_group);
            maintab.setOnCheckedChangeListener(this);// 设置RadioGroup监听器
            tabhost = getTabHost();
            mPager = (ViewPager) findViewById(R.id.center_body_view_pagers);
            manager = new LocalActivityManager(this, true);
            manager.dispatchCreate(savedInstanceState);
            InitViewPager();
            }
        
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.local:
                index=0;
                //tabhost.setCurrentTabByTag("localmusic");
                listViews.set(0, getView("A", new Intent(TabHostMainActivity.this, MusicListActivity.class)));
                mpAdapter.notifyDataSetChanged();
                mPager.setCurrentItem(0);
                break;
            
            case R.id.online:
                index=1;
                //tabhost.setCurrentTabByTag("netmusic");
                listViews.set(1, getView("B", new Intent(TabHostMainActivity.this, MusicOnlineActivity.class)));
                mpAdapter.notifyDataSetChanged();
                mPager.setCurrentItem(1);
                break;
            }
    
        }
        /***初始化滑动**/
        private void InitViewPager() {
            Intent intent = null;
            listViews = new ArrayList<View>();
            mpAdapter = new MyPagerAdapter(listViews);
            intent = new Intent(TabHostMainActivity.this, MusicListActivity.class);
            listViews.add(getView("A", intent));
           
            intent = new Intent(TabHostMainActivity.this, MusicOnlineActivity.class);
            listViews.add(getView("B", intent));
            mPager.setOffscreenPageLimit(0);
            mPager.setAdapter(mpAdapter);
            mPager.setCurrentItem(0);
            mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }
        public class MyPagerAdapter extends PagerAdapter{
            public List<View> mListViews;
             
            public MyPagerAdapter(List<View> mListViews) {
                    this.mListViews = mListViews;
            }
            @Override
            public int getCount() {
                return mListViews.size();
            }
    
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == (arg1);
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ((ViewPager) container).removeView(mListViews.get(position));
            }
            @Override
            public void finishUpdate(ViewGroup container) {
            
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                 ((ViewPager) container).addView(mListViews.get(position), 0);
                 return mListViews.get(position);
            }
            @Override
            public Parcelable saveState() {
                return null;
            }
            @Override
            public void startUpdate(ViewGroup container) {
            
            }
            
        }
         public class MyOnPageChangeListener implements OnPageChangeListener {
    
            @Override
            public void onPageScrollStateChanged(int arg0) {
                
            }
    
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                
            }
    
            @Override
            public void onPageSelected(int pos) {
                  manager.dispatchResume();
                  switch (pos) {
                case 0:
                     index = 0;
                     maintab.check(R.id.local);
                    listViews.set(0, getView("A", new Intent(TabHostMainActivity.this, MusicListActivity.class)));
                    mpAdapter.notifyDataSetChanged();    
                    break;
    
                case 1:
                    index=1;
                    maintab.check(R.id.online);
                    listViews.set(1, getView("B", new Intent(TabHostMainActivity.this, MusicOnlineActivity.class)));
                    mpAdapter.notifyDataSetChanged();
                    mPager.setCurrentItem(1);
                    break;
                }
                
            }
             
         }
         private View getView(String id, Intent intent) {
             return manager.startActivity(id, intent).getDecorView();
     }
        
        
    
    }

    (在:F:\java\TabHost和Viewpaper的结合使用)

  • 相关阅读:
    Linux性能监控
    程序员技术练级攻略
    使用 GDB 调试多进程程序
    nginx下面部署fast-cgi和C++【原】
    ROS Learning-024 (提高篇-002) rviz的安装和使用
    ROS Learning-023 (提高篇-001) 准备工作 --- 安装一些必要的软件包
    STM32 C++编程 005 I2c(Soft)类
    Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫
    设置 PyCharm 软件中 Terminal 窗口 中启动的 python 交互命令的版本
    在PyCharm 软件中设置你的项目 使用的Python版本
  • 原文地址:https://www.cnblogs.com/ct732003684/p/2850695.html
Copyright © 2011-2022 走看看