您好,登錄后才能下訂單哦!
本文實例為大家分享了Android模仿微信主界面展示的具體代碼,供大家參考,具體內容如下
先看一下效果圖
實現的原理:
ViewPager+FragmentPagerAdapter
主界面可分為三部分:
這里為了布局的優化,將top和bottom抽取出來 ,然后用include將其導入主布局,如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <include layout="@layout/top" /> <android.support.v4.view.ViewPager android:id="@+id/vp_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </android.support.v4.view.ViewPager> <include layout="@layout/bottom" /> </LinearLayout>
bottom_tab的布局,為了方便管理將它們的共性抽取出來一個style
tab有兩種狀態,選中和非選中,因此將其背景做成一個選擇器selector
<?xml version="1.0" encoding="utf-8"?> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rg_tab_container" android:layout_width="match_parent" android:layout_height="@dimen/bar_height" android:background="@drawable/bottom_bar" android:gravity="center" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_tab_weixin" android:checked="true" android:drawableTop="@drawable/tab_weixin_selector" android:text="@string/tab_weixin" /> <RadioButton android:id="@+id/rb_tab_friend" android:drawableTop="@drawable/tab_friend_selector" android:text="@string/tab_friend" /> <RadioButton android:id="@+id/rb_tab_contact" android:drawableTop="@drawable/tab_contact_selector" android:text="@string/tab_contact" /> <RadioButton android:id="@+id/rb_tab_setting" android:drawableTop="@drawable/tab_setting_selector" android:text="@string/tab_setting" /> </RadioGroup>
最后看一下作為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 List<Fragment> fragments; 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<Fragment>(); 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(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。