一下是个人的一些总结。
为fragment创建XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/yulelist"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class Yule extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.yule,container,false);
}
//主要就是加载我们刚刚写好的fragment.xml布局文件并返回
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentdemo.Fragment1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentdemo.Fragment2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取屏幕尺寸
Display display = getWindowManager().getDefaultDisplay();
if (display.getWidth() > display.getHeight()) {
Fragment1 fragment1 = new Fragment1();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit();
} else {
Fragment2 fragment2 = new Fragment2();
getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/onelist"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="555555555555555"
android:layout_weight="3"
android:id="@+id/one"/>
</LinearLayout>
public class Fragment1 extends Fragment implements AdapterView.OnItemClickListener{
private ListView listView;
private String str[]={"sssss","ddddd","ffffff","gggggg","hhhhhh","jj","mmmm"};
//传参接口
private OnHeadlineSelectedListener mOnHeadlineSelectedListener;
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
mybaseAdapter = (MybaseAdapter) adapterView.getAdapter();
String item=mybaseAdapter.getItem(i).toString();
//使用接口传参
// mOnHeadlineSelectedListener.onSelectItemClick(item);
//使用getActivity()获取fragment2的TextView进行传参
TextView textView=(TextView)getActivity().findViewById(R.id.one);
Log.e("getActivity()",getActivity()+"");
textView.setText(item);
}
//定义传参的接口
public interface OnHeadlineSelectedListener{
public void onSelectItemClick(String message);
}
//和activity建立关系
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mOnHeadlineSelectedListener=(OnHeadlineSelectedListener)activity;
}
//为这个fragment加载布局
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment1,container,false);
listView=(ListView)view.findViewById(R.id.onelist);
return view;
}
//当Activity加载完成后将数据用adapter存入
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter arrayAdapter=new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,str);
}
public class Fragment2 extends android.support.v4.app.Fragment{
private TextView mtext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment2,container,false);
mtext= (TextView) view.findViewById(R.id.one);
mtext.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
// mtext.setText("内容");
return view;
}
//写个方法为text放入参数
public void setMtext(String message){
mtext.setText(message);
}
}
public class MyActivity extends FragmentActivity implements Fragment1.OnHeadlineSelectedListener{
Fragment2 fragment2;
Fragment1 fragment1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fragment1=new Fragment1();
fragment2=new Fragment2();
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.onefragment,fragment1);
fragmentTransaction.add(R.id.twofragment,fragment2);
fragmentTransaction.commit();
}
@Override
public void onSelectItemClick(String message) {
//(( Fragment2)fragment2).setMtext(message);
fragment2.setMtext(message);
}
}
原文:http://blog.csdn.net/sinat_23134455/article/details/43083995