滑动菜单+Tab页

详细可以查看滑动菜单博文,除了标记的,其他都基本一样。

滑动菜单+Tab页

滑动菜单+Tab页

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

    <!--
         <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0" />
    -->

    <view
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        class="com.heima.news.view.LazyViewPager"
         />
    <!-- </FrameLayout> -->

    <RadioGroup
        android:id="@+id/main_radio"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        android:paddingTop="2dp"
        android:gravity="center_vertical"
        android:background="@drawable/bottom_tab_bg"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rb_function"
            style="@style/main_tab_bottom"
            android:drawableTop="@drawable/icon_function"
            android:text="@string/tab_function" />


        <RadioButton
            android:id="@+id/rb_news_center"
            style="@style/main_tab_bottom"
            android:drawableTop="@drawable/icon_newscenter"
            android:text="@string/tab_news_center" />


        <RadioButton
            android:id="@+id/rb_smart_service"
            style="@style/main_tab_bottom"
            android:drawableTop="@drawable/icon_smartservice"
            android:text="@string/tab_smart_service" />


        <RadioButton
            android:id="@+id/rb_gov_affairs"
            style="@style/main_tab_bottom"
            android:drawableTop="@drawable/icon_govaffairs"
            android:text="@string/tab_gov_affairs" />


        <RadioButton
            android:id="@+id/rb_setting"
            style="@style/main_tab_bottom"
            android:drawableTop="@drawable/icon_setting"
            android:text="@string/tab_setting" />
    </RadioGroup>

</LinearLayout>



package com.heima.news.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.heima.news.R;
import com.heima.news.base.BaseFragment;
import com.heima.news.base.BasePage;
import com.heima.news.home.FunctionPage;
import com.heima.news.home.GovAffairsPage;
import com.heima.news.home.NewsCenterPage;
import com.heima.news.home.SettingPage;
import com.heima.news.home.SmartServicePage;
import com.heima.news.view.LazyViewPager;
import com.heima.news.view.LazyViewPager.OnPageChangeListener;

public class HomeFragment extends BaseFragment {
	/**
	 * 1 初始化viewpager 通过使用adapter的形式去实现
	 */
	private View view;
	private LazyViewPager viewpager;
	private RadioGroup main_radio;
	private int checkedId = R.id.rb_function;

	@Override
	public View initView(LayoutInflater inflater) {
		view = inflater.inflate(R.layout.frag_home2, null);
		//中间显示的区域
		viewpager = (LazyViewPager) view.findViewById(R.id.viewpager);
		main_radio = (RadioGroup) view.findViewById(R.id.main_radio);
		return view;
	}

	List<BasePage> list = new ArrayList<BasePage>();;

	@Override
	public void initData(Bundle savedInstanceState) {
		
		list.add(new FunctionPage(ct));
		list.add(new NewsCenterPage(ct));
		list.add(new SmartServicePage(ct));
		list.add(new GovAffairsPage(ct));
		list.add(new SettingPage(ct));
		HomePageAdapter adapter = new HomePageAdapter(ct, list);
		viewpager.setAdapter(adapter);
		

		main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_function:
					viewpager.setCurrentItem(0, false);
					checkedId = 0;
					break;

				case R.id.rb_news_center:
					viewpager.setCurrentItem(1, false);
					checkedId = 1;
					break;
				case R.id.rb_smart_service:
					viewpager.setCurrentItem(2, false);
					checkedId = 2;
					break;
				case R.id.rb_gov_affairs:
					viewpager.setCurrentItem(3, false);
					checkedId = 3;
					break;
				case R.id.rb_setting:
					viewpager.setCurrentItem(4, false);
					checkedId = 4;
					break;
				}

			}
		});
		main_radio.check(checkedId);
	}

	class HomePageAdapter extends PagerAdapter {
		private Context ct;
		private List<BasePage> list;

		public HomePageAdapter(Context ct, List<BasePage> list) {
			this.ct = ct;
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
//			super.destroyItem(container, position, object);
			((LazyViewPager) container).removeView(list.get(position)
					.getRootView());

		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			((LazyViewPager) container).addView(list.get(position)
					.getRootView(), 0);
			return list.get(position).getRootView();
		}

	}
}



你可能感兴趣的:(滑动菜单+Tab页)