标签:
Fragment必须被“嵌入”Activity中使用,因此虽然Fragment也拥有自己的生命周期,但Fragment的生命周期会受它所在的Activity的生命周期的控制。
Fragment与Activity通信
为了在Activity中显示Activity,还必须将Fragment添加到Activity中。将Fragment添加到Activity中有如下两种方式:
1,在布局文件中使用<fragment.../>元素添加Fragment,<fragment.../>元素的android:name属性指定Fragment的实现类。
2,在java代码中通过FragmentTransaction对象的add()方法来添加Fragment。
将Fragment添加到Activity之后,Fragment必须与Activity交互信息,这就需要Fragment能获取它所在的Activity,Activity也能获取它所在的任意Fragment。可以按如下方法进行:
Fragment获取它所在的Activity:调用Fragment的getActivity()方法即可返回它所在的Activity。
Activity获取它包含的Fragment:调用Activity关联的FragmentManager的findFragmentById(int id)或findFragmentByTag(String tag)方法即可获取指定的Fragment。
1 package com.example.day21_06fregmentcommuniction; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 public class MainActivity extends Activity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 } 13 }
界面左侧的"头条","IT新闻","娱乐","体育","历史"Fragment
1 package com.example.day21_06fregmentcommuniction; 2 3 import android.app.Fragment; 4 import android.app.FragmentManager; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.AdapterView; 10 import android.widget.AdapterView.OnItemClickListener; 11 import android.widget.ArrayAdapter; 12 import android.widget.ListView; 13 14 public class Fragment_tab extends Fragment implements OnItemClickListener { 15 16 String[] itmes = {"头条","IT新闻","娱乐","体育","历史"}; 17 int[] imagmes = { 18 R.drawable.a1, 19 R.drawable.a2, 20 R.drawable.a3, 21 R.drawable.a4, 22 R.drawable.a5 }; 23 24 @Override 25 public View onCreateView(LayoutInflater inflater, ViewGroup container, 26 Bundle savedInstanceState) { 27 // TODO Auto-generated method stub 28 ListView list = new ListView(getActivity()); 29 30 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,itmes); 31 list.setAdapter(adapter); 32 33 list.setOnItemClickListener(this); 34 return list;// super.onCreateView(inflater, container, savedInstanceState); 35 } 36 37 // 当用户点击左侧的Fragment的某个一listitme时,应该在响应函数内 改变右侧Fragment的内容 38 @Override 39 public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 40 // TODO Auto-generated method stub 41 //先通过FragmentManager将我们需要操作的Fragment找到,然后在调用它的方法来操作修改它的内容 42 43 FragmentManager fm = getFragmentManager(); 44 Fragment_content fragment_content =(Fragment_content) fm.findFragmentById(R.id.fragment_content); 45 fragment_content.setTitle(itmes[position]); 46 fragment_content.setPhoto(imagmes[position]); 47 } 48 }
界面右侧的内容Fragment
1 package com.example.day21_06fregmentcommuniction; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.ImageView; 9 import android.widget.TextView; 10 11 public class Fragment_content extends Fragment { 12 13 private TextView tv_tile; 14 private ImageView iv_photo; 15 16 @Override 17 public View onCreateView(LayoutInflater inflater, ViewGroup container, 18 Bundle savedInstanceState) { 19 // 加载fragment_content.xml布局文件 20 View v =inflater.inflate(R.layout.fragment_content, null); 21 tv_tile = (TextView) v.findViewById(R.id.tv_title); 22 iv_photo = (ImageView ) v.findViewById(R.id.iv_photo); 23 return v; 24 } 25 26 public void setTitle(String s){ 27 tv_tile.setText(s); 28 } 29 30 public void setPhoto(int i){ 31 iv_photo.setImageResource(i); 32 } 33 }
MainActivity.xml中对Activity中左侧Fragment和右侧Fragment进行比例大小等设置
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 6 tools:context="com.example.day21_06fregmentcommuniction.MainActivity" 7 android:orientation="horizontal" > 8 9 <fragment 10 android:id="@+id/fragment_tab" 11 android:layout_width="0dp" 12 android:layout_height="fill_parent" 13 android:layout_weight="3" 14 android:name="com.example.day21_06fregmentcommuniction.Fragment_tab"/> 15 16 <fragment 17 android:id="@+id/fragment_content" 18 android:layout_width="0dp" 19 android:layout_height="fill_parent" 20 android:layout_weight="7" 21 android:name="com.example.day21_06fregmentcommuniction.Fragment_content"/> 22 23 </LinearLayout>
右侧Fragment布局文件
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 <TextView 8 android:id="@+id/tv_title" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_gravity="center_horizontal" 12 android:background="#00FF00"/> 13 <ImageView 14 android:id="@+id/iv_photo" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:src="@drawable/ic_launcher" 18 /> 19 </LinearLayout>
点击“体育”,将切换到体育Fragment
点击“历史”,将切换到历史Fragment
标签:
原文地址:http://www.cnblogs.com/woodrow2015/p/4547894.html