ViewPager+RadioGroup+Fragment超高仿微信主界面,可通过左右滑动或点击底部RadioButton切换Fragment,废话不多说,上源码。
下载源码地址:http://download.csdn.net/detail/shenyuanqing/8847959
界面:
MainActivity.java
- package com.example.administrator.first.activity;
-
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.view.ViewPager;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
-
- import com.example.administrator.first.R;
- import com.example.administrator.first.adapter.myFragmentPagerAdapter;
- import com.example.administrator.first.fragment.ContactsFragment;
- import com.example.administrator.first.fragment.DiscoveryFragment;
- import com.example.administrator.first.fragment.MeFragment;
- import com.example.administrator.first.fragment.WeChatFragment;
-
- import java.util.ArrayList;
-
-
- public class MainActivity extends FragmentActivity{
- private ViewPager mPager;
- private RadioGroup mGroup;
- private RadioButton rbChat,rbContacts,rbDiscovery,rbMe;
- private ArrayList<Fragment> fragmentList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- initView();
-
- initViewPager();
- }
-
- private void initView(){
- mPager=(ViewPager)findViewById(R.id.viewPager);
- mGroup=(RadioGroup)findViewById(R.id.radiogroup);
- rbChat=(RadioButton)findViewById(R.id.rb_chat);
- rbContacts=(RadioButton)findViewById(R.id.rb_contacts);
- rbDiscovery=(RadioButton)findViewById(R.id.rb_discovery);
- rbMe=(RadioButton)findViewById(R.id.rb_me);
-
- mGroup.setOnCheckedChangeListener(new myCheckChangeListener());
- }
-
- private void initViewPager(){
- WeChatFragment weChatFragment=new WeChatFragment();
- ContactsFragment contactsFragment=new ContactsFragment();
- DiscoveryFragment discoveryFragment=new DiscoveryFragment();
- MeFragment meFragment=new MeFragment();
- fragmentList=new ArrayList<Fragment>();
- fragmentList.add(weChatFragment);
- fragmentList.add(contactsFragment);
- fragmentList.add(discoveryFragment);
- fragmentList.add(meFragment);
-
- mPager.setAdapter(new myFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
-
- mPager.setCurrentItem(0);
-
- mPager.setOnPageChangeListener(new myOnPageChangeListener());
- }
-
-
-
-
- private class myCheckChangeListener implements RadioGroup.OnCheckedChangeListener{
-
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- switch (checkedId){
- case R.id.rb_chat:
-
- mPager.setCurrentItem(0,false);
- break;
- case R.id.rb_contacts:
- mPager.setCurrentItem(1,false);
- break;
- case R.id.rb_discovery:
- mPager.setCurrentItem(2,false);
- break;
- case R.id.rb_me:
- mPager.setCurrentItem(3,false);
- break;
- }
- }
- }
-
-
-
-
- private class myOnPageChangeListener implements ViewPager.OnPageChangeListener{
-
- @Override
- public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
-
- }
-
- @Override
- public void onPageSelected(int position) {
- switch (position){
- case 0:
- mGroup.check(R.id.rb_chat);
- break;
- case 1:
- mGroup.check(R.id.rb_contacts);
- break;
- case 2:
- mGroup.check(R.id.rb_discovery);
- break;
- case 3:
- mGroup.check(R.id.rb_me);
- break;
- }
- }
-
- @Override
- public void onPageScrollStateChanged(int state) {
-
- }
- }
- }
activity_main.xml
- <RelativeLayout 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">
-
- <RadioGroup
- android:id="@+id/radiogroup"
- android:layout_width="match_parent"
- android:layout_height="55dp"
- android:orientation="horizontal"
- android:layout_alignParentBottom="true"
- android:background="@color/white">
-
- <RadioButton
- android:id="@+id/rb_chat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:drawableTop="@drawable/rb_chat_selector"
- android:checked="true"
- android:text="@string/radiobutton_wechat"
- style="@style/style_RadioButton"/>
- <RadioButton
- android:id="@+id/rb_contacts"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:drawableTop="@drawable/rb_contacts_selector"
- android:text="@string/radiobutton_contacts"
- style="@style/style_RadioButton"/>
- <RadioButton
- android:id="@+id/rb_discovery"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:drawableTop="@drawable/rb_discovery_selector"
- android:text="@string/radiobutton_discovery"
- style="@style/style_RadioButton"/>
- <RadioButton
- android:id="@+id/rb_me"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:drawableTop="@drawable/rb_me_selector"
- android:text="@string/radiobutton_me"
- style="@style/style_RadioButton"/>
- </RadioGroup>
- <android.support.v4.view.ViewPager
- android:id="@+id/viewPager"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@id/radiogroup"
- />
- </RelativeLayout>
myFragmentPagerAdapter
- package com.example.administrator.first.adapter;
-
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentPagerAdapter;
-
- import java.util.ArrayList;
-
-
-
-
- public class myFragmentPagerAdapter extends FragmentPagerAdapter {
- ArrayList<Fragment> list;
- public myFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list){
- super(fm);
- this.list=list;
- }
- @Override
- public Fragment getItem(int position) {
- return list.get(position);
- }
-
- @Override
- public int getCount() {
- return list.size();
- }
- }
style.xml
- <resources>
-
-
- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
-
- </style>
- <style name="style_RadioButton">
- <item name="android:button">@null</item>
- <item name="android:layout_weight">0.25</item>
- <item name="android:gravity">center</item>
- <item name="android:textColor">@color/rb_foucs_color</item>
- <item name="android:textSize">12sp</item>
- <item name="android:paddingTop">5dp</item>
- </style>
-
- </resources>