45 Android activity 向 fragment 传值

  • Post author:
  • Post category:其他

left.xml (加载到fragment)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#f0f000"
     >

    <TextView
        android:id="@+id/textViewleft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左边" 
       android:layout_gravity="center"
        android:textSize="25sp"
        />

</LinearLayout>

right.xml (加载到fragment)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    
    >

    <TextView
        android:id="@+id/textViewright"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右边" 
       android:layout_gravity="center"
        android:textSize="25sp"
        />

</LinearLayout>

activity_main.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="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#cccccc"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

MianActivity.java 代码

package com.example.android_fragment_activity;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;

public class MainActivity extends Activity {
	private FragmentManager manager;
	private FragmentTransaction transaction ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		manager=getFragmentManager();
		transaction=manager.beginTransaction();
		
		LeftFragment leftFragment=new LeftFragment();
		Bundle bundle=new Bundle();
		bundle.putInt("id", 10010);
		leftFragment.setArguments(bundle);
		
		
		RightFragment rightFragment=new RightFragment();
		Bundle bundle2=new Bundle();
		bundle2.putString("name", "Jack");
		rightFragment.setArguments(bundle2);
		
		transaction.add(R.id.left, leftFragment, "leftFragment");
		transaction.add(R.id.right, rightFragment, "rightFragment");
		transaction.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

LeftFragment extends fragment

package com.example.android_fragment_activity;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class LeftFragment extends Fragment {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view =inflater.inflate(R.layout.left, null);
		TextView textview=(TextView)view.findViewById(R.id.textViewleft);
		Bundle bundle=getArguments();
		int id=bundle.getInt("id");
		textview.setText(""+id);
		return view;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
}

RightFragment extends Fragment

package com.example.android_fragment_activity;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class RightFragment extends Fragment {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view =inflater.inflate(R.layout.right, null);
		TextView textView=(TextView)view.findViewById(R.id.textViewright);
		Bundle bundle=getArguments();
		String str=bundle.getString("name");
		textView.setText(str);
		return view;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
}

左边显示10010   右边显示Jack