美文网首页
Android Fragment的使用

Android Fragment的使用

作者: 2019Say10 | 来源:发表于2019-05-18 17:30 被阅读0次

    静态添加fragment

    1.创建xml布局

    activity_static_fragment

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f0f"
        >
     <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="staticFragment"
            android:textColor="#000"
            android:gravity="center"
            />
    </android.support.constraint.ConstraintLayout>
    

    2.创建fragment

    StaticFragment

    public class StaticFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.activity_static_fragment,container,false);
            return view;
        }
    }
    

    创建一个类继承Fragment,重写onCreateView()方法。fragment的OnCreateView类似于activity的onCreate();

    3.布局中调用fragment

        <fragment
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:id="@+id/frag_test"
            android:name="com.example.test.fragment.StaticFragment"
            >
        </fragment>
    
    

    通过name实例化要加载的碎片

    4.效果

    static.png

    动态添加fragment

    1.创建xml布局

    activity_left_fragemnt

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#38d6b5"
    
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="LeftFragment"
            android:textColor="#000"
            />
    </android.support.constraint.ConstraintLayout>
    

    activity_right_fragment

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f74747"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="RightFragment"
            android:textColor="#000"
            android:gravity="center"
            />
    </android.support.constraint.ConstraintLayout>
    

    2.创建fragment

    LeftFragment

    public class LeftFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.activity_left_fragemnt,container,false);
            initView();
            return  view;
        }
    }
    

    RightFragment

    public class RightFragment  extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.activity_right_fragment,container,false);
            return  view;
        }
    ]
    

    activity_main

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHeight_percent="0.9"
            android:id="@+id/cl_content"
            >
        </android.support.constraint.ConstraintLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintHeight_percent="0.1"
            app:layout_constraintTop_toBottomOf="@id/cl_content"
            >
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="left_fragment"
                android:background="#f4d03e"
                android:id="@+id/bt_left"
                />
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="right_fragment"
                android:background="#f4d03e"
                android:id="@+id/bt_right"
                />
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
    

    3.往Activity添加fragment

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            Button bt_left=findViewById(R.id.bt_left);
            Button bt_right=findViewById(R.id.bt_right);
            bt_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeFragment(new LeftFragment());
                }
            });
            bt_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeFragment(new RightFragment());
                }
            });
        }
        private void changeFragment(Fragment fragment){
            //实例化碎片管理器对象
            FragmentManager fm=getSupportFragmentManager();
            FragmentTransaction ft=fm.beginTransaction();
            //选择fragment替换的部分
            ft.replace(R.id.cl_content,fragment);
            ft.commit();
        }
    
    }
    

    4.效果图 效果.gif

    其他补充

    fragment获取当前对象:getActivity();
    fragment可以往fragment里添加fragment:方法与往activity添加fragment方法一致,不同的是在fragment动态添加子fragment,碎片管理的获取方式为getChildFragmentManager();

       FragmentManager fm=getChildFragmentManager();
    

    相关文章

      网友评论

          本文标题:Android Fragment的使用

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