5. Fragment详解

onCreateView是Fragment生命周期方法中最重要的一个。因为在该 方法中会创建在Fragment中显示的View。 

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){

     // 装载布局文件

     View view = inflater.inflate(R.layout.my_fragment, null); TextView textview =

           (TextView)view.findViewById(R.id.textview);                  

     testview.setText("Fragment Test");

    return view;

Fragment与Activity之间可以通过Fragment.setArguments方法向 Fragment传递参数值,并且可以通过Fragment.getArguments方法获取 这些传递的参数值。 

 范例1:第一个Fragment程序。

 1 import android.app.Activity;
 2 import android.os.Bundle;
 3 
 4 public class FirstFragmentActivity extends Activity {
 5     @Override
 6     public void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_first_fragment);
 9     }
10 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent" >
 4 
 5     <fragment
 6         android:id="@+id/titles"
 7         android:layout_width="match_parent"
 8         android:layout_height="match_parent"
 9 
10        class="cn.eoe.first.fragment.LeftFragment" />
11 </LinearLayout> 
 1 package cn.eoe.first.fragment;
 2 
 3 import java.io.InputStream;
 4 
 5 import android.app.Fragment;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.AdapterView;
12 import android.widget.AdapterView.OnItemClickListener;
13 import android.widget.ArrayAdapter;
14 import android.widget.ListView;
15 import android.widget.TextView;
16 
17 public class LeftFragment extends Fragment implements OnItemClickListener {
18 
19     private String[] data = new String[] { "灵魂战车2", "变形金刚3:月黑之时", "敢死队2" };
20     private ListView listView;
21 
22     @Override
23     public View onCreateView(LayoutInflater inflater, ViewGroup container,
24             Bundle savedInstanceState) {
25         View view = inflater.inflate(R.layout.left_fragment, null);
26         listView = (ListView) view.findViewById(R.id.listview_movie_list);
27         listView.setOnItemClickListener(this);
28         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
29                 getActivity(), android.R.layout.simple_list_item_activated_1,
30                 data);
31         listView.setAdapter(arrayAdapter);
32         listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
33 
34         return view;
35     }
36 
37     @Override
38     public void onItemClick(AdapterView<?> parent, View view, int position,
39             long id) {
40         try {
41 
42             TextView textView = (TextView) getActivity().findViewById(
43                     R.id.textview_detail);
44             InputStream is = getActivity().getResources().getAssets()
45                     .open("m" + position);
46             byte[] buffer = new byte[1024];
47             int count = is.read(buffer);
48             String detail = new String(buffer, 0, count, "utf-8");
49             if (textView == null) {
50                 Intent intent = new Intent(getActivity(), DetailActivity.class);
51                 intent.putExtra("detail", detail);
52                 startActivity(intent);
53             } else {
54                 textView.setText(detail);
55             }
56             is.close();
57         } catch (Exception e) {
58             // TODO: handle exception
59         }
60 
61     }
62 
63 }
 1 public class DetailActivity extends Activity {
 2     @Override
 3     public void onCreate(Bundle savedInstanceState) {
 4         super.onCreate(savedInstanceState);
 5         setContentView(R.layout.activity_detail);
 6         TextView detail = (TextView) findViewById(R.id.textview_detail);
 7         detail.setText(getIntent().getExtras().getString("detail"));
 8 
 9     }
10 }
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent" >
 4 
 5     <fragment
 6         android:id="@+id/details"
 7         android:layout_width="match_parent"
 8         android:layout_height="match_parent"
 9         class="cn.eoe.first.fragment.RightFragment" />
10 
11 </RelativeLayout>
1 public class RightFragment extends Fragment {
2     @Override
3     public View onCreateView(LayoutInflater inflater, ViewGroup container,
4             Bundle savedInstanceState) {
5         View view = inflater.inflate(R.layout.right_fragment, null);
6         return view;
7     }
8 }

范例2:向Fragment传递数据,并获取传递数据的值

activity_fragment_argument.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical" >
 5 
 6     <Button
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:layout_marginBottom="10dp"
10         android:onClick="onClick_SendData"
11         android:text="向Fragment传递数据" />
12 
13     <FrameLayout
14         android:id="@+id/fragment_container1"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content" />
17 
18 </LinearLayout>
 1 import android.app.Activity;
 2 import android.app.FragmentManager;
 3 import android.app.FragmentTransaction;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.EditText;
 7 import android.widget.Toast;
 8 
 9 public class FragmentArgumentActivity extends Activity {
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_fragment_argument);
15     }
16 
17     // 向Fragment传递数据
18     public void onClick_SendData(View view) {
19         // 获取传递的数据(页面)
20         MyFragment fragment = new MyFragment();
21 
22         Bundle bundle = new Bundle();
23 
24         bundle.putString("name", "Hello Fragment1");
25         fragment.setArguments(bundle);
26         FragmentManager fragmentManager = getFragmentManager();
27         FragmentTransaction fragmentTransaction = fragmentManager
28                 .beginTransaction();
29         fragmentTransaction.add(R.id.fragment_container1, fragment, "fragment");
30 
31         fragmentTransaction.commit();
32 
33         Toast.makeText(this, "数据已成功传递.", Toast.LENGTH_LONG).show();
34 
35     }
36 
37     // 获取传递的数据
38     public void onClick_ShowArgument(View view) {
39         EditText editText = (EditText) findViewById(R.id.edittext);
40 
41         String name = getFragmentManager().findFragmentByTag("fragment")
42                 .getArguments().getString("name");
43         editText.setText(name);
44     }
45 }
 1 import android.app.Fragment;
 2 import android.os.Bundle;
 3 import android.util.Log;
 4 import android.view.LayoutInflater;
 5 import android.view.View;
 6 import android.view.ViewGroup;
 7 
 8 public class MyFragment extends Fragment {
 9     @Override
10     public View onCreateView(LayoutInflater inflater, ViewGroup container,
11             Bundle savedInstanceState) {
12         View view = inflater.inflate(R.layout.my_fragment, container, false);
13         return view;
14     }
15 
16     @Override
17     public void onDestroyView() {
18         Log.d("name", getArguments().getString("name"));
19         super.onDestroyView();
20     }
21 
22 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical" >
 5 
 6     <EditText
 7         android:id="@+id/edittext"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content" />
10 
11     <Button
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:onClick="onClick_ShowArgument"
15         android:text="获取传递的数据" />
16 
17 </LinearLayout>
原文地址:https://www.cnblogs.com/androidsj/p/3871893.html