5. Fragment详解

时间:2023-03-09 18:10:30
5. Fragment详解

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程序。

 import android.app.Activity;
import android.os.Bundle; public class FirstFragmentActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_fragment);
}
}
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <fragment
android:id="@+id/titles"
android:layout_width="match_parent"
android:layout_height="match_parent" class="cn.eoe.first.fragment.LeftFragment" />
</LinearLayout>
 package cn.eoe.first.fragment;

 import java.io.InputStream;

 import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView; public class LeftFragment extends Fragment implements OnItemClickListener { private String[] data = new String[] { "灵魂战车2", "变形金刚3:月黑之时", "敢死队2" };
private ListView listView; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, null);
listView = (ListView) view.findViewById(R.id.listview_movie_list);
listView.setOnItemClickListener(this);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_activated_1,
data);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); return view;
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
try { TextView textView = (TextView) getActivity().findViewById(
R.id.textview_detail);
InputStream is = getActivity().getResources().getAssets()
.open("m" + position);
byte[] buffer = new byte[1024];
int count = is.read(buffer);
String detail = new String(buffer, 0, count, "utf-8");
if (textView == null) {
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("detail", detail);
startActivity(intent);
} else {
textView.setText(detail);
}
is.close();
} catch (Exception e) {
// TODO: handle exception
} } }
 public class DetailActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
TextView detail = (TextView) findViewById(R.id.textview_detail);
detail.setText(getIntent().getExtras().getString("detail")); }
}
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <fragment
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="cn.eoe.first.fragment.RightFragment" /> </RelativeLayout>
 public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, null);
return view;
}
}

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

activity_fragment_argument.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="onClick_SendData"
android:text="向Fragment传递数据" /> <FrameLayout
android:id="@+id/fragment_container1"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>
 import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class FragmentArgumentActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_argument);
} // 向Fragment传递数据
public void onClick_SendData(View view) {
// 获取传递的数据(页面)
MyFragment fragment = new MyFragment(); Bundle bundle = new Bundle(); bundle.putString("name", "Hello Fragment1");
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.fragment_container1, fragment, "fragment"); fragmentTransaction.commit(); Toast.makeText(this, "数据已成功传递.", Toast.LENGTH_LONG).show(); } // 获取传递的数据
public void onClick_ShowArgument(View view) {
EditText editText = (EditText) findViewById(R.id.edittext); String name = getFragmentManager().findFragmentByTag("fragment")
.getArguments().getString("name");
editText.setText(name);
}
}
 import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
return view;
} @Override
public void onDestroyView() {
Log.d("name", getArguments().getString("name"));
super.onDestroyView();
} }
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick_ShowArgument"
android:text="获取传递的数据" /> </LinearLayout>