重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本文实例为大家分享了Android模仿微信主界面展示的具体代码,供大家参考,具体内容如下
创新互联建站主要业务有网站营销策划、网站制作、成都网站制作、微信公众号开发、微信小程序定制开发、H5网站设计、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、网络营销推广资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。
先看一下效果图
实现的原理:
ViewPager+FragmentPagerAdapter
主界面可分为三部分:
这里为了布局的优化,将top和bottom抽取出来 ,然后用include将其导入主布局,如下
bottom_tab的布局,为了方便管理将它们的共性抽取出来一个style
tab有两种状态,选中和非选中,因此将其背景做成一个选择器selector
<?xml version="1.0" encoding="utf-8"?>
最后看一下作为Controller的Activity
/** * 主界面 模仿微信界面 * * @author admin * */ public class MainActivity extends FragmentActivity { /** tab */ private RadioGroup mRadioGroup; private RadioButton weixinRadioButton; private RadioButton friendRadioButton; private RadioButton contactRadioButton; private RadioButton settingRadioButton; /** 显示内容容器 */ private ViewPager mViewPager; private Listfragments; private Fragment mWeiXinFragment; private Fragment mFriendFragment; private Fragment mContactFragment; private Fragment mSettingFragment; private FragmentPagerAdapter mFragmentPagerAdapter; private final static int TAB_WEIXIN = 0; private final static int TAB_FRIEND = 1; private final static int TAB_CONTACT = 2; private final static int TAB_SETTING = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); initEvent(); } private void initEvent() { mRadioGroup.setOnCheckedChangeListener(myCheckedChangeListener); mViewPager.setOnPageChangeListener(myOnPageChangeListener); } private void initView() { mRadioGroup = (RadioGroup) findViewById(R.id.rg_tab_container); weixinRadioButton = (RadioButton) findViewById(R.id.rb_tab_weixin); friendRadioButton = (RadioButton) findViewById(R.id.rb_tab_friend); contactRadioButton = (RadioButton) findViewById(R.id.rb_tab_contact); settingRadioButton = (RadioButton) findViewById(R.id.rb_tab_setting); mViewPager = (ViewPager) findViewById(R.id.vp_content); fragments = new ArrayList (); mWeiXinFragment = new WeiXinFragment(); mFriendFragment = new FriendFragment(); mContactFragment = new ContactFragment(); mSettingFragment = new SettingFragment(); fragments.add(mWeiXinFragment); fragments.add(mFriendFragment); fragments.add(mContactFragment); fragments.add(mSettingFragment); //这里需要继承FragmentActivity mFragmentPagerAdapter = new FragmentAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mFragmentPagerAdapter); } /** * 当页面切换时,tab也跟着切换 */ private OnPageChangeListener myOnPageChangeListener = new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { switch (arg0) { case TAB_WEIXIN: if (!weixinRadioButton.isChecked()) { weixinRadioButton.setChecked(true); } break; case TAB_FRIEND: friendRadioButton.setChecked(true); break; case TAB_CONTACT: contactRadioButton.setChecked(true); break; case TAB_SETTING: settingRadioButton.setChecked(true); break; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }; /** * 点击tab,切换到相应的页面 */ private OnCheckedChangeListener myCheckedChangeListener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb_tab_weixin: mViewPager.setCurrentItem(TAB_WEIXIN); break; case R.id.rb_tab_friend: mViewPager.setCurrentItem(TAB_FRIEND); break; case R.id.rb_tab_contact: mViewPager.setCurrentItem(TAB_CONTACT); break; case R.id.rb_tab_setting: mViewPager.setCurrentItem(TAB_SETTING); break; } } }; private class FragmentAdapter extends FragmentPagerAdapter { public FragmentAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { return fragments.get(arg0); } @Override public int getCount() { return fragments.size(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。