首页 > 代码库 > tab界面实现方式之ViewPager+FragmentPager
tab界面实现方式之ViewPager+FragmentPager
这次的实现的目的其实跟viewpager+view实现的功能一样的,唯一不同的是:这里的viewpager加载的是Fragment。
其中xml代码与之前的完全一样,所以这里就不贴了。这里我只贴下MainActivity的代码
MainActivity代码
java代码
1 package com.example.android_tab3; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.example.Fragment.AddressFragment; 7 import com.example.Fragment.FriendFragment; 8 import com.example.Fragment.SettingFragment; 9 import com.example.Fragment.WeixinFragment; 10 11 import android.os.Bundle; 12 import android.support.v4.app.Fragment; 13 import android.support.v4.app.FragmentActivity; 14 import android.support.v4.app.FragmentPagerAdapter; 15 import android.support.v4.view.ViewPager; 16 import android.support.v4.view.ViewPager.OnPageChangeListener; 17 import android.util.Log; 18 import android.view.View; 19 import android.view.View.OnClickListener; 20 import android.view.Window; 21 import android.widget.ImageButton; 22 import android.widget.LinearLayout; 23 24 public class MainActivity extends FragmentActivity implements OnClickListener{ 25 private ViewPager viewpager = null; 26 private List<Fragment> list = null; 27 private FragmentPagerAdapter adapter = null; 28 private ImageButton imagebutton_weixin = null; 29 private ImageButton imagebutton_friend = null; 30 private ImageButton imagebutton_address = null; 31 private ImageButton imagebutton_setting = null; 32 33 private LinearLayout linear_weixin = null; 34 private LinearLayout linear_friend = null; 35 private LinearLayout linear_address = null; 36 private LinearLayout linear_setting = null; 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 requestWindowFeature(Window.FEATURE_NO_TITLE); 40 setContentView(R.layout.activity_main); 41 initview(); 42 initEvents(); 43 } 44 45 private void initview() 46 { 47 viewpager = (ViewPager) findViewById(R.id.viewpager); 48 49 imagebutton_weixin = (ImageButton) findViewById(R.id.imagebutton_weixin); 50 imagebutton_friend = (ImageButton) findViewById(R.id.imagebutton_friend); 51 imagebutton_address = (ImageButton) findViewById(R.id.imagebutton_address); 52 imagebutton_setting = (ImageButton) findViewById(R.id.imagebutton_setting); 53 54 linear_weixin = (LinearLayout) findViewById(R.id.linear_weixin); 55 linear_friend = (LinearLayout) findViewById(R.id.linear_friend); 56 linear_address = (LinearLayout) findViewById(R.id.linear_address); 57 linear_setting = (LinearLayout) findViewById(R.id.linear_setting); 58 list = new ArrayList<Fragment>(); 59 Fragment weixinFragment = new WeixinFragment(); 60 Fragment friendFragment = new FriendFragment(); 61 Fragment addressFragment = new AddressFragment(); 62 Fragment settingFragment = new SettingFragment(); 63 list.add(weixinFragment); 64 list.add(friendFragment); 65 list.add(addressFragment); 66 list.add(settingFragment); 67 adapter = new FragmentPagerAdapter(getSupportFragmentManager()) { 68 69 public int getCount() { 70 71 return list.size(); 72 } 73 74 public Fragment getItem(int position) { 75 76 return list.get(position); 77 } 78 }; 79 viewpager.setAdapter(adapter); 80 viewpager.setOnPageChangeListener(new OnPageChangeListener() { 81 82 @Override 83 public void onPageSelected(int position) { 84 viewpager.setCurrentItem(position); 85 resetimage(); 86 if(position == 0) 87 { 88 imagebutton_weixin.setImageResource(R.drawable.tab_weixin_pressed); 89 } 90 else if(position == 1) 91 { 92 imagebutton_friend.setImageResource(R.drawable.tab_find_frd_pressed); 93 } 94 else if(position == 2) 95 { 96 imagebutton_address.setImageResource(R.drawable.tab_address_pressed); 97 } 98 else if (position == 3) 99 {100 imagebutton_setting.setImageResource(R.drawable.tab_settings_pressed);101 }102 }103 104 @Override105 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {106 // TODO Auto-generated method stub107 108 }109 110 @Override111 public void onPageScrollStateChanged(int state) {112 // TODO Auto-generated method stub113 114 }115 });116 }117 private void initEvents() {118 linear_weixin.setOnClickListener(this);119 Log.i("main", "2");120 linear_friend.setOnClickListener(this);121 linear_address.setOnClickListener(this);122 linear_setting.setOnClickListener(this);123 }124 public void onClick(View v) {125 resetimage();126 switch(v.getId())127 {128 case R.id.linear_weixin:129 {130 viewpager.setCurrentItem(0);131 imagebutton_weixin.setImageResource(R.drawable.tab_weixin_pressed);132 break;133 }134 case R.id.linear_friend:135 {136 viewpager.setCurrentItem(1);137 imagebutton_friend.setImageResource(R.drawable.tab_find_frd_pressed);138 break;139 }140 case R.id.linear_address:141 {142 viewpager.setCurrentItem(2);143 imagebutton_address.setImageResource(R.drawable.tab_address_pressed);144 break;145 }146 case R.id.linear_setting:147 {148 viewpager.setCurrentItem(3);149 imagebutton_setting.setImageResource(R.drawable.tab_settings_pressed);150 break;151 }152 }153 }154 private void resetimage()155 {156 imagebutton_weixin.setImageResource(R.drawable.tab_weixin_normal);157 imagebutton_friend.setImageResource(R.drawable.tab_find_frd_normal);158 imagebutton_address.setImageResource(R.drawable.tab_address_normal);159 imagebutton_setting.setImageResource(R.drawable.tab_settings_normal);160 }161 }
tab界面实现方式之ViewPager+FragmentPager
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。