Android 第十七课 碎片的简单用法及动态添加碎片

Fragment(碎片)是一种可以嵌入在活动当中的UI片段,它可以让程序更加合理和充分的利用大屏幕的空间。碎片和活动太像了,同样都包含布局,都有自己的声明周期,可以将碎片理解为一种迷你型的活动。

新建FragmentTest项目。假设项目已经建立完成。

新建一个左侧布局left_fragment.xml,代码如下:

<?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">
    
    <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:orientation="vertical"
    android:layout_width="match_parent"
    android:background="#00ff00"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is right fragment"/>
</LinearLayout>

我们只是将这个布局的背景设置成了绿色,并放置了一个TextView用于显示一段文本。

接着新建一个LeftFragment类,这个类继承自Fragment,这里有两个不同包下的Fragment供你选择,我们要选择support-v4库中的Fragment,因为它可以让碎片在所有Android 版本中保持功能的一致性。另外,我们不需要在build.gradle文件中添加support-v4库的依赖,因为build.gradle文件中已经添加了appcompat-v7库的依赖,而这个库,会将support-v4库也一并引入进来。

现在编写LeftFragment中的代码,如下所示:

package com.example.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ZHJ on 2018/3/6.
 */

public class LeftFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return  view ;
    }
}

这里仅仅重写了Fragment的onCreata()方法,然后在这个方法中通过LayoutInflater的inflate()方法将刚才定义的left_fragment布局动态加载进来,整个方法就简单明了。

我们再新建一个RightFragment,代码如下:

package com.example.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ZHJ on 2018/3/6.
 */

public class RightFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.right_fragment,container,false);
        return  view ;
    }

}

基本山代码都是相同的,接下里修改acitivity_main.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">

    
    <fragment
        android:id="@+id/left_fragment"
        android:name ="com.example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <fragment
        android:id="@+id/right_fragment"
        android:name ="com.example.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    
    
</LinearLayout>

我们使用了<fragment>标签在布局中添加碎片,其中指定的大多数属性都是比较熟悉的,只不过通过android:name属性来显式指明要添加地碎片类名,注意一定要将类的包名也加上。

我们可以运行一下程序:


两个碎片平分了整个活动地布局。



2、动态添加碎片

碎片地真正地强大之处在于,它可以在程序运行时动态地添加到活动当中,根据具体情况来添加碎片,你就可以将程序界面定制的更加多样化。

我们接着上一节的内容,新建another_right_fragment.xml,代码如下:

<?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:background="#ffff00"
    android:layout_height="match_parent">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is another right fragment"
        />

</LinearLayout>

这个布局文件和right_fragment.xml中的代码基本相同,只是将背景色变成了黄色。

新建AnotherRightFragment作为另一个右侧碎片,代码如下:


package com.example.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ZHJ on 2018/3/6.
 */

public class AnotherRightFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.another_right_fragment,container,false);
        return  view ;
    }
}
代码同样简单。在onCreateView()方法中加载了刚刚创建的another_right_fragment布局。这样我们就准备好了另一个碎片,接下来,看一下如何动态将它添加到活动当中,修改activity_main.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">


    <fragment
        android:id="@+id/left_fragment"
        android:name ="com.example.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    
    
    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

</LinearLayout>

现在将右侧碎片替换成一个Fragment中,Fragment是Android中最简单的一种布局,所有控件默认都会摆放在布局的左上角。

由于这里仅仅需要在布局里放入一个碎片,不需要任何定位,非常适合使用FragmentLayout。

我们在代码中向Fragment中添加内容,从而实现动态添加碎片的功能。修改MainActivity中的代码,如下:

package com.example.fragmenttest;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 view) {
        switch (view.getId()){
            case R.id.button:
                replaceFragment(new AnotherRightFragment());
                break;
            default:
                break;
        }
    }

    private void replaceFragment(android.support.v4.app.Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction tranction = fragmentManager.beginTransaction();
        tranction.replace(R.id.right_layout,fragment);
        tranction.commit();
    }

}

首先我们给左侧碎片中的按钮注册了一个点击事件,然后调用replaceFragment()方法动态的添加Fragment这个碎片。当点击按钮时,又会调用replaceFragment()方法将右侧碎片替换成AnotherRightFragment.结合replaceFramment()方法中的代码可以看出,动态添加碎片主要分为5步。

1、创建待添加的碎片实例

2、创建FragmentManager,在活动中可以直接通过调用getSupportFragmentManager()方法得到。

3、开启一个事务,通过调用beginTransaction方法开启。

4、向容器内添加或替换碎片,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。

5、提交事务,调用commit()方法来完成。

运行一下程序:点击按钮可以看到效果:







  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值