android主页框架FragmentTabHost+ViewPager,左右滑动

先看图android主页框架FragmentTabHost+ViewPager,左右滑动_第1张图片android主页框架FragmentTabHost+ViewPager,左右滑动_第2张图片android主页框架FragmentTabHost+ViewPager,左右滑动_第3张图片

主要代码

package com.example;

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

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.example.CartFragment;
import com.example.CategoryFragment;
import com.example.HomeFragment;
import com.example.HotFragment;
import com.example.MineFragment;
import com.example.R;

public class MainActivity extends FragmentActivity {
    private static final String TAG = "MainActivity";
    private FragmentTabHost tabHost;
    private String currentTabId;
    private LayoutInflater inflate;
    private int mImageViewArray[] = {R.drawable.selector_icon_home,
            R.drawable.selector_icon_hot, R.drawable.selector_icon_category,
            R.drawable.selector_icon_cart, R.drawable.selector_icon_mine};

    private String mTextviewArray[] = {MyApplication.getContext().getResources().getString(R.string.home),
            MyApplication.getContext().getResources().getString(R.string.hot),
            MyApplication.getContext().getResources().getString(R.string.catagory),
            MyApplication.getContext().getResources().getString(R.string.cart),
            MyApplication.getContext().getResources().getString(R.string.mine),
    };


    private int itemTextArry[] = {R.string.home, R.string.hot, R.string.catagory,
            R.string.cart, R.string.mine};

    private HomeFragment mHomeFragment = new HomeFragment();
    private HotFragment mHotFragment = new HotFragment();
    private CategoryFragment mCategoryFragment = new CategoryFragment();
    private CartFragment mCartFragment = new CartFragment();
    private MineFragment mMineFragment = new MineFragment();
    private View tab[] = new View[5];

    // 定义数组来存放Fragment界面
    private Class fragmentArray[] = {HomeFragment.class, HotFragment.class,
            CategoryFragment.class, CartFragment.class, MineFragment.class};
    private Fragment fragmentPage[] = {mHomeFragment, mHotFragment,
            mCategoryFragment, mCartFragment, mMineFragment};

    private ViewPager viewPager;
    private List fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTabHost();
        initViewPager();

    }

    private void initTabHost() {
        fragmentList = new ArrayList();
        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(),
                android.R.id.tabcontent);
        // 实例化视图
        inflate = LayoutInflater.from(this);
        int count = mTextviewArray.length;

        for (int i = 0; i < count; i++) {
            // 获取图片
            tab[i] = getTabItemView(i);
            // 添加fragmentPageList            fragmentList.add(fragmentPage[i]);
            // 为每一个Tab按钮设置图标、文字和内容并添加进Tab选项卡中
            tabHost.addTab(
                    tabHost.newTabSpec(mTextviewArray[i]).setIndicator(tab[i]),
                    fragmentArray[i], null);

            // 设置第一个按钮被选中
            currentTabId = mTextviewArray[0];


            // 设置去间隔线
            tabHost.getTabWidget().setDividerDrawable(null);
            tabHost.setOnTabChangedListener(new OnTabChangeListener() {
                @Override
                public void onTabChanged(String tabId) {
                    if (!currentTabId.equals(tabId)) {

                        currentTabId = tabId;
                    }
                    viewPager.setCurrentItem(tabHost.getCurrentTab());
                }
            });
        }
    }

    private void initViewPager() {
        viewPager = (ViewPager) findViewById(R.id.itemViewPager);
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                tabHost.setCurrentTab(arg0);
            }

        });

        viewPager.setAdapter(new FragmentViewPagerAdapter(
                getSupportFragmentManager(), fragmentList));
        viewPager.setOffscreenPageLimit(3);
    }

    /**
     * Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = inflate.inflate(R.layout.tab_item_view, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray[index]);

        TextView textView = (TextView) view.findViewById(R.id.textview);
        textView.setText(itemTextArry[index]);

        //选中设置
        textView.setSelected(true);
        //不选中设置
        textView.setSelected(false);
        return view;
    }
}
源码下载

http://download.csdn.net/download/qq_31939617/9927729

你可能感兴趣的:(android)