fragment是一种可以嵌入在activity中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间。
可以把fragment理解成一种迷你的activity。
Fragment的简单用法(静态加载Fragment):
1.先创建一个layout布局文件
2.创建对应layout类,继承fragment,重写onCreateView方法
3.在layout中加入fragment
在left_button中添加一个button
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="button"
android:id="@+id/left_button"/>
</LinearLayout>
创建一个LeftFragment类继承fragment
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
同样创建right_TextView.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_tv"
android:text="This is right_fragment "
android:textSize="20sp"
android:layout_gravity="center"/>
</LinearLayout>
然后创建RightFragment类也继承fragemnt
public class RightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
}
最后在主布局文件中加入fragment
<LinearLayout 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"
tools:context=".MainActivity">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/left_fragment"
android:name="com.gandazhipc.fragment.LeftFragment"/>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:weight="1"
android:id="@+id/right_fragment"
android:name="com.gandazhipc.fragment.RightFragment"
<LinearLayout/>
android:name属性来显示指明要添加的fragment名(一定要类的包名也加上)
效果图
动态加载Fragment
1.创建待添加的fragment实列
2.获取到FragmentManager
3.调用beginTransaction()开启一个事务
4.使用replace()向容器中添加fragment,需要传入容器的id和待添加的fragment实列
5.提交事务
在上个demo的基础上新建一个another_right_fragment.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is another fragment"
android:textSize="20sp"/>
</LinearLayout>
为了对比只是改变了背景和TextView
也创建一个OtherFragment继承Fragment
public class AnotherRightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_right_fragment,container,false);
return view;
}
}
修改主布局文件 第二个fragment上添加一个布局
<LinearLayout 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"
tools:context=".MainActivity">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/left_fragment"
android:name="com.gandazhipc.fragment.LeftFragment"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/right_layout">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/right_anotherFragment"
android:name="com.gandazhipc.fragment.RightFragment"/>
</FrameLayout>
</LinearLayout>
最后修改MainActivity中的类容
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.left_button);
button.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.left_button:
AnotherRightFragment fragment = new AnotherRightFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.commit();
break;
default:
break;
}
}
}
现在可以通过点击左侧的button 动态加载右边的fragment