Fragment的创建以及与activity的参数传递

时间:2023-03-09 13:19:39
Fragment的创建以及与activity的参数传递

点击下面不同的TextView变化不同的Fragment

Fragment的创建以及与activity的参数传递

Fragment的创建以及与activity的参数传递

avtivity与Fragment之间传递消息不能使用构造器传递,用bunder传递

首先写一个含有FrameLayout(这个布局最佳),里面最好没有其他的控件的xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zzw.testfragment.MainActivity" > <FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</FrameLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:id="@+id/weixin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="军事"
android:textColor="@android:color/holo_red_light"
android:textSize="30sp" /> <TextView
android:id="@+id/tongxinlu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="IT"
android:textColor="@android:color/holo_green_light"
android:textSize="30sp" /> <TextView
android:id="@+id/faxian"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="娱乐"
android:textColor="@android:color/holo_orange_light"
android:textSize="30sp" /> <TextView
android:id="@+id/me"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="音乐"
android:textColor="@android:color/holo_blue_light"
android:textSize="30sp" />
</LinearLayout> </LinearLayout>

写一个每一个界面要显示item.xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
android:textStyle="bold" /> </RelativeLayout>

写一个继承Fragment的类:

 package com.zzw.testfragment;

 import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; public class TestFragment extends Fragment {
private static final String TAG = "TestFragment";
int image_id;
String content;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = this.getArguments();
image_id = b.getInt("IMAGE");
content = b.getString("CONTENT");
Log.d(TAG, image_id+"--"+content);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_start, null);
return view;
} @Override
public void onViewCreated(View view, Bundle savedInstanceState) { ImageView image = (ImageView) view.findViewById(R.id.image); image.setImageResource(image_id); TextView textView = (TextView) view.findViewById(R.id.textView); textView.setText(content);
} }

MainActivity:

 package com.zzw.testfragment;

 import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import junit.framework.Test;
import junit.framework.TestResult; public class MainActivity extends Activity implements OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); load(setFragmentData(R.drawable.a, "000")); findViewById(R.id.weixin).setOnClickListener(this);
findViewById(R.id.tongxinlu).setOnClickListener(this);
findViewById(R.id.faxian).setOnClickListener(this);
findViewById(R.id.me).setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.weixin:
load(setFragmentData(R.drawable.a, "000"));
break;
case R.id.tongxinlu:
load(setFragmentData(R.drawable.d, "1111"));
break;
case R.id.faxian:
load(setFragmentData(R.drawable.zzzz, "222"));
break;
case R.id.me:
load(setFragmentData(R.drawable.ic_launcher, "333"));
break;
}
} // 加载Fragment
private void load(Fragment fragment) {
FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelayout, fragment);
// addToBackStack添加到回退栈,addToBackStack与ft.add(R.id.fragment, new
// MyFragment())效果相当
// ft.addToBackStack("test");
ft.commit();
} // 设置要传递给Fragment的参数
private Fragment setFragmentData(int image_id, String content) {
Fragment f = new TestFragment(); Bundle b = new Bundle();
b.putInt("IMAGE", image_id);
b.putString("CONTENT", content); f.setArguments(b);
return f;
}
}

相关文章