Fragment+ViewPager 实现仿微信页面

效果图:








实现代码:

外形资源:

选择器:

[html]  view plain  copy
  1. <strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_checked="true" android:drawable="@drawable/et_shape"></item>  
  4.     <item android:state_checked="false" android:drawable="@drawable/et_shape2"></item>  
  5. </selector></span></strong>  
背景:

[html]  view plain  copy
  1. <strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle"  
  4.     >  
  5.     <solid android:color="#c4f4ee"></solid>  
  6. </shape></span></strong>  


[html]  view plain  copy
  1. <strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle"  
  4.     >  
  5.     <solid android:color="#fcfefc"></solid>  
  6. </shape></span></strong>  

xml:

[html]  view plain  copy
  1. <strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  6.     android:layout_height="match_parent" tools:context="zking.com.myapplication.MainActivity"  
  7.     android:orientation="vertical"  
  8.     >  
  9.   
  10.     <android.support.v4.view.ViewPager  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="0dp"  
  13.         android:layout_weight="1"  
  14.         android:id="@+id/vp_main_page"  
  15.         ></android.support.v4.view.ViewPager>  
  16.   
  17.     <RadioGroup  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:orientation="horizontal"  
  21.         android:id="@+id/rg_main_rg"  
  22.         >  
  23.         <RadioButton  
  24.             android:layout_width="0dp"  
  25.             android:layout_height="wrap_content"  
  26.             android:text="微信"  
  27.             android:layout_weight="1"  
  28.             android:button="@null"  
  29.             android:drawableTop="@drawable/icon_user"  
  30.             android:gravity="center"  
  31.             android:background="@drawable/et_select"  
  32.             android:id="@+id/rb_main_rb1"  
  33.             />  
  34.         <RadioButton  
  35.             android:layout_width="0dp"  
  36.             android:layout_height="wrap_content"  
  37.             android:text="通讯录"  
  38.             android:button="@null"  
  39.             android:layout_weight="1"  
  40.             android:drawableTop="@drawable/icon_user"  
  41.             android:gravity="center"  
  42.             android:background="@drawable/et_select"  
  43.             android:id="@+id/rb_main_rb2"  
  44.             />  
  45.         <RadioButton  
  46.             android:layout_width="0dp"  
  47.             android:layout_height="wrap_content"  
  48.             android:text="发现"  
  49.             android:button="@null"  
  50.             android:layout_weight="1"  
  51.             android:drawableTop="@drawable/icon_user"  
  52.             android:gravity="center"  
  53.             android:background="@drawable/et_select"  
  54.             android:id="@+id/rb_main_rb3"  
  55.             />  
  56.         <RadioButton  
  57.             android:layout_width="0dp"  
  58.             android:layout_height="wrap_content"  
  59.             android:text="我的"  
  60.             android:button="@null"  
  61.             android:layout_weight="1"  
  62.             android:drawableTop="@drawable/icon_user"  
  63.             android:gravity="center"  
  64.             android:background="@drawable/et_select"  
  65.             android:id="@+id/rb_main_rb4"  
  66.             />  
  67.   
  68.     </RadioGroup>  
  69.   
  70. </LinearLayout></span></strong>  

碎片布局:

[html]  view plain  copy
  1. <strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="这是通讯录界面"  
  10.         android:textSize="30sp"  
  11.         android:background="#660000ff"  
  12.         />  
  13.   
  14. </LinearLayout></span></strong>  

 
 
[html] view plain copy
  1. <strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="这是发现界面"  
  10.         android:textSize="30sp"  
  11.         android:background="#6600ff00"  
  12.         />  
  13.   
  14. </LinearLayout></span></strong>  
[html] view plain copy
  1. <strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="这是我的界面"  
  10.         android:textSize="30sp"  
  11.         android:background="#6600ffff"  
  12.         />  
  13.   
  14. </LinearLayout></span></strong>  
[html] view plain copy
  1. <strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <TextView  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="这是微信界面(聊天历史)"  
  10.         android:textSize="30sp"  
  11.         android:background="#66ff0000"  
  12.         />  
  13.   
  14. </LinearLayout></span></strong>  
java:
 
 
[html] view plain copy
  1. <strong><span style="font-size:18px;">public class ContactsFragment extends Fragment{  
  2.   
  3.     @Nullable  
  4.     @Override  
  5.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
  6.         return inflater.inflate(R.layout.fragment_contacts,null);  
  7.     }  
  8. }  
  9. </span></strong>  
  
  
