android高级控件-Fragment

来源:互联网 发布:制作电子报刊的软件 编辑:程序博客网 时间:2024/05/10 21:27

  Frament+ViewPager

 viewpage可以做简单的导航,到页面菜单,可以使用视图滑动,实现像lanucher左右滑动等等,

  在布局文件中加入控件viewpager,这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载xml布局文件,他是不会显示内容的


 1.frament作为activity界面的一部分组成出现。
 2.可以一个activity界面同时出现对个frament,并且一个frament也可以再多个activity中使用
 3.在activity运行中可以动态添加,移除,替换frament

结果如下:能够自由切换效果


这是主页面

[html] view plain copy
  1. <?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"  
  7.     tools:context="com.example.android_pagetframent.MainActivity"  
  8.     android:orientation="vertical"  
  9.     >  
  10.   
  11.     <android.support.v4.view.ViewPager  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="match_parent"  
  14.         android:layout_weight="1"  
  15.         android:id="@+id/Res_ViewPager">  
  16.     </android.support.v4.view.ViewPager>  
  17.   
  18.     <RadioGroup  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:orientation="horizontal"  
  22.         android:id="@+id/res_RadioGroup"  
  23.   
  24.         >  
  25.         <RadioButton  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:drawableTop="@drawable/xiao"  
  29.             android:background="@drawable/yanse"  
  30.             android:paddingLeft="60dp"  
  31.             android:text="微信"  
  32.             android:button="@null"  
  33.             android:id="@+id/res_1"  
  34.             />  
  35.   
  36.         <RadioButton  
  37.             android:id="@+id/res_2"  
  38.             android:layout_width="wrap_content"  
  39.             android:layout_height="wrap_content"  
  40.             android:background="@drawable/yanse"  
  41.             android:button="@null"  
  42.             android:drawableTop="@drawable/xiao"  
  43.             android:paddingLeft="100dp"  
  44.             android:text="通讯录" />  
  45.   
  46.         <RadioButton  
  47.             android:id="@+id/res_3"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:background="@drawable/yanse"  
  51.             android:button="@null"  
  52.             android:drawableTop="@drawable/xiao"  
  53.             android:paddingLeft="90dp"  
  54.             android:text="发现" />  
  55.   
  56.         <RadioButton  
  57.             android:layout_width="wrap_content"  
  58.             android:layout_height="wrap_content"  
  59.             android:paddingLeft="103dp"  
  60.             android:drawableTop="@drawable/xiao"  
  61.             android:background="@drawable/yanse"  
  62.             android:button="@null"  
  63.             android:text="我的"  
  64.             android:id="@+id/res_4"  
  65.             />  
  66.     </RadioGroup>  
  67.   
  68. </LinearLayout>  

每一个按钮都是一个View视图,所以我写了四个View,在调用的时候转成Frament

[html] view plain copy
  1. <?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"  
  7.     android:orientation="vertical"  
  8.     tools:context="com.example.android_pagetframent.MainActivity">  
  9.   
  10.     <TextView  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:textSize="30sp"  
  14.         android:text="这是发现界面"  
  15.         android:background="#66000000"  
  16.         />  
  17.   
  18. </LinearLayout>  
[html] view plain copy
  1.   
[html] view plain copy
  1. <span style="font-size:18px;">ActivityJAVA类</span>  

[html] view plain copy
  1. package com.example.android_pagetframent;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. /**  
  10.  * Created by Administrator on 2017/6/14 0014.  
  11.  */  
  12.   
  13. public class weixinFrament extends Fragment {  
  14.     @Override  
  15.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  16.         return inflater.inflate(R.layout.weixin,null);  
  17.     }  
  18. }  

[html] view plain copy
  1. <?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"  
  7.     android:orientation="vertical"  
  8.     tools:context="com.example.android_pagetframent.MainActivity">  
  9.     <TextView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:textSize="30sp"  
  13.         android:text="这是通讯录界面"  
  14.         android:background="#66ff00ff"  
  15.         />  
  16. </LinearLayout>  
[html] view plain copy
  1. <span style="font-size:18px;">ActivityJAVA类</span>  

[html] view plain copy
  1. package com.example.android_pagetframent;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.annotation.Nullable;  
  5. import android.support.v4.app.Fragment;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9.   
  10. /**  
  11.  * Created by Administrator on 2017/6/14 0014.  
  12.  */  
  13.   
  14. public class tongxunluFrament extends Fragment {  
  15.     @Nullable  
  16.     @Override  
  17.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
  18.         return inflater.inflate(R.layout.tongxunlu,null);  
  19.     }  
  20. }  

     每个视图都有一个ActivityJAVA类为了能实现切换效果,我就继承了Frament碎片,把所有的View转成碎片

