您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關ViewPager+RadioGroup如何仿微信實現主界面,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
如圖效果,可以點擊底部按鈕選擇頁面,也可以滑動頁面進行選擇。
那么我們下面就來實現一下吧。
首先創建工程WXTest,主界面布局使用ViewPager+RadioGroup來實現,界面代碼如下。
<?xml version="1.0" encoding="utf-8"?> <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" > <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </android.support.v4.view.ViewPager> <TextView android:id="@+id/division_line" android:layout_width="match_parent" android:layout_height="1dp" android:background="#333333" android:layout_above="@+id/rg_contain" /> <RadioGroup android:id="@+id/rg_contain" android:layout_width="match_parent" android:layout_height="48dp" android:layout_alignParentBottom="true" android:gravity="center" android:background="#ffffff" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_wx" android:checked="true" android:text="微信" android:drawableTop="@drawable/tab_radiobutton_wx_selector" android:textColor="@color/txt_tab_selector" /> <RadioButton android:id="@+id/rb_book" android:checked="true" android:text="通訊錄" android:drawableTop="@drawable/tab_radiobutton_book_selector" android:textColor="@color/txt_tab_selector" /> <RadioButton android:id="@+id/rb_discover" android:checked="true" android:text="發現" android:drawableTop="@drawable/tab_radiobutton_discover_selector" android:textColor="@color/txt_tab_selector" /> <RadioButton android:drawableTop="@drawable/tab_radiobutton_me_selector" android:id="@+id/rb_me" android:checked="true" android:text="我" android:textColor="@color/txt_tab_selector" /> </RadioGroup> </LinearLayout>
在MainActivity中進行初始化數據
mViewPager = (ViewPager) findViewById(R.id.view_pager); mRGContain = (RadioGroup) findViewById(R.id.rg_contain); mRBWX = (RadioButton) findViewById(R.id.rb_wx); mRBBook = (RadioButton) findViewById(R.id.rb_book); mRBDiscover = (RadioButton) findViewById(R.id.rb_discover); mRBMe = (RadioButton) findViewById(R.id.rb_me); //給圖標設置大小 Drawable drawableRbWX = mRBWX.getCompoundDrawables()[1]; drawableRbWX.setBounds(0,0, dp2px(24),dp2px(24)); mRBWX.setCompoundDrawables(null,drawableRbWX,null,null); Drawable drawableRbBook = mRBBook.getCompoundDrawables()[1]; drawableRbBook.setBounds(0,0, dp2px(24), dp2px(24)); mRBBook.setCompoundDrawables(null,drawableRbBook,null,null); Drawable drawableRbDiscover = mRBDiscover.getCompoundDrawables()[1]; drawableRbDiscover.setBounds(0,0, dp2px(24), dp2px(24)); mRBDiscover.setCompoundDrawables(null,drawableRbDiscover,null,null); Drawable drawableRbMe = mRBMe.getCompoundDrawables()[1]; drawableRbMe.setBounds(0,0, dp2px(24), dp2px(24)); mRBMe.setCompoundDrawables(null,drawableRbMe,null,null); /** * dp 轉 px * @param dp * @return */ public int dp2px(int dp) { // 1、獲取屏幕密度 float density = getApplicationContext().getResources().getDisplayMetrics().density; // 2、進行乘法操作 return (int) (dp * density + 0.5); }
給ViewPager設置數據我們使用FragmentPagerAdapter ,所以MainActivity需要繼承FragmentActivity。Fragment選擇V4包,可以兼容低版本。
新建BaseFragment繼承Fragment,讓子類實現initView()方法。
public abstract class BaseFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return initView(inflater,container,savedInstanceState); } public abstract View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState); }
微信界面
public class WXFragment extends BaseFragment { @Override public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.wx_fragment, container,false); return view; } }
wx_fragment.xml代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" android:text="微信" android:textSize="15dp" /> </LinearLayout>
其余三個界面類似
/** * 通訊錄界面 */ public class BookFragment extends BaseFragment { @Override public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.book_fragment, container,false); return view; } } /** * 發現界面 */ public class DiscoverFragment extends BaseFragment { @Override public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.discover_fragment, container,false); return view; } } /** * 我界面 */ public class MeFragment extends BaseFragment { @Override public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.me_fragment, container,false); return view; } }
然后使用MainFragmentFactory來存儲頁面Fragment
package com.example.wxtest.fragment; import java.util.HashMap; public class MainFragmentFactory { private static HashMap<Integer, BaseFragment> mSavedFragment = new HashMap<Integer, BaseFragment>(); //根據position得到對應的fragment public static BaseFragment getFragment(int position) { BaseFragment fragment = mSavedFragment.get(position); if(fragment == null) { switch (position) { case 0: fragment = new WXFragment(); break; case 1: fragment = new BookFragment(); break; case 2: fragment = new DiscoverFragment(); break; case 3: fragment = new MeFragment(); break; } //創建之后保存 mSavedFragment.put(position, fragment); } return fragment; } public static void deleteFragment(){ for (int i = 0 ; i < 4 ; i++){ mSavedFragment.remove(i); } } }
適配器
/** * 適配器 */ class MainFragmentAdapter extends FragmentPagerAdapter { public MainFragmentAdapter(FragmentManager fm) { super(fm); } //根據position的值 返回對應的fragment對象 @Override public Fragment getItem(int position) { Fragment fragment = MainFragmentFactory.getFragment(position); return fragment; } //返回ViewPager要顯示的item數量 @Override public int getCount() { return 4; } }
ViewPager設置數據
//給ViewPager設置數據 mViewPager.setAdapter(new MainFragmentAdapter(getSupportFragmentManager()));
ViewPager頁面選中監聽
//界面改變監聽 mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } //當前選中頁面 @Override public void onPageSelected(int position) { switch (position){ case 0: mRGContain.check(R.id.rb_wx); break; case 1: mRGContain.check(R.id.rb_book); break; case 2: mRGContain.check(R.id.rb_discover); break; case 3: mRGContain.check(R.id.rb_me); break; default: break; } } @Override public void onPageScrollStateChanged(int state) { } });
RadioGroup選中監聽
//RadioGroup選中監聽 mRGContain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.rb_wx: //微信 mViewPager.setCurrentItem(0,false); break; case R.id.rb_book: //通訊錄 mViewPager.setCurrentItem(1,false); break; case R.id.rb_discover: //發現 mViewPager.setCurrentItem(2,false); break; case R.id.rb_me: //我 mViewPager.setCurrentItem(3,false); break; default: break; } } }); //選中微信界面 mRGContain.check(R.id.rb_wx);
然后運行程序,就可以實現圖中的效果了!
關于“ViewPager+RadioGroup如何仿微信實現主界面”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。