[html] view plain copy
  1. <strong><span style="font-size:18px;">public class FindFragment extends Fragment{  
  2.   
  3.     @Nullable  
  4.     @Override  
  5.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
  6.         return inflater.inflate(R.layout.fragment_find,null);  
  7.     }  
  8. }</span></strong>  
[html] view plain copy
  1. <strong><span style="font-size:18px;">public class MyFragment extends Fragment{  
  2.   
  3.     @Nullable  
  4.     @Override  
  5.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
  6.         return inflater.inflate(R.layout.fragment_my,null);  
  7.     }  
  8. }</span></strong>  
[html] view plain copy
  1. <strong><span style="font-size:18px;">public class WeiXinFragment extends Fragment{  
  2.   
  3.     @Nullable  
  4.     @Override  
  5.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
  6.         return inflater.inflate(R.layout.fragment_weixin,null);  
  7.     }  
  8. }</span></strong>  
[html] view plain copy
  1. <strong><span style="font-size:18px;">public class MainActivity extends AppCompatActivity {  
  2.   
  3.     private ViewPager vp_main_page;  
  4.     private List<Fragment> fragments=new ArrayList<>();  
  5.     private int[] id={R.id.rb_main_rb1,R.id.rb_main_rb2,R.id.rb_main_rb3,R.id.rb_main_rb4};  
  6.     private RadioGroup rg_main_rg;  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.   
  13.         //获取控件:ViewPage  
  14.         vp_main_page = (ViewPager) findViewById(R.id.vp_main_page);  
  15.         ContactsFragment contactsFragment=new ContactsFragment();  
  16.         FindFragment findFragment=new FindFragment();  
  17.         MyFragment myFragment=new MyFragment();  
  18.         WeiXinFragment weiXinFragment=new WeiXinFragment();  
  19.         fragments.add(weiXinFragment);  
  20.         fragments.add(contactsFragment);  
  21.         fragments.add(findFragment);  
  22.         fragments.add(myFragment);  
  23.         vp_main_page.setAdapter(new MyAdpadter(getSupportFragmentManager()));  
  24.         //获取单选组  
  25.         rg_main_rg = (RadioGroup) findViewById(R.id.rg_main_rg);  
  26.         rg_main_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  27.             @Override  
  28.             public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {  
  29. //                Toast.makeText(MainActivity.this, ""+group.getCheckedRadioButtonId(), Toast.LENGTH_SHORT).show();  
  30. //                vp_main_page.setCurrentItem(checkedId-1);  
  31.                 if (checkedId==id[0]){  
  32.                     vp_main_page.setCurrentItem(0);  
  33.                 }else if (checkedId==id[1]){  
  34.                     vp_main_page.setCurrentItem(1);  
  35.                 }else if (checkedId==id[2]){  
  36.                     vp_main_page.setCurrentItem(2);  
  37.                 }else if (checkedId==id[3]){  
  38.                     vp_main_page.setCurrentItem(3);  
  39.                 }  
  40.             }  
  41.         });  
  42.         rg_main_rg.check(id[0]);  
  43.         vp_main_page.setCurrentItem(0);  
  44.         vp_main_page.getCurrentItem();  
  45.   
  46.        //ViewPager控件的滑动监听  
  47.         vp_main_page.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {  
  48.             @Override  
  49.             public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {  
  50.   
  51.             }  
  52.   
  53.             @Override  
  54.             public void onPageSelected(int position) {  
  55.   
  56.             }  
  57.   
  58.             @Override  
  59.             public void onPageScrollStateChanged(int state) {//此方法是在状态改变的时候调用  
  60.                 //state==0  代表什么都没做  
  61.                 //state==1  代表正在滑动  
  62.                 //state==2  代表滑动完毕  
  63.                 if (state==2){  
  64.                     rg_main_rg.check(id[vp_main_page.getCurrentItem()]);  
  65.                 }  
  66.             }  
  67.         });  
  68.     }  
  69.   
  70.     class MyAdpadter extends FragmentPagerAdapter{  
  71.         public MyAdpadter(FragmentManager fm) {  
  72.             super(fm);  
  73.         }  
  74.   
  75.         @Override  
  76.         public Fragment getItem(int position) {  
  77.             return fragments.get(position);  
  78.         }  
  79.   
  80.         @Override  
  81.         public int getCount() {  
  82.             return fragments.size();  
  83.         }  
  84.     }  
  85.   
  86. }</span></strong>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值