转自 https://www.cnblogs.com/labixiaoxin/p/4981444.html
在Fragment的生命周期中,需要重点关注onCreate、onCreateView、onViewCreated。Activity与Fragment生命周期在设计模式上大体一致。
1 package com.lixu.fragment_test; 2 3 import android.app.Activity; 4 5 import android.app.FragmentManager; 6 import android.app.FragmentTransaction; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.Window; 11 import android.widget.ImageView; 12 13 public class MainActivity extends Activity implements OnClickListener { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 requestWindowFeature(Window.FEATURE_NO_TITLE); 19 setContentView(R.layout.activity_main); 20 //设置初始界面为“微信”。 21 getweixn(); 22 23 ImageView iv1 = (ImageView) findViewById(R.id.iv1); 24 ImageView iv2 = (ImageView) findViewById(R.id.iv2); 25 ImageView iv3 = (ImageView) findViewById(R.id.iv3); 26 ImageView iv4 = (ImageView) findViewById(R.id.iv4); 27 28 iv1.setOnClickListener(this); 29 iv2.setOnClickListener(this); 30 iv3.setOnClickListener(this); 31 iv4.setOnClickListener(this); 32 33 } 34 35 public void getweixn() { 36 FragmentManager fm = this.getFragmentManager(); 37 FragmentTransaction ft = fm.beginTransaction(); 38 // 将原有的Activity替换成Fragment。也可以添加到原有之上不替换 如:ft.add(R.id.rel, new 39 // Fragment()) ;如果添加的多了会比较耗资源。 40 ft.replace(R.id.fl, new MyFragment1()); 41 // 提交 42 ft.commit(); 43 44 } 45 46 public void gettongxunlu() { 47 //设置Fragment管理器 48 FragmentManager fm = this.getFragmentManager(); 49 FragmentTransaction ft = fm.beginTransaction(); 50 51 ft.replace(R.id.fl, new MyFragment2()); 52 53 ft.commit(); 54 55 } 56 57 public void getfaxian() { 58 FragmentManager fm = this.getFragmentManager(); 59 FragmentTransaction ft = fm.beginTransaction(); 60 61 ft.replace(R.id.fl, new MyFragment3()); 62 63 ft.commit(); 64 65 } 66 67 public void getwo() { 68 FragmentManager fm = this.getFragmentManager(); 69 FragmentTransaction ft = fm.beginTransaction(); 70 71 ft.replace(R.id.fl, new MyFragment4()); 72 73 ft.commit(); 74 75 } 76 77 @Override 78 public void onClick(View v) { 79 switch (v.getId()) { 80 case R.id.iv1: 81 getweixn(); 82 break; 83 case R.id.iv2: 84 gettongxunlu(); 85 break; 86 case R.id.iv3: 87 getfaxian(); 88 break; 89 case R.id.iv4: 90 getwo(); 91 break; 92 93 default: 94 break; 95 } 96 } 97 98 }
1 package com.lixu.fragment_test; 2 3 4 import android.app.Fragment; 5 import android.os.Bundle; 6 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.ImageView; 11 12 class MyFragment1 extends Fragment { 13 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 } 18 19 // 创建view 也可以直接在这里面获取view中的控件。 20 @Override 21 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 22 23 View view = inflater.inflate(R.layout.image, null); 24 25 return view; 26 } 27 28 // 创建view完毕 可以直接在onCreateView中获取view中控件。 29 @Override 30 public void onViewCreated(View view, Bundle savedInstanceState) { 31 super.onViewCreated(view, savedInstanceState); 32 33 ImageView iv = (ImageView) view.findViewById(R.id.iv); 34 iv.setImageResource(R.drawable.weixinjiemian); 35 } 36 }
1 package com.lixu.fragment_test; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 import android.app.Fragment; 7 import android.content.Context; 8 import android.graphics.Color; 9 import android.os.Bundle; 10 11 import android.view.LayoutInflater; 12 import android.view.View; 13 import android.view.ViewGroup; 14 import android.widget.ArrayAdapter; 15 import android.widget.ListView; 16 import android.widget.TextView; 17 18 class MyFragment2 extends Fragment { 19 private static final String TYPE = "type"; 20 private static final String DATE = "date"; 21 private static final int GROUP = -1; 22 private static final int ITEM = -2; 23 24 private ArrayList> item = null; 25 26 @Override 27 public void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 30 } 31 32 // 创建view 33 @Override 34 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 35 36 View view = inflater.inflate(R.layout.list, null); 37 38 return view; 39 } 40 41 // 创建view完毕 42 @Override 43 public void onViewCreated(View view, Bundle savedInstanceState) { 44 super.onViewCreated(view, savedInstanceState); 45 46 ListView lv = (ListView) view.findViewById(R.id.lv); 47 MyAdaper adapter = new MyAdaper(getActivity(), -1); 48 49 item = new ArrayList >(); 50 51 String[] groups = { "家人", "朋友", "同学", "同事" }; 52 String[] date = { "张三", "李四", "王五", "狗儿" }; 53 for (String str : groups) { 54 HashMap group_map = new HashMap (); 55 56 group_map.put(TYPE, GROUP); 57 group_map.put(DATE, str); 58 item.add(group_map); 59 for (String n : date) { 60 61 HashMap date_map = new HashMap (); 62 63 date_map.put(TYPE, ITEM); 64 date_map.put(DATE, n); 65 item.add(date_map); 66 67 } 68 69 } 70 lv.setAdapter(adapter); 71 } 72 73 private class MyAdaper extends ArrayAdapter { 74 private LayoutInflater flater = null; 75 private Context context; 76 77 public MyAdaper(Context context, int resource) { 78 super(context, resource); 79 this.context = context; 80 flater = LayoutInflater.from(context); 81 } 82 83 @Override 84 public int getCount() { 85 return item.size(); 86 } 87 88 @Override 89 public int getItemViewType(int position) { 90 HashMap map = item.get(position); 91 return (Integer) map.get(TYPE); 92 } 93 94 @Override 95 public int getViewTypeCount() { 96 97 return 2; 98 } 99 100 @Override 101 public View getView(int position, View convertView, ViewGroup parent) { 102 int type = getItemViewType(position); 103 // 根据不同的view type加载不同的布局文件。 104 switch (type) { 105 case GROUP: 106 107 convertView = flater.inflate(android.R.layout.simple_list_item_1, null); 108 TextView tv1 = (TextView) convertView.findViewById(android.R.id.text1); 109 tv1.setText(item.get(position).get(DATE) + ""); 110 tv1.setBackgroundColor(Color.RED); 111 tv1.setTextSize(20); 112 113 break; 114 case ITEM: 115 convertView = flater.inflate(android.R.layout.simple_list_item_1, null); 116 TextView tv2 = (TextView) convertView.findViewById(android.R.id.text1); 117 tv2.setText(item.get(position).get(DATE) + ""); 118 119 break; 120 121 default: 122 break; 123 } 124 125 return convertView; 126 } 127 128 } 129 }
1 package com.lixu.fragment_test; 2 3 4 import android.app.Fragment; 5 import android.os.Bundle; 6 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.ImageView; 11 12 class MyFragment3 extends Fragment { 13 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 } 18 19 // 创建view 20 @Override 21 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 22 23 View view = inflater.inflate(R.layout.image, null); 24 25 return view; 26 } 27 28 // 创建view完毕 29 @Override 30 public void onViewCreated(View view, Bundle savedInstanceState) { 31 super.onViewCreated(view, savedInstanceState); 32 33 ImageView iv = (ImageView) view.findViewById(R.id.iv); 34 iv.setImageResource(R.drawable.faxianjiemian); 35 } 36 }
1 package com.lixu.fragment_test; 2 3 4 import android.app.Fragment; 5 import android.os.Bundle; 6 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.ImageView; 11 12 class MyFragment4 extends Fragment { 13 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 } 18 19 // 创建view 20 @Override 21 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 22 23 View view = inflater.inflate(R.layout.image, null); 24 25 return view; 26 } 27 28 // 创建view完毕 29 @Override 30 public void onViewCreated(View view, Bundle savedInstanceState) { 31 super.onViewCreated(view, savedInstanceState); 32 33 ImageView iv = (ImageView) view.findViewById(R.id.iv); 34 iv.setImageResource(R.drawable.wojiemian); 35 } 36 }
xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/LinearLayout1" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context="com.lixu.fragment_test.MainActivity" > 8 9 <FrameLayout 10 android:id="@+id/fl" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:layout_weight="1" > 14 FrameLayout> 15 16 <LinearLayout 17 android:id="@+id/linearlayout" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:orientation="horizontal" > 21 22 <ImageView 23 android:id="@+id/iv1" 24 android:layout_width="90dp" 25 android:layout_height="60dp" 26 android:layout_weight="1" 27 android:background="@drawable/weixin" /> 28 29 <ImageView 30 android:id="@+id/iv2" 31 android:layout_width="90dp" 32 android:layout_height="60dp" 33 android:layout_weight="1" 34 android:background="@drawable/tongxunlu" /> 35 36 <ImageView 37 android:id="@+id/iv3" 38 android:layout_width="90dp" 39 android:layout_height="60dp" 40 android:layout_weight="1" 41 android:background="@drawable/faxian" /> 42 43 <ImageView 44 android:id="@+id/iv4" 45 android:layout_width="90dp" 46 android:layout_height="60dp" 47 android:layout_weight="1" 48 android:background="@drawable/wo" /> 49 LinearLayout> 50 51 LinearLayout>
1 xml version="1.0" encoding="utf-8"?> 2 <ImageView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/iv" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" > 6 7 ImageView>
1 xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <ListView 8 android:id="@+id/lv" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" > 11 ListView> 12 13 LinearLayout>