试水Fragment(2)-动态添加Fragment

来源:互联网 发布:21级登陆艇升级数据 编辑:程序博客网 时间:2024/06/06 18:11

现在我们动态添加Fragment,参考最好的教材,官方文档:http://tool.oschina.net/apidocs/apidoc?api=android/reference

说一条小定律:Manager一般由上下文get,如下FragmentManager。

Performing Fragment Transactions

A great feature about using fragments in your activity is the ability to add, remove, replace, and perform other actions with them, in response to user interaction. Each set of changes that you commit to the activity is called a transaction and you can perform one using APIs in FragmentTransaction. You can also save each transaction to a back stack managed by the activity, allowing the user to navigate backward through the fragment changes (similar to navigating backward through activities).

You can acquire an instance of FragmentTransaction from the FragmentManager like this:

FragmentManager fragmentManager = getFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//开启事务,要么同时成功
现在我们通过动态添加Fragment实现一个手机横竖屏切换变换Fragment的效果:

1.获取手机分辨率:

        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);        int width = wm.getDefaultDisplay().getWidth();        int height = wm.getDefaultDisplay().getHeight();
2.通过高和宽判断横竖屏,

3.通过上下文获取FragmentManager,开启事务,调用事务的replace方法:

        FragmentManager fragmentManager = getFragmentManager();        FragmentTransaction transaction = fragmentManager.beginTransaction();        if(height > width){            //竖屏            //android.R.id.content代表当前手机的窗体            transaction.replace(android.R.id.content,new FragmentV());        }else{            //横屏            transaction.replace(android.R.id.content,new FragmentH());        }
4.最好一定记得commit:

        transaction.commit();


效果图:




核心源代码:

package com.quan.car.fragmenttest;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.WindowManager;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取手机分辨率        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);        int width = wm.getDefaultDisplay().getWidth();        int height = wm.getDefaultDisplay().getHeight();        //判断横竖屏        //通过上下文获取FragmentManager,开启事务        FragmentManager fragmentManager = getFragmentManager();        FragmentTransaction transaction = fragmentManager.beginTransaction();        if(height > width){            //竖屏            //android.R.id.content代表当前手机的窗体            transaction.replace(android.R.id.content,new FragmentV());        }else{            //横屏            transaction.replace(android.R.id.content,new FragmentH());        }        //记得commit        transaction.commit();    }}


package com.quan.car.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;/** * Created by 权兴权意 on 2016/9/1. */public class FragmentH extends Fragment {    //Fragment build.gradle-defaultConfig-minSdkVersion>11    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_h,null);        return view;    }}

布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Fragment-横屏"        android:textSize="22sp"        /></LinearLayout>














0 0
原创粉丝点击