本文實(shí)例為大家分享了Android模仿微信主界面展示的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)建站主要業(yè)務(wù)有網(wǎng)站營銷策劃、網(wǎng)站制作、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、微信小程序定制開發(fā)、H5網(wǎng)站設(shè)計(jì)、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過程中,公司還積累了豐富的行業(yè)經(jīng)驗(yàn)、網(wǎng)絡(luò)營銷推廣資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。
先看一下效果圖
實(shí)現(xiàn)的原理:
ViewPager+FragmentPagerAdapter
主界面可分為三部分:
這里為了布局的優(yōu)化,將top和bottom抽取出來 ,然后用include將其導(dǎo)入主布局,如下
bottom_tab的布局,為了方便管理將它們的共性抽取出來一個(gè)style
tab有兩種狀態(tài),選中和非選中,因此將其背景做成一個(gè)選擇器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; /** 顯示內(nèi)容容器 */ 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); } /** * 當(dāng)頁面切換時(shí),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) { } }; /** * 點(diǎn)擊tab,切換到相應(yīng)的頁面 */ 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(); } } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。