Fragment传值

***Fragment间传值

*1.利用setArguments(bundle)方法

案列实现效果:当点击左边传值按钮后,右边的fragment才出现界面,之后点击取值按钮,会将值呈现在textView中

activity_main.xml中:

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="passValue"
    android:text="传值"/>

<FrameLayout 
    android:id="@+id/fl_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#999"
    android:layout_weight="1">
</FrameLayout>

content_fragment.xml中:

 <Button 
    android:id="@+id/btn_getValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="取值"/>

<TextView 
    android:id="@+id/tv_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

MainActivity.java中:

public class MainActivity extends Activity {
	private FragmentManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		manager = getFragmentManager();
	}
	
	//当点击传值按钮时,Fragment才加入到Activity中,并传值
	public void passValue(View view){
		FragmentTransaction transaction = manager.beginTransaction();
		ContentFragment fragment = new ContentFragment();	
		Bundle args = new Bundle();
		args.putString("key", "我是Activity传递的信息");
		fragment.setArguments(args);//传递数据
		transaction.add(R.id.fl_container,fragment);
		transaction.commit();
	}
}

ContentFragment.java中:

public class ContentFragment extends Fragment {
	private Bundle arguments;
	private Button btnGetValue;
	private TextView tvValue;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.content_fragment, null);
		btnGetValue = (Button) view.findViewById(R.id.btn_getValue);
		tvValue = (TextView) view.findViewById(R.id.tv_value);
		
		arguments = getArguments();
		
		//在按钮点击事件中取值
		btnGetValue.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(arguments!=null){
					String value = arguments.getString("key", "");
					tvValue.setText("接收的信息="+value);
				}				
			}
		});
		
		return view;
	}
}

*2.接口回调

案列实现效果:左右界面开始都已经显示,当点击左边传值按钮后,再点击取值按钮,会将值呈现在textView中

activity_main.xml中:

<Button  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="传值"
    android:onClick="setValue"
    android:textSize="20sp"/>

<FrameLayout 
    android:id="@+id/fragment_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#999">    
</FrameLayout>

content_fragment.xml中:

<Button 
    android:id="@+id/btn_getValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="getValue"
    android:text="取值"
    android:textSize="20sp"/>

<TextView 
    android:id="@+id/tv_getValue"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textSize="20sp"/>

MainActivity.java中:

public class MainActivity extends Activity {
	private FragmentManager manger;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		manger = getFragmentManager();
		FragmentTransaction transaction = manger.beginTransaction();
		ContentFragment fragment = new ContentFragment();
		transaction.add(R.id.fragment_container, fragment, "content");//第三个参数tag:是fragment唯一的标识
		transaction.commit();
		
	}
	public void setValue(View view){
		//根据标识找到一个fragment对象;
		ContentFragment fragment = (ContentFragment) manger.findFragmentByTag("content");
		fragment.setMsg("我在Activity中");
	}
}

ContentFragment.java中:

public class ContentFragment extends Fragment {
	private Button btn_getValue;
	private TextView tv_getValue;
	private String msg;
	
	public void setMsg(String msg){
		this.msg = msg;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.content_fragment, null);
		btn_getValue = (Button) view.findViewById(R.id.btn_getValue);
		tv_getValue = (TextView) view.findViewById(R.id.tv_getValue);
		btn_getValue.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(msg!=null){
					tv_getValue.setText(msg);
				}else{
					tv_getValue.setText("值为空!");
				}
			}
		});
		return view;
	}
}

原文地址:https://www.cnblogs.com/SanguineBoy/p/9791077.html