Android之Fragment切换
Fragment要点
1.Fragment作为Activity界面的一部分组成出现
2.可以在一个Activity中同时出现多个Fragment,并且,一个Fragment 亦可在多个Activity中使用。
3.在Activity运行过程中,可以添加、移除或者替换 Fragment(add()、remove()、replace())
4.Fragment可以响应自己的输入事件,并且有自己的生命周期,当然,它 们的生命周期直接被其所属的宿主activity的生命周期影响。
之前在学android时用tabActivity做过一个切换卡,的小示例,但是发现代码很多,而且界面很难调整,所以上网搜了一下,说是可以用Fragment实现像tableActivity那样的切换卡功能;今天做了一个小示例,如下:
1、创建主Activity:
主Activity的布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 菜单项 -->
<LinearLayout
android:id="@+id/check_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:padding="0dip"
android:orientation="horizontal"
android:background="#437c1e"
android:layout_alignParentTop="true">
<LinearLayout android:id="@+id/tab_one"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#43ac1e">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="15dip"
android:paddingBottom="10dip"
android:text="当日订单"
android:textSize="17sp"
android:textColor="#fff"/>
</LinearLayout>
<LinearLayout android:id="@+id/tab_two"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="10dip"
android:gravity="center_horizontal"
android:text="平台结算"
android:textSize="17sp"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
<!-- Fragment内容区 -->
<FrameLayout
android:layout_below="@id/check_tab"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="8"/>
</RelativeLayout>
主activity的代码:
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener{
//控件
LinearLayout tab_one ,tab_two,tab_three,tab_four;
TodayFragment todayFragment=null;
SettleFragment settleFragment=null;
FragmentManager fmm;
FragmentTransaction ftt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initViews();
initListeners();
checkTab(0);
}
public void initViews(){
tab_one = (LinearLayout)findViewById(R.id.tab_one);
tab_two = (LinearLayout)findViewById(R.id.tab_two);
tab_three = (LinearLayout)findViewById(R.id.tab_three);
tab_four = (LinearLayout)findViewById(R.id.tab_four_bar);
}
public void initListeners(){
tab_one.setOnClickListener(this);
tab_two.setOnClickListener(this);
}
//选项卡切换
public void checkTab(int id){
fmm = getFragmentManager();
ftt = fmm.beginTransaction();
clearSelection();
hiddenFragment(ftt);
switch (id) {
case 0:
tab_one.setBackgroundColor(Color.rgb(67, 172, 30));
if (todayFragment == null) {
// 如果MessageFragment为空,则创建一个并添加到界面上
todayFragment = new TodayFragment();
//此处为传参所用
/*Bundle bundle = new Bundle();
ArrayList arraylist = new ArrayList();
arraylist.add(list);
bundle.putCharSequenceArrayList("result", arraylist);
bundle.putInt("xxx", xxx);
bundle.putInt("xxx", xxx);/
bundle.putString("xxx", xxx);
bundle.putInt("xxx", xxx);/
todayFragment.setArguments(bundle);*/
ftt.add(R.id.content, todayFragment);
} else {
// 如果MessageFragment不为空,则直接将它显示出来
ftt.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
ftt.show(todayFragment);
}
break;
case 1:
tab_two.setBackgroundColor(Color.rgb(67, 172, 30));
if (settleFragment == null) {
// 如果MessageFragment为空,则创建一个并添加到界面上
settleFragment = new SettleFragment();
ftt.add(R.id.content, settleFragment);
} else {
// 如果MessageFragment不为空,则直接将它显示出来
ftt.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
ftt.show(settleFragment);
}
break;
default:
break;
}
ftt.commit();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tab_one:
// 当点击了消息tab时,选中第1个tab
checkTab(0);
break;
case R.id.tab_two:
// 当点击了联系人tab时,选中第2个tab
checkTab(1);
break;
default:
break;
}
}
//清除状态
private void clearSelection(){
tab_one.setBackgroundColor(Color.rgb(67, 124, 30));
tab_two.setBackgroundColor(Color.rgb(67, 124, 30));
tab_three.setBackgroundColor(Color.rgb(67, 124, 30));
tab_four.setBackgroundColor(Color.rgb(67, 124, 30));
}
//隐藏fragment
private void hiddenFragment(FragmentTransaction transaction){
if (todayFragment !=null) {
transaction.hide(todayFragment);
}
if (settleFragment!=null) {
transaction.hide(settleFragment);
}
}
}
2、创建Fragment
下面是第一个Fragment的布局(fragment_one.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_one"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 此布局为要显示的切换卡模块内容 -->
<TextView
android:id="@+id/title_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是第一个fragment切换卡"
android:textSize="18sp"
android:padding="5dip"
android:layout_alignParentTop="true"/>
<View
android:id="@+id/title_line"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#999"
android:layout_below="@id/title_bar"/>
<ImageView
android:id="@+id/content_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dip"
android:layout_below="@id/title_line"
android:layout_centerHorizontal="true"
android:src="@drawable/tab_one_bg"
android:contentDescription="xxx"/>
</RelativeLayout>
下面是第一个fragment(TodayFragment.java)
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TodayFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_one, container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
}
下面是第二个Fragment
布局(fragment_two.xml)
<?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" >
<TextView
android:id="@+id/ftwo_title_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#fff"
android:padding="5dip"
android:drawableBottom="@drawable/table_two_bg"/>
</LinearLayout>
代码(SettleFragment.java)
package com.yyh.takeaway;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SettleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
程序大体代码就是上面这个样子,如果要设置控件的响应事件,直接在onActivityCreated中写即可,大体跟Activity中是一样的,加载控件的话在onCreateView中加载。关于Fragment,还有很多知识点,本人新手,还没有掌握,此处只是对自己用到的做一个总结,记录一下,方便以后翻阅