加入到一个集合中去根据他所点击的来切换。

[html] view plain copy
  1. package com.example.android_pagetframent;  
  2.   
  3. import android.support.annotation.IdRes;  
  4. import android.support.v4.app.Fragment;  
  5. import android.support.v4.app.FragmentManager;  
  6. import android.support.v4.app.FragmentPagerAdapter;  
  7. import android.support.v4.app.FragmentTransaction;  
  8. import android.support.v4.view.ViewPager;  
  9. import android.support.v7.app.AppCompatActivity;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.widget.RadioGroup;  
  13. import android.widget.Toast;  
  14.   
  15. import java.util.ArrayList;  
  16. import java.util.List;  
  17.   
  18. public class MainActivity extends AppCompatActivity{  
  19.     private ViewPager res_viewPager;  
  20.     private List<Fragment> fragmentList=new ArrayList<>();  
  21.     private RadioGroup res_radioGroup;  
  22.     private int[] list={R.id.res_1,R.id.res_2,R.id.res_3,R.id.res_4};  
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.         res_viewPager = (ViewPager) findViewById(R.id.Res_ViewPager);  
  28. <span style="white-space:pre">  </span>  
[html] view plain copy
  1. <span style="font-size: 18px; white-space: pre;">   </span><span style="font-size:24px;">//这是我自己写的四个Frament</span><span style="font-size: 18px;">  
  2.         faxianFrament faxian=new faxianFrament();  
  3.         tongxunluFrament tong=new tongxunluFrament();  
  4.         weixinFrament weixain=new weixinFrament();  
  5.         woFrament wo=new woFrament();  
  6.         fragmentList.add(faxian);  
  7.         fragmentList.add(tong);  
  8.         fragmentList.add(weixain);  
  9.         fragmentList.add(wo);  
  10.   
  11.         res_viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));  
  12.   
  13.         res_radioGroup = (RadioGroup) findViewById(R.id.res_RadioGroup);  
  14.         res_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  15.             @Override  
  16.             public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {</span>  
[html] view plain copy
  1. <span style="font-size: 18px;"><span style="white-space:pre">       </span>//拿到我所点击的id,进行判断要跳转的页面  
  2.               if(checkedId==list[0]){  
  3.                     res_viewPager.setCurrentItem(0);  
  4.                 }else if(checkedId==list[1]){  
  5.                     res_viewPager.setCurrentItem(1);  
  6.                 }else if(checkedId==list[2]){  
  7.                     res_viewPager.setCurrentItem(2);  
  8.                 }else if(checkedId==list[3]){  
  9.                     res_viewPager.setCurrentItem(3);  
  10.                 }  
  11.             }  
  12.         });</span>  
[html] view plain copy
  1. <span style="font-size: 18px;"><span style="white-space:pre">   </span>//设置默认被选中的  
  2.         res_viewPager.setCurrentItem(0);  
  3.         res_radioGroup.check(list[0]);  
  4.         //ViewPager控件的滑动监听  
  5.         res_viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {  
  6.             @Override  
  7.             public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {  
  8.             }  
  9.             @Override  
  10.             public void onPageSelected(int position) {  
  11.             }  
  12.             //此方法是在状态改变的时候调用  
  13.             @Override  
  14.             public void onPageScrollStateChanged(int state) {  
  15.                 //state==0  代表什么都没做  
  16.                 //state==1  代表正在滑动  
  17.                 //state==2  代表滑动完毕  
  18.                 if (state==2){</span>  
[html] view plain copy
  1. <span style="font-size: 18px;"><span style="white-space:pre">           </span>//当他滑动的时候调用这个方法,一切换页面成功则刷新一次按钮选中的是他相对应的</span>  
[html] view plain copy
  1. <span style="font-size: 18px;"><span style="white-space:pre">       </span>界面,  
  2.                     res_radioGroup.check(list[res_viewPager.getCurrentItem()]);</span>  
[html] view plain copy
  1. <span style="font-size: 18px;"></span><pre name="code" class="html"><span style="font-size: 18px;"><span style="white-space:pre">       </span>//res_viewPager.getCurrentItem();拿到当前页面的下标</span></pre> } } }); } class MyAdapter extends FragmentPagerAdapter{ public MyAdapter(FragmentManager  
  2.  fm) { super(fm); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } }}<p></p>  
  3. <pre></pre>  
  4. <br>  
  5. <br>  
  6. <p></p>  
  7. <p><span style="font-size:14px"><br>  
  8. </span></p>  
  9.      
原创粉丝点击