viewPager+RadioButton+Fragment

ViewPager+RadioGroup+Fragment超高仿微信主界面,可通过左右滑动或点击底部RadioButton切换Fragment,废话不多说,上源码。

下载源码地址:http://download.csdn.net/detail/shenyuanqing/8847959

界面:

 


MainActivity.java

[java]  view plain copy
  1. package com.example.administrator.first.activity;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.support.v4.app.FragmentActivity;  
  6. import android.support.v4.view.ViewPager;  
  7. import android.widget.RadioButton;  
  8. import android.widget.RadioGroup;  
  9.   
  10. import com.example.administrator.first.R;  
  11. import com.example.administrator.first.adapter.myFragmentPagerAdapter;  
  12. import com.example.administrator.first.fragment.ContactsFragment;  
  13. import com.example.administrator.first.fragment.DiscoveryFragment;  
  14. import com.example.administrator.first.fragment.MeFragment;  
  15. import com.example.administrator.first.fragment.WeChatFragment;  
  16.   
  17. import java.util.ArrayList;  
  18.   
  19.   
  20. public class MainActivity extends FragmentActivity{  
  21.     private ViewPager mPager;  
  22.     private RadioGroup mGroup;  
  23.     private RadioButton rbChat,rbContacts,rbDiscovery,rbMe;  
  24.     private ArrayList<Fragment> fragmentList;  
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_main);  
  29.         //初始化界面组件  
  30.         initView();  
  31.         //初始化ViewPager  
  32.         initViewPager();  
  33.     }  
  34.   
  35.     private void initView(){  
  36.         mPager=(ViewPager)findViewById(R.id.viewPager);  
  37.         mGroup=(RadioGroup)findViewById(R.id.radiogroup);  
  38.         rbChat=(RadioButton)findViewById(R.id.rb_chat);  
  39.         rbContacts=(RadioButton)findViewById(R.id.rb_contacts);  
  40.         rbDiscovery=(RadioButton)findViewById(R.id.rb_discovery);  
  41.         rbMe=(RadioButton)findViewById(R.id.rb_me);  
  42.         //RadioGroup选中状态改变监听  
  43.         mGroup.setOnCheckedChangeListener(new myCheckChangeListener());  
  44.     }  
  45.   
  46.     private void initViewPager(){  
  47.         WeChatFragment weChatFragment=new WeChatFragment();  
  48.         ContactsFragment contactsFragment=new ContactsFragment();  
  49.         DiscoveryFragment discoveryFragment=new DiscoveryFragment();  
  50.         MeFragment meFragment=new MeFragment();  
  51.         fragmentList=new ArrayList<Fragment>();  
  52.         fragmentList.add(weChatFragment);  
  53.         fragmentList.add(contactsFragment);  
  54.         fragmentList.add(discoveryFragment);  
  55.         fragmentList.add(meFragment);  
  56.         //ViewPager设置适配器  
  57.         mPager.setAdapter(new myFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));  
  58.         //ViewPager显示第一个Fragment  
  59.         mPager.setCurrentItem(0);  
  60.         //ViewPager页面切换监听  
  61.         mPager.setOnPageChangeListener(new myOnPageChangeListener());  
  62.     }  
  63.   
  64.     /** 
  65.      *RadioButton切换Fragment 
  66.      */  
  67.     private class myCheckChangeListener implements RadioGroup.OnCheckedChangeListener{  
  68.   
  69.         @Override  
  70.         public void onCheckedChanged(RadioGroup group, int checkedId) {  
  71.             switch (checkedId){  
  72.                 case R.id.rb_chat:  
  73.                     //ViewPager显示第一个Fragment且关闭页面切换动画效果  
  74.                     mPager.setCurrentItem(0,false);  
  75.                     break;  
  76.                 case R.id.rb_contacts:  
  77.                     mPager.setCurrentItem(1,false);  
  78.                     break;  
  79.                 case R.id.rb_discovery:  
  80.                     mPager.setCurrentItem(2,false);  
  81.                     break;  
  82.                 case R.id.rb_me:  
  83.                     mPager.setCurrentItem(3,false);  
  84.                     break;  
  85.             }  
  86.         }  
  87.     }  
  88.   
  89.     /** 
  90.      *ViewPager切换Fragment,RadioGroup做相应变化 
  91.      */  
  92.     private class myOnPageChangeListener implements ViewPager.OnPageChangeListener{  
  93.   
  94.         @Override  
  95.         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {  
  96.   
  97.         }  
  98.   
  99.         @Override  
  100.         public void onPageSelected(int position) {  
  101.             switch (position){  
  102.                 case 0:  
  103.                     mGroup.check(R.id.rb_chat);  
  104.                     break;  
  105.                 case 1:  
  106.                     mGroup.check(R.id.rb_contacts);  
  107.                     break;  
  108.                 case 2:  
  109.                     mGroup.check(R.id.rb_discovery);  
  110.                     break;  
  111.                 case 3:  
  112.                     mGroup.check(R.id.rb_me);  
  113.                     break;  
  114.             }  
  115.         }  
  116.   
  117.         @Override  
  118.         public void onPageScrollStateChanged(int state) {  
  119.   
  120.         }  
  121.     }  
  122. }  


