标签栏主界面实现(二)

本文介绍如何使用 ViewPager 和 PagerAdapter 实现带标签栏的主界面。通过代码示例展示了初始化视图、设置适配器及监听器的过程,并解释了如何在页面切换时更新标签栏的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ViewPager + View + PagerAdapter 实现标签栏主界面:

效果图:


代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.mainframework02;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.support.v4.view.PagerAdapter;  
  9. import android.support.v4.view.ViewPager;  
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.widget.ImageButton;  
  15. import android.widget.ImageView;  
  16. import android.widget.LinearLayout;  
  17.   
  18. public class TraditionalViewPagerAcvitity extends Activity  
  19. {  
  20.   
  21.     /** 
  22.      * ViewPager 
  23.      */  
  24.     private ViewPager mViewPager;  
  25.     /** 
  26.      * ViewPager的适配器 
  27.      */  
  28.     private PagerAdapter mAdapter;  
  29.     private List<View> mViews;  
  30.     private LayoutInflater mInflater;  
  31.       
  32.     private int currentIndex;  
  33.   
  34.     /** 
  35.      * 底部四个按钮 
  36.      */  
  37.     private LinearLayout mTabBtnWeixin;  
  38.     private LinearLayout mTabBtnFrd;  
  39.     private LinearLayout mTabBtnAddress;  
  40.     private LinearLayout mTabBtnSettings;  
  41.   
  42.     @Override  
  43.     protected void onCreate(Bundle savedInstanceState)  
  44.     {  
  45.         super.onCreate(savedInstanceState);  
  46.         setContentView(R.layout.activity_main);  
  47.         mInflater = LayoutInflater.from(this);  
  48.         mViewPager = (ViewPager) findViewById(R.id.id_viewpager);  
  49.           
  50.         /** 
  51.          * 初始化View 
  52.          */  
  53.         initView();  
  54.           
  55.         mViewPager.setAdapter(mAdapter);  
  56.   
  57.         mViewPager.setOnPageChangeListener(new OnPageChangeListener()  
  58.         {  
  59.   
  60.             @Override  
  61.             public void onPageSelected(int position)  
  62.             {  
  63.                 resetTabBtn();  
  64.                 switch (position)  
  65.                 {  
  66.                 case 0:  
  67.                     ((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))  
  68.                             .setImageResource(R.drawable.tab_weixin_pressed);  
  69.                     break;  
  70.                 case 1:  
  71.                     ((ImageButton) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))  
  72.                             .setImageResource(R.drawable.tab_find_frd_pressed);  
  73.                     break;  
  74.                 case 2:  
  75.                     ((ImageButton) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))  
  76.                             .setImageResource(R.drawable.tab_address_pressed);  
  77.                     break;  
  78.                 case 3:  
  79.                     ((ImageButton) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))  
  80.                             .setImageResource(R.drawable.tab_settings_pressed);  
  81.                     break;  
  82.                 }  
  83.   
  84.                 currentIndex = position;  
  85.             }  
  86.   
  87.             @Override  
  88.             public void onPageScrolled(int arg0, float arg1, int arg2)  
  89.             {  
  90.   
  91.             }  
  92.   
  93.             @Override  
  94.             public void onPageScrollStateChanged(int arg0)  
  95.             {  
  96.             }  
  97.         });  
  98.   
  99.     }  
  100.   
  101.     protected void resetTabBtn()  
  102.     {  
  103.         ((ImageButton) mTabBtnWeixin.findViewById(R.id.btn_tab_bottom_weixin))  
  104.                 .setImageResource(R.drawable.tab_weixin_normal);  
  105.         ((ImageButton) mTabBtnFrd.findViewById(R.id.btn_tab_bottom_friend))  
  106.                 .setImageResource(R.drawable.tab_find_frd_normal);  
  107.         ((ImageButton) mTabBtnAddress.findViewById(R.id.btn_tab_bottom_contact))  
  108.                 .setImageResource(R.drawable.tab_address_normal);  
  109.         ((ImageButton) mTabBtnSettings.findViewById(R.id.btn_tab_bottom_setting))  
  110.                 .setImageResource(R.drawable.tab_settings_normal);  
  111.     }  
  112.   
  113.     private void initView()  
  114.     {  
  115.   
  116.         mTabBtnWeixin = (LinearLayout) findViewById(R.id.id_tab_bottom_weixin);  
  117.         mTabBtnFrd = (LinearLayout) findViewById(R.id.id_tab_bottom_friend);  
  118.         mTabBtnAddress = (LinearLayout) findViewById(R.id.id_tab_bottom_contact);  
  119.         mTabBtnSettings = (LinearLayout) findViewById(R.id.id_tab_bottom_setting);  
  120.   
  121.         mViews = new ArrayList<View>();  
  122.         View first = mInflater.inflate(R.layout.main_tab_01, null);  
  123.         View second = mInflater.inflate(R.layout.main_tab_02, null);  
  124.         View third = mInflater.inflate(R.layout.main_tab_03, null);  
  125.         View fourth = mInflater.inflate(R.layout.main_tab_04, null);  
  126.         mViews.add(first);  
  127.         mViews.add(second);  
  128.         mViews.add(third);  
  129.         mViews.add(fourth);  
  130.   
  131.         mAdapter = new PagerAdapter()  
  132.         {  
  133.             @Override  
  134.             public void destroyItem(ViewGroup container, int position, Object object)  
  135.             {  
  136.                 container.removeView(mViews.get(position));  
  137.             }  
  138.   
  139.             @Override  
  140.             public Object instantiateItem(ViewGroup container, int position)  
  141.             {  
  142.                 View view = mViews.get(position);  
  143.                 container.addView(view);  
  144.                 return view;  
  145.             }  
  146.   
  147.             @Override  
  148.             public boolean isViewFromObject(View arg0, Object arg1)  
  149.             {  
  150.                 return arg0 == arg1;  
  151.             }  
  152.   
  153.             @Override  
  154.             public int getCount()  
  155.             {  
  156.                 return mViews.size();  
  157.             }  
  158.         };  
  159.     }  
  160.   
  161. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值