在要传参的Fragment里面定义一个接口,接口里面有个方法,然后再activity里面实现接口的方法,先看效果图:
package com.example.myandroid.Fragment.Interaction1;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.myandroid.R;
public class ListsFragment extends Fragment {
private ListView mListView;
private String[] names = new String[] { "人类", "猴子", "猩猩", "老虎", "阿凡达",
"德莱尼", "克林姆" };
private onSetText monsettext;
public interface onSetText {
public void showMessage(String message);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
monsettext=(onSetText) activity;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.lists_fragment_layout, container,
false);
mListView = (ListView) v.findViewById(R.id.lists_list);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_expandable_list_item_1, names);
mListView.setAdapter(adapter);
// mListView.setEmptyView(mProgressBar);//记住setEmptyView()方法,当没内容时进度条又显示出来
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ArrayAdapter<String>adpter=(ArrayAdapter<String>) parent.getAdapter();
String itemmessage=adpter.getItem(position);
monsettext.showMessage(itemmessage);
}
});
}
}
package com.example.myandroid.Fragment.Interaction1;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.myandroid.R;
public class ContentsFragment extends Fragment {
private static TextView mTextView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.contents_fragment,container,false);
mTextView=(TextView) v.findViewById(R.id.contents_fragment_text);
mTextView.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
mTextView.setText("每项内容");
return v;
}
public static void setMessageToTextView(String message){
mTextView.setText(message);
}
}
package com.example.myandroid.Fragment.Interaction1;
import com.example.myandroid.R;
import com.example.myandroid.Fragment.Interaction1.ListsFragment.onSetText;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
public class FragmentActivity extends Activity implements onSetText {
ContentsFragment contentsFragment;
ListsFragment listsfragment;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fragment_interaction_layout);
contentsFragment = new ContentsFragment();
listsfragment=new ListsFragment();
FragmentTransaction tran =getFragmentManager()
.beginTransaction();
tran.add(R.id.lists_fragment,listsfragment).commit();
// getFragmentManager().beginTransaction()
// .add(R.id.lists_fragment, new ListsFragment()).commit();
getFragmentManager().beginTransaction()
.add(R.id.contents_fragment, new ContentsFragment()).commit();
}
@Override
public void showMessage(String message) {
// Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
contentsFragment.setMessageToTextView(message);
}
}
<?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" >
<ListView
android:id="@+id/lists_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?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"
>
<TextView
android:id="@+id/contents_fragment_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="30sp"
/>
</LinearLayout>
<?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" >
<FrameLayout
android:id="@+id/lists_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/contents_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>