Android Fragment的使用 《第一行代码》
2015-12-22 13:40
525 查看
一.静态Fragment
1.定义Fragment的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#000000"> <TextView android:textSize="20sp" android:text="This is headlines fragment" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
2.创建对应布局文件的Fragment类
对与Fragment类的布局文件的加载需要使用动态加载LayoutInflater.inflater()获取LayoutInflater实例的三种方式
1.LayoutInflater inflater = getLayoutInflater();在Activity中获取实例对象
2.LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
3.LayoutInflater inflater = LayoutInflater.from(context);
public class HeadlinesFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_headlines, container, false); } }
定义显示Fragment的Activity的布局文件
在布局文件中需要添加Fragment的位置添加<fragment android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:name="定义的Fragment对应类" android:id="@+id/article_fragment"/>
定义显示Activity的类
public class FragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); } }
二.动态Fragment
1.定义需要动态添加的Fragment布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:background="#ffff00"> <TextView android:text="This is another fragment" android:textSize="20sp" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
2.定义需要动态添加的Fragment类
public class anotherFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_another, container, false); } }
3.Activity的布局文件中添加Fragment容器
<FrameLayout android:id="@+id/right_layout" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.myfirst.myfirst.HeadlinesFragment" android:id="@+id/headlines_fragment"/> </FrameLayout>
4.获取FragmentManager
在活动中可以直接获取FragmentManager对象FragmentManager fragmentManager = getFragmentManager();
5.开启一个事务,通过调用beginTransaction()方法开启
FragmentTransaction transaction = fragmentManager .beginTransaction();
6.调用替换的方法
传入的两个参数,第一个是放置Fragmnet的容器的id,第二个参数是新的Fragment对象的实例对象transaction.replace(R.id.right_layout,fragment);
7.提交事务
transaction.commit();
所有代码
fragment_article.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:text="Button" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
ArticleFragment.java
public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_article, container, false); } }
fragment_headlines.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#000000"> <TextView android:textSize="20sp" android:text="This is headlines fragment" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
HeadlinesFragment.java
public class HeadlinesFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_headlines, container, false); } }
another_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:background="#ffff00"> <TextView android:text="This is another fragment" android:textSize="20sp" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
anotherFragment.java
public class anotherFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_another, container, false); } }
activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.myfirst.myfirst.ArticleFragment"
android:id="@+id/article_fragment"/>
<FrameLayout android:id="@+id/right_layout" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.myfirst.myfirst.HeadlinesFragment" android:id="@+id/headlines_fragment"/> </FrameLayout>
</LinearLayout>
FragmentActivity.java
public class FragmentActivity extends Activity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v){ switch (v.getId()){ case R.id.button: anotherFragment fragment = new anotherFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.right_layout,fragment); //使按下back键可以返回到上一个碎片 transaction.addToBackStack(null); transaction.commit(); break; default: break; } } }
以上是对Fragment学习的记录。
相关文章推荐
- ListView和CheckBox的混乱问题
- 史上最全!最经典!最无私的Android资料(书籍+代码)分享-不要积分
- 解决ListView setOnItemLongClickListener 长按无响应(嵌套GridView)
- android studio logcat 打印不出信息
- android的左右滑动效果实现-ViewFlipper
- 在android下使用i2c tools
- 安卓异常android.view.InflateException: Binary XML file line #13: Error inflating class cn.utils.RoundIma
- Android四大组件之广播的使用
- Android学习笔记(25):带动画效果的View切换ViewAnimator及其子类
- Android自定义ActionBar背景色、字体颜色等样式style
- Android学习笔记(24):进度条组件ProgressBar及其子类
- android sdk 更新失败问题
- ViewPager onPageChangeListener总结
- Android属性 android:focusableInTouchMode
- Android使用系统打电话
- android studio logcat 包名显示问号
- Android多媒体之 wav和amr的互转
- android中 init()
- AndroidStudio配置常见问题及解决
- 开放android的root权限