码迷,mamicode.com
首页 > 其他好文 > 详细

Fragment解析创建和传参,动态添加fragment

时间:2015-01-24 11:39:51      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:android   通信   

一下是个人的一些总结。

为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布局文件并返回


 

然后打开或新建activity_main.xml作为主Activity的布局文件,在里面加入两个Fragment的引用,使用android:name前缀来引用具体的Fragment:
<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>  


 

动态添加fragment将activity_main.xml中的两个fragmnet控件删除,只留下linearlayout布局。为这个布局设置一个id
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(); 
        } 
    }  
}

首先,我们要获取屏幕的宽度和高度,然后进行判断,如果屏幕宽度大于高度就添加fragment1,如果高度大于宽度就添加fragment2。动态添加Fragment主要分为4步:

1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。

2.开启一个事务,通过调用beginTransaction方法开启。

3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。

4.提交事务,调用commit方法提交。
 
//fragment的生命周期,与activity不同的
onAttach方法:Fragment和Activity建立关联的时候调用。
onCreateView方法:为Fragment加载布局时调用。
onActivityCreated方法:当Activity中的onCreate方法执行完后调用。
onDestroyView方法:Fragment中的布局被移除时调用。
onDetach方法:Fragment和Activity解除关联的时候调用。


 

Fragment之间通信:使用了接口传参和getActivity两种方式
fragment1.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/onelist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

fragment2.xml布局:
<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>

fragment1:
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);
    }

fragment2:
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);
    }
}

MainActivity:
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);
    }
}

 

Fragment解析创建和传参,动态添加fragment

标签:android   通信   

原文地址:http://blog.csdn.net/sinat_23134455/article/details/43083995

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!