Android Fragment間的通信

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/gxh27954/article/details/68484515

其實進行通信就是一個Fragment把數據傳遞給Activity,然後Activity再傳給另外一個Fragment所以實現了通信。

其中Activity實現了第一個Fragment中定義的一個接口。



展示~

所有代碼如下:


主類:MainActivity


package com.example.fragmenttest;

import android.app.Activity;
import android.os.Bundle;

import com.example.fragmenttest.FragmentB.CallBack;

public class MainActivity extends Activity implements CallBack {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.all);		
	}	

	@Override
	public void showProByName(Bundle bundle) {
		// TODO Auto-generated method stub
		FragmentA fa = new FragmentA();
		fa.setArguments(bundle);
		getFragmentManager().beginTransaction().replace(R.id.kk, fa).commit();
	}
}

showProByName
就是實現了接口


all.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/frag1"
        android:name="com.example.fragmenttest.FragmentB"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <GridLayout
        android:id="@+id/kk"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" >
    </GridLayout>

</LinearLayout>


左邊是一個fragment,右邊是一個隨便的佈局


FragmentB:(B是左邊佈局)

package com.example.fragmenttest;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragmentB extends ListFragment {

	public interface CallBack {
		public void showProByName(Bundle bundle);
	}

	CallBack mCallback;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity != null) {
			mCallback = (MainActivity) activity;
		}

	}

	ArrayList<String> title;
	ArrayList<String> content;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		title = new ArrayList<String>();
		title.add("title1");
		title.add("title2");

		content = new ArrayList<String>();
		content.add("content1");
		content.add("cnotent2");
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, title);
		//ListView list = (ListView) view.findViewById(R.id.listView1);
		this.setListAdapter(adapter);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Bundle bundle = new Bundle();
		bundle.putString("title", title.get(position));
		bundle.putString("content", content.get(position));
		mCallback.showProByName(bundle);
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fb, container, false);
		// getArguments().
		
		return view;

	}
}
左邊的Fragment被點擊後,把數據給Bundle,然後傳給Activity


fb.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>




FragmentA:(A是右邊佈局)

package com.example.fragmenttest;

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

public class FragmentA extends Fragment{

	String title;
	String content;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		title = bundle.getString("title");
		content = bundle.getString("content");
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fa, container, false);
		TextView tv1 = (TextView) view.findViewById(R.id.textView1);
		TextView tv2 = (TextView) view.findViewById(R.id.textView2);
		tv1.setText(title);
		tv2.setText(content);
		//getArguments().
		return view;
		
	}
}

右邊的Fragment接收數據,然後展示在佈局中


fa.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="60dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="60dp" />

</LinearLayout>



完整項目包:http://download.csdn.net/download/gxh27954/9798718

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章