其实进行通信就是一个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>
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接收数据,然后展示在布局中
<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.youkuaiyun.com/download/gxh27954/9798718