zoukankan      html  css  js  c++  java
  • Fragment碎片的使用

    第一种:静态使用方法

    left_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="button" />
    </LinearLayout>  

    right_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00ff00"
        android:orientation="vertical">
    
    
        <TextView
            android:text="This is right fragment"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    LeftFragment.java和 RightFragment.java 一样

    package com.example.kaige.fragmenttest1;
    
    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;
    
    public class LeftFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.left_fragment,container,false);
            return view;
        }
    }
    activity_main.xml
    <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context="com.example.kaige.fragmenttest1.MainActivity">
    
        <!--静态碎片使用-->
        <fragment
            android:id="@+id/left_fragment"
            android:name="com.example.kaige.fragmenttest1.LeftFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
    
        <fragment
            android:id="@+id/right_fragment"
            android:name="com.example.kaige.fragmenttest1.RightFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2" />
    
        
    </LinearLayout>
    

    MainActivity.java  不用改动

    第二种:动态加载Fragment

    修改activity_main.xml

    <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context="com.example.kaige.fragmenttest1.MainActivity">
    
        <!--静态碎片使用-->
        <fragment
            android:id="@+id/left_fragment"
            android:name="com.example.kaige.fragmenttest1.LeftFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <!--动态添加fragment-->
        <FrameLayout
            android:id="@+id/right_fragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

    添加

    another_right_fragment.xml 和 AnotherRightFragment

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff00"
        android:orientation="vertical">
    
        <TextView
            android:textSize="20sp"
            android:text="This is another right frament"
            android:layout_width="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    package com.example.kaige.fragmenttest1;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    /**
     * 动态添加fragment
     */
    public class AnotherRightFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.another_right_fragment, container, false);
            return view;
        }
    }

    最后修改MainActivity.java 

    package com.example.kaige.fragmenttest1;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button = (Button) findViewById(R.id.button);
            button.setOnClickListener(this);
            replaceFragment(new RightFragment());
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    replaceFragment(new AnotherRightFragment());
                    break;
            }
        }
        private void replaceFragment(Fragment fragment) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.right_fragment, fragment);
            fragmentTransaction.commit();
        }
    }
    

     运行看看效果吧..

  • 相关阅读:
    【C#学习笔记】【7】我想从键盘输入变量的值,这样程序就更灵活了(输入输出函数以及类型转换)
    【C#学习笔记】【6】找出0-100以内的所有的能被3整除或者被5整除余2的数(二)(流程控制)
    【C#学习笔记】【5】找出0-100以内的所有的能被3整除或者被5整除余2的数(一)(流程控制)
    【C#学习笔记】【4】除了加减乘除,还有别的运算么(算数运算符与逻辑运算符)
    【C#学习笔记】【3】从计算1+1=2开始(变量与类型)
    【C#学习笔记】【2】我的第一个程序My first program
    【C#学习笔记】【1】VisualStudio2010Express安装详解
    【C#学习笔记】【0】C#学习笔记系列博文
    红豆带你从零学C#系列之——初识C#
    红豆带你从零学C#系列之:初识继承与多态
  • 原文地址:https://www.cnblogs.com/412013cl/p/9066229.html
Copyright © 2011-2022 走看看