美文网首页Android技术知识Android开发
Fragment的导包问题和相关注意点

Fragment的导包问题和相关注意点

作者: Jon0418 | 来源:发表于2017-04-04 23:55 被阅读126次

    我们知道android sdk有个v4包,这个包和v7一样,主要是用来实现一些功能的低版本兼容的。Fragment的正式推出是在3.0后,如果我们要兼容到1.6版本的Android版本,那我们就需要使用v4包中的Fragment。

    先看看正常使用(不是v4包)一般需要导入哪些包:

    //适用于3.0及以上安卓版本
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    

    </br>

    使用v4包中的Fragment:

    //适用于1.6及以上安卓版本
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    

    现在在Android Studio上新建Module,默认是会添加v4包和v7包的,所以还是挺方便的。

    特别要注意几个坑(这才是本文重点):

    1. Fragment中的引用的包和Fragment所在activity引用的包要保持一致,不然编译会出错,用v4就都用v4,用sdk就都用sdk

    2. 如果是用的v4包,那么请注意,如果使用fragment的activity的布局里使用了<fragment/>标签,那么activity必须继承FragmentActivity

    3. 在获取FragmentManager实例的时候,如果用的v4包,那么应该这样写:FragmentManager manager = getSupportFragmentManager();
      否则这样写:FragmentManager manager = getFragmentManager();

    说句实话:7.0都出来了,v4其实没必要用了

    </br>

    顺便看一下Fragment基本用法(非v4包),两个fragment,一个通过布局添加,一个通过代码在MainActivity中添加:

    </br>

    新建两个fragment继承自Fragment,具体怎么写,不详细展开了,很复杂
    具体看这篇博客http://blog.csdn.net/lilu_leo/article/details/7671533

    一般来讲要重写这几个方法

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_blank, container, false);
        }
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
        }
    

    这里例子简单,这样写就够了

    接着两个fragment的布局:
    fragment_blank

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="dh.fragmenttest.BlankFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="苹果"
            android:background="@android:color/holo_red_dark"/>
    
    </FrameLayout>
    

    fragment_blank2

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="dh.fragmenttest.BlankFragment2">
    
        <!-- TODO: Update blank fragment layout -->
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="加载viewStub"
            android:gravity="center"
            android:background="@android:color/holo_blue_dark"/>
    
    </FrameLayout>
    

    </br>

    activity_main

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"    
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="dh.fragmenttest.MainActivity">
    
        <LinearLayout
            android:id="@+id/fragment_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <fragment
                android:name="dh.fragmenttest.BlankFragment2"
                android:layout_width="match_parent"
                android:layout_height="40dp">
            </fragment>
        </LinearLayout>
        
    </LinearLayout>
    

    最后activity

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            init();
        }
    
        private void init() {
    
            FragmentManager manager = getFragmentManager();
    
            FragmentTransaction transaction = manager.beginTransaction();
    
            BlankFragment fragment = new BlankFragment();
    
            transaction.add(R.id.fragment_layout, fragment);
    
            transaction.commit();
        }
    }
    

    </br>
    </br>

    2017/4/4

    相关文章

      网友评论

        本文标题:Fragment的导包问题和相关注意点

        本文链接:https://www.haomeiwen.com/subject/ilzzottx.html