Android Tab类型主界面总结

转载至http://blog.csdn.NET/lmj623565791/article/details/24740977 Android现在实现Tab类型的界面方式越来越多,今天就把常见的实现方式给大家来个总结。目前写了:1、传统的ViewPager实现2、FragmentManager+Fragment实现3、ViewPager+FragmentPagerAdapter实现...
摘要由CSDN通过智能技术生成

转载至http://blog.csdn.NET/lmj623565791/article/details/24740977

 

Android现在实现Tab类型的界面方式越来越多,今天就把常见的实现方式给大家来个总结。目前写了:

1、传统的ViewPager实现

2、FragmentManager+Fragment实现

3、ViewPager+FragmentPagerAdapter实现

4、TabPageIndicator+ViewPager+FragmentPagerAdapter

 

1、传统的ViewPager实现

主要就是ViewPager+ViewAdapter这个还是比较常见的,就不多说了

效果图:

 

 

 

代码:

 

package com.example.mainframework02;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.support.v4.view.PagerAdapter;  
import android.support.v4.view.ViewPager;  
import android.support.v4.view.ViewPager.OnPageChangeListener;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.ImageButton;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
  
public class TraditionalViewPagerAcvitity extends Activity  
{  
  
    /** 
     * ViewPager 
     */  
    private ViewPager mViewPager;  
    /** 
     * ViewPager的适配器 
     */  
    private PagerAdapter mAdapter;  
    private List<View> mViews;  
    private LayoutInflater mInflater;  
      
    private int currentIndex;  
  
    /** 
     * 底部四个按钮 
     */  
    private LinearLayout mTabBtnWeixin;  
    private LinearLayout mTabBtnFrd;  
    private LinearLayout mTabBtnAddress;  
    private LinearLayout mTabBtnSettings;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mInflater = LayoutInflater.from(this);  
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);  
          
        /** 
         * 初始化View 
         */  
        initView();  
          
        mViewPager.setAdapter(mAdapter);  
  
        mViewPager.setOnPageChangeListener(new OnPageChangeListener()  
        {  
  
            @Override  
            public void onPageSelected(int position)  
            {  
                resetTabBtn();  
                switch (position)  
                {  
                case 0:  
                    ((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))  
                            .setImageResource(R.drawable.tab_weixin_pressed);  
                    break;  
                case 1:  
                    ((ImageButton) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))  
                            .setImageResource(R.drawable.tab_find_frd_pressed);  
                    break;  
                case 2:  
                    ((ImageButton) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))  
                            .setImageResource(R.drawable.tab_address_pressed);  
                    break;  
                case 3:  
                    ((ImageButton) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))  
                            .setImageResource(R.drawable.tab_settings_pressed);  
                    break;  
                }  
  
                currentIndex = position;  
            }  
  
            @Override  
            public void onPageScrolled(int arg0, float arg1, int arg2)  
            {  
  
            }  
  
            @Override  
            public void onPageScrollStateChanged(int arg0)  
            {  
            }  
        });  
  
    }  
  
    protected void resetTabBtn()  
    {  
        ((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))  
                .setImageResource(R.drawable.tab_weixin_normal);  
        ((ImageButton) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))  
                .setImageResource(R.drawable.tab_find_frd_normal);  
        ((ImageButton) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))  
                .setImageResource(R.drawable.tab_address_normal);  
        ((ImageButton) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))  
                .setImageResource(R.drawable.tab_settings_normal);  
    }  
  
    private void initView()  
    {  
  
        mTabBtnWeixin = (LinearLayout) findViewById(R.id.id_tab_bottom_weixin);  
        mTabBtnFrd = (LinearLayout) findViewById(R.id.id_tab_bottom_friend);  
        mTabBtnAddress = (LinearLayout) findViewById(R.id.id_tab_bottom_contact);  
        mTabBtnSettings = (LinearLayout) findViewById(R.id.id_tab_bottom_setting);  
  
        mViews = new ArrayList<View>();  
        View first = mInflater.inflate(R.layout.main_tab_01, null);  
        View second = mInflater.inflate(R.layout.main_tab_02, null);  
        View third = mInflater.inflate(R.layout.main_tab_03, null);  
        View fourth = mInflater.inflate(R.layout.main_tab_04, null);  
        mViews.add(first);  
        mViews.add(second);  
        mViews.add(third);  
        mViews.add(fourth);  
  
        mAdapter = new PagerAdapter()  
        {  
            @Override  
            public void destroyItem(ViewGroup container, int position, Object object)  
            {  
                container.removeView(mViews.get(position));  
            }  
  
            @Override  
            public Object instantiateItem(ViewGroup container, int position)  
            {  
                View view = mViews.get(position);  
                container.addView(view);  
                return view;  
            }  
  
            @Override  
            public boolean isViewFromObject(View arg0, Object arg1)  
            {  
                return arg0 == arg1;  
            }  
  
            @Override  
            public int getCount()  
            {  
                return mView
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值