Fragment详解(一)--->核心基础以及Fragment与Activity传递数据完整示例

MainActivity如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testsimplefragment0;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.FragmentTransaction;  
  6. import cc.testsimplefragment0.TitlesListFragment.TitlesListFragmentCallBack;  
  7. /** 
  8.  * Demo描述: 
  9.  * Fragment基本使用以及Fragment与Activity之间数据的传递 
  10.  *  
  11.  * 参考资料: 
  12.  * Android疯狂讲义(第二版) 
  13.  *  
  14.  * 备注说明: 
  15.  * 利用接口实现Fragment与Activity的信息传递. 
  16.  * 这个思路是不错的. 
  17.  * 在此总结一下Fragment与Activity之间交换数据的方式: 
  18.  * 1 Activity向Fragment传递数据 
  19.  *   fragment.setArguments(bundle) 
  20.  * 2 Fragment向Activity传递数据 
  21.  *   在Fragment内部定义一个回调接口.让包含该Fragment的 
  22.  *   Activity实现该接口.这样Fragment就可调用该回调方法 
  23.  *   将数据传给Activity 
  24.  *  
  25.  */  
  26. public class MainActivity extends Activity implements TitlesListFragmentCallBack{  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.           
  33.     }  
  34.   
  35.     //实现TitlesListFragmentCallBack接口中的方法  
  36.     @Override  
  37.     public void onItemSelected(int index) {  
  38.         DetailFragment detailFragment=new DetailFragment();  
  39.         Bundle bundle=new Bundle();  
  40.         bundle.putInt(Data.ID, index);  
  41.         //向detailFragment传入参数  
  42.         detailFragment.setArguments(bundle);  
  43.           
  44.         //开始Fragment的事务Transaction  
  45.         FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();  
  46.         //替换容器(container)原来的Fragment  
  47.         fragmentTransaction.replace(R.id.relativeLayoutContainer, detailFragment);   
  48.         //设置转换效果  
  49.         fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);   
  50.         //将事务添加到Back栈.即按下Back键时回到替换Fragment之前的状态.类似于Activity的返回  
  51.         fragmentTransaction.addToBackStack(null);  
  52.         //提交事务  
  53.         fragmentTransaction.commit();   
  54.     }  
  55.   
  56.       
  57.   
  58. }  


TitlesListFragment如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testsimplefragment0;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ListFragment;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.ListView;  
  9. /** 
  10.  * 备注说明: 
  11.  * 因为是继承自ListFragment 
  12.  * 所以不用覆写onCreateView()方法 
  13.  * 
  14.  */  
  15. public class TitlesListFragment extends ListFragment {  
  16.     private TitlesListFragmentCallBack mTitlesListFragmentCallBack;  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         //设置适配器  
  21.         ArrayAdapter<String> arrayAdapter=  
  22.         new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_checked, Data.TITLES);  
  23.         setListAdapter(arrayAdapter);   
  24.           
  25.     }  
  26.       
  27.     //当该Fragment被添加,显示到Activity时调用该方法  
  28.     //在此判断显示到的Activity是否已经实现了接口  
  29.     @Override  
  30.     public void onAttach(Activity activity) {  
  31.         super.onAttach(activity);  
  32.         if (!(activity instanceof TitlesListFragmentCallBack)) {  
  33.             throw new IllegalStateException("TitlesListFragment所在的Activity必须实现TitlesListFragmentCallBack接口");  
  34.         }  
  35.         mTitlesListFragmentCallBack=(TitlesListFragmentCallBack) activity;  
  36.     }  
  37.       
  38.     //当该Fragment从它所属的Activity中被删除时调用该方法  
  39.     @Override  
  40.     public void onDetach() {  
  41.         super.onDetach();  
  42.         mTitlesListFragmentCallBack=null;  
  43.     }  
  44.       
  45.     //点击ListFragment中某个条目时调用该方法  
  46.     @Override  
  47.     public void onListItemClick(ListView l, View v, int position, long id) {  
  48.         super.onListItemClick(l, v, position, id);  
  49.         mTitlesListFragmentCallBack.onItemSelected(position);  
  50.         //设置ListView为单选模式  
  51.         getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);  
  52.         //显示选中的条目  
  53.         getListView().setItemChecked(position, true);   
  54.     }  
  55.       
  56.     //定义一个业务接口  
  57.     //该Fragment所在Activity需要实现该接口  
  58.     //该Fragment将通过此接口与它所在的Activity交互  
  59.     public interface TitlesListFragmentCallBack{  
  60.         public void onItemSelected(int index);  
  61.     }  
  62.   
  63. }  


DetailFragment如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testsimplefragment0;  
  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.TextView;  
  9.   
  10. public class DetailFragment extends Fragment {  
  11.     private int id=0;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         if (getArguments().containsKey(Data.ID)) {  
  16.             id=getArguments().getInt(Data.ID);  
  17.         }  
  18.     }  
  19.       
  20.     @Override  
  21.     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {  
  22.         View view=inflater.inflate(R.layout.detail_fragment, container, false);  
  23.         TextView titleTextView=(TextView) view.findViewById(R.id.titleTextView);  
  24.         titleTextView.setText(Data.TITLES[id]);  
  25.           
  26.         TextView detailTextView=(TextView) view.findViewById(R.id.detailTextView);  
  27.         detailTextView.setText(Data.DETAILS[id]);  
  28.           
  29.         return view;  
  30.     }  
  31.   
  32. }  


Data如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testsimplefragment0;  
  2. public final class Data {  
  3.     public static final String ID="id";  
  4.     //标题  
  5.     public static final String[] TITLES = { "标题1","标题2""标题3","标题4"};  
  6.     //内容  
  7.     public static final String[] DETAILS = { "内容1","内容2""内容3","内容4"};  
  8. }  


main.xml如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 定义一个水平排列的LinearLayout,并指定使用中等分隔条 -->  
  3. <LinearLayout  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:orientation="horizontal"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:layout_marginLeft="16dp"  
  9.     android:layout_marginRight="16dp"  
  10.     android:divider="?android:attr/dividerHorizontal"  
  11.     android:showDividers="middle">  
  12.       
  13.     <!-- 添加一个Fragment -->  
  14.     <fragment  
  15.         android:id="@+id/titlesListFragment"  
  16.         android:name="cc.testsimplefragment0.TitlesListFragment"  
  17.         android:layout_width="0dp"  
  18.         android:layout_height="match_parent"  
  19.         android:layout_weight="1" />  
  20.       
  21.     <!-- 添加一个RelativeLayout容器 -->  
  22.     <RelativeLayout  
  23.         android:id="@+id/relativeLayoutContainer"  
  24.         android:layout_width="0dp"  
  25.         android:layout_height="match_parent"  
  26.         android:layout_weight="3" />  
  27.       
  28. </LinearLayout>  


detail_fragment.xml如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.     <!-- 显示标题 -->  
  8.     <TextView  
  9.         android:id="@+id/titleTextView"  
  10.         style="?android:attr/textAppearanceLarge"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:padding="16dp" />  
  14.   
  15.     <!-- 显示详细 -->  
  16.     <TextView  
  17.         android:id="@+id/detailTextView"  
  18.         style="?android:attr/textAppearanceMedium"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="match_parent"  
  21.         android:padding="16dp" />  
  22.   
  23. </LinearLayout>  


AndroidManifest.xml如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cc.testsimplefragment0"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="14"  
  9.         android:targetSdkVersion="14" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="cc.testsimplefragment0.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </manifest>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值