在Fragment的生命周期中,需要重点关注onCreate、onCreateView、onViewCreated。Activity与Fragment生命周期在设计模式上大体一致。
package com.lixu.fragment_test; import android.app.Activity; import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//设置初始界面为“微信”。
getweixn(); ImageView iv1 = (ImageView) findViewById(R.id.iv1);
ImageView iv2 = (ImageView) findViewById(R.id.iv2);
ImageView iv3 = (ImageView) findViewById(R.id.iv3);
ImageView iv4 = (ImageView) findViewById(R.id.iv4); iv1.setOnClickListener(this);
iv2.setOnClickListener(this);
iv3.setOnClickListener(this);
iv4.setOnClickListener(this); } public void getweixn() {
FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// 将原有的Activity替换成Fragment。也可以添加到原有之上不替换 如:ft.add(R.id.rel, new
// Fragment()) ;如果添加的多了会比较耗资源。
ft.replace(R.id.fl, new MyFragment1());
// 提交
ft.commit(); } public void gettongxunlu() {
//设置Fragment管理器
FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fl, new MyFragment2()); ft.commit(); } public void getfaxian() {
FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fl, new MyFragment3()); ft.commit(); } public void getwo() {
FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fl, new MyFragment4()); ft.commit(); } @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv1:
getweixn();
break;
case R.id.iv2:
gettongxunlu();
break;
case R.id.iv3:
getfaxian();
break;
case R.id.iv4:
getwo();
break; default:
break;
}
} }
package com.lixu.fragment_test; import android.app.Fragment;
import android.os.Bundle; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; class MyFragment1 extends Fragment { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} // 创建view 也可以直接在这里面获取view中的控件。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.image, null); return view;
} // 创建view完毕 可以直接在onCreateView中获取view中控件。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); ImageView iv = (ImageView) view.findViewById(R.id.iv);
iv.setImageResource(R.drawable.weixinjiemian);
}
}
package com.lixu.fragment_test; import java.util.ArrayList;
import java.util.HashMap; import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView; class MyFragment2 extends Fragment {
private static final String TYPE = "type";
private static final String DATE = "date";
private static final int GROUP = -1;
private static final int ITEM = -2; private ArrayList<HashMap<String, Object>> item = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); } // 创建view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.list, null); return view;
} // 创建view完毕
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); ListView lv = (ListView) view.findViewById(R.id.lv);
MyAdaper adapter = new MyAdaper(getActivity(), -1); item = new ArrayList<HashMap<String, Object>>(); String[] groups = { "家人", "朋友", "同学", "同事" };
String[] date = { "张三", "李四", "王五", "狗儿" };
for (String str : groups) {
HashMap<String, Object> group_map = new HashMap<String, Object>(); group_map.put(TYPE, GROUP);
group_map.put(DATE, str);
item.add(group_map);
for (String n : date) { HashMap<String, Object> date_map = new HashMap<String, Object>(); date_map.put(TYPE, ITEM);
date_map.put(DATE, n);
item.add(date_map); } }
lv.setAdapter(adapter);
} private class MyAdaper extends ArrayAdapter {
private LayoutInflater flater = null;
private Context context; public MyAdaper(Context context, int resource) {
super(context, resource);
this.context = context;
flater = LayoutInflater.from(context);
} @Override
public int getCount() {
return item.size();
} @Override
public int getItemViewType(int position) {
HashMap<String, Object> map = item.get(position);
return (Integer) map.get(TYPE);
} @Override
public int getViewTypeCount() { return 2;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
// 根据不同的view type加载不同的布局文件。
switch (type) {
case GROUP: convertView = flater.inflate(android.R.layout.simple_list_item_1, null);
TextView tv1 = (TextView) convertView.findViewById(android.R.id.text1);
tv1.setText(item.get(position).get(DATE) + "");
tv1.setBackgroundColor(Color.RED);
tv1.setTextSize(20); break;
case ITEM:
convertView = flater.inflate(android.R.layout.simple_list_item_1, null);
TextView tv2 = (TextView) convertView.findViewById(android.R.id.text1);
tv2.setText(item.get(position).get(DATE) + ""); break; default:
break;
} return convertView;
} }
}
package com.lixu.fragment_test; import android.app.Fragment;
import android.os.Bundle; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; class MyFragment3 extends Fragment { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} // 创建view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.image, null); return view;
} // 创建view完毕
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); ImageView iv = (ImageView) view.findViewById(R.id.iv);
iv.setImageResource(R.drawable.faxianjiemian);
}
}
package com.lixu.fragment_test; import android.app.Fragment;
import android.os.Bundle; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; class MyFragment4 extends Fragment { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} // 创建view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.image, null); return view;
} // 创建view完毕
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); ImageView iv = (ImageView) view.findViewById(R.id.iv);
iv.setImageResource(R.drawable.wojiemian);
}
}
xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lixu.fragment_test.MainActivity" > <FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</FrameLayout> <LinearLayout
android:id="@+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/iv1"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@drawable/weixin" /> <ImageView
android:id="@+id/iv2"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@drawable/tongxunlu" /> <ImageView
android:id="@+id/iv3"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@drawable/faxian" /> <ImageView
android:id="@+id/iv4"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@drawable/wo" />
</LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent" > </ImageView>
<?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" > <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>
运行效果图,演示为主,效果为辅,图片有点不真实,哈哈。