android fragment参数,fragment之间参数传入和传出

参数传入使用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

}

}

eeb22c179d7f78790a884defeb101a57?fid=572673695-250528-102820793860644&time=1429610400&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-D509mBAlpPwAyHSBWDm6cyMj7LQ%3D&rt=sh&expires=2h&r=635377785&sharesign=unknown&size=c710_u500&quality=100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值