activity_main.xml

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <RadioGroup  
  7.         android:id="@+id/radiogroup"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="55dp"  
  10.         android:orientation="horizontal"  
  11.         android:layout_alignParentBottom="true"  
  12.         android:background="@color/white">  
  13.   
  14.         <RadioButton  
  15.             android:id="@+id/rb_chat"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:drawableTop="@drawable/rb_chat_selector"  
  19.             android:checked="true"  
  20.             android:text="@string/radiobutton_wechat"  
  21.             style="@style/style_RadioButton"/>  
  22.         <RadioButton  
  23.             android:id="@+id/rb_contacts"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:drawableTop="@drawable/rb_contacts_selector"  
  27.             android:text="@string/radiobutton_contacts"  
  28.             style="@style/style_RadioButton"/>  
  29.         <RadioButton  
  30.             android:id="@+id/rb_discovery"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:drawableTop="@drawable/rb_discovery_selector"  
  34.             android:text="@string/radiobutton_discovery"  
  35.             style="@style/style_RadioButton"/>  
  36.         <RadioButton  
  37.             android:id="@+id/rb_me"  
  38.             android:layout_width="wrap_content"  
  39.             android:layout_height="wrap_content"  
  40.             android:drawableTop="@drawable/rb_me_selector"  
  41.             android:text="@string/radiobutton_me"  
  42.             style="@style/style_RadioButton"/>  
  43.     </RadioGroup>  
  44.     <android.support.v4.view.ViewPager  
  45.         android:id="@+id/viewPager"  
  46.         android:layout_width="wrap_content"  
  47.         android:layout_height="wrap_content"  
  48.         android:layout_above="@id/radiogroup"  
  49.         />  
  50. </RelativeLayout>  

myFragmentPagerAdapter
[java]  view plain copy
  1. package com.example.administrator.first.adapter;  
  2.   
  3. import android.support.v4.app.FragmentManager;  
  4. import android.support.v4.app.Fragment;  
  5. import android.support.v4.app.FragmentPagerAdapter;  
  6.   
  7. import java.util.ArrayList;  
  8.   
  9. /** 
  10.  * Created by Administrator on 2015/6/24. 
  11.  */  
  12. public class myFragmentPagerAdapter extends FragmentPagerAdapter {  
  13.     ArrayList<Fragment> list;  
  14.     public myFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list){  
  15.         super(fm);  
  16.         this.list=list;  
  17.     }  
  18.     @Override  
  19.     public Fragment getItem(int position) {  
  20.         return list.get(position);  
  21.     }  
  22.   
  23.     @Override  
  24.     public int getCount() {  
  25.         return list.size();  
  26.     }  
  27. }  

style.xml

[html]  view plain copy
  1. <resources>  
  2.   
  3.     <!-- Base application theme. -->  
  4.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  5.         <!-- Customize your theme here. -->  
  6.     </style>  
  7.     <style name="style_RadioButton">  
  8.         <item name="android:button">@null</item>  
  9.         <item name="android:layout_weight">0.25</item>  
  10.         <item name="android:gravity">center</item>  
  11.         <item name="android:textColor">@color/rb_foucs_color</item>  
  12.         <item name="android:textSize">12sp</item>  
  13.         <item name="android:paddingTop">5dp</item>  
  14.     </style>  
  15.   
  16. </resources>  

你可能感兴趣的:(viewPager+RadioButton+Fragment)