参数传入使用setArgment(bundle),参数回传使用回调方法 mainActivity主布局
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout">
package com.demo.fragmenttest;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment1 fragment1 = new Fragment1();
getFragmentManager().beginTransaction().replace(R.id.main_layout,fragment1).commit();
}
}
Fragment1的布局文件
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/image_result" />
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="点击传递参数给2"
android:id="@+id/button1"
android:background="#ffff55ac"/>
package com.demo.fragmenttest;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
/**
* Created by Administrator on 2015/4/21.
*/
public class Fragment1 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_layout_1,container,false);
Button button =(Button)view.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment2 fragment2 =Fragment2.newInstance("hello from fragment1");
fragment2.setResultListener(new Fragment2.ImageSelectInterface() {
@Override
public void onImageSelect(int selectID) {
ImageView im= (ImageView)getView().findViewById(R.id.image_result);
im.setImageResource(selectID);
}
});
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(Fragment1.this).add(R.id.main_layout, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
fragment2布局文件
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view"/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/images1"
android:id="@+id/imageView1"
android:layout_weight="1"/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/images2"
android:id="@+id/imageView2"
android:layout_weight="1"/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/images3"
android:id="@+id/imageView3"
android:layout_weight="1"/>
package com.demo.fragmenttest;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
/**
* Created by Administrator on 2015/4/21.
*/
public class Fragment2 extends Fragment implements View.OnClickListener {
//使用回调传递参数,定义接口,在fragment1里面重写该接口方法,当点击图像时,传递图像资源ID,并在fragment1中显示该图像
private ImageSelectInterface imageSelectInterface;
public interface ImageSelectInterface {
public void onImageSelect(int selectID);
}
public void setResultListener(ImageSelectInterface i){
imageSelectInterface = i;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_2,container,false);
if(getArguments()!=null){
String mParam = getArguments().getString("param");
TextView tv = (TextView)view.findViewById(R.id.text_view);
tv.setText(mParam);
}
view.findViewById(R.id.imageView1).setOnClickListener(this);
view.findViewById(R.id.imageView2).setOnClickListener(this);
view.findViewById(R.id.imageView3).setOnClickListener(this);
return view;
}
public static Fragment2 newInstance(String string){
Fragment2 fragment2 = new Fragment2();
Bundle args = new Bundle();
args.putString("param",string);
fragment2.setArguments(args);
return fragment2;
}
@Override
public void onClick(View v) {
int id =v.getId();
switch (id){
case R.id.imageView1:
imageSelectInterface.onImageSelect(R.drawable.images1);
break;
case R.id.imageView2:
imageSelectInterface.onImageSelect(R.drawable.images2);
break;
case R.id.imageView3:
imageSelectInterface.onImageSelect(R.drawable.images3);
break;
default:
break;
}
getActivity().getFragmentManager().popBackStack();//回退到第一个fragment
}
}