第四章 探究碎片、动态加载碎片与限定符

**

一、什么是碎片

**
碎片是一种可以嵌入在活动当中的UI片段,从而让程序更加合理和充分地利用大屏幕的空间,因此在平板上应用得非常广泛。
碎片
**

二、简单的碎片实例

**
1、先新建 一个左侧碎片布局和右侧碎片布局
左侧碎片布局(具体的碎片布局)

<?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>

右侧碎片布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#00ff99"
    android:layout_width="match_parent"
    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 frament"
        />
</LinearLayout>

2、新建一个LeftFragment和RightFragment类,并让他继承Fragment(动态加载进碎片布局)

package com.example.kim.fragmenttest;

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

/**
 * Created by KIM on 2017/5/1.
 */

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;
    }
}
package com.example.kim.fragmenttest;

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

/**
 * Created by KIM on 2017/5/2.
 */

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;
    }
}

3、修改activity_main.xml中的代码(构建出两个碎片框架并导入具体碎片实例)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.kim.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.kim.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

最终效果

三、动态添加碎片

1、再新建 一个碎片文件another_right_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="ffff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

2、新建一个AnotherRightFragment动态加载进another_right_fragment碎片

package com.example.kim.fragmenttest;
;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.zip.Inflater;

/**
 * Created by KIM on 2017/5/3.
 */

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;
    }
}

3、修改activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

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

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

将右侧碎片替换成了一个FrameLayout布局,由于这里仅需要在布局中放入一个碎片,不需要任何定位,因此非常适合使用FrameLayout
4、修改MainActivity中的代码,在代码中向FrameLayout里添加内容,从而实现动态添加碎片

package com.example.kim.fragmenttest;
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.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                replaceFragment(new AnotherRightFragment());
            }
        });
    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager=getSupportFragmentManager();
        //通过调用getSupportFragmentManager()方法来获取FragmentManager
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        //通过fragmentManager调用beginTransaction()方法开启
        fragmentTransaction.replace(R.id.right_layout,fragment);
        //通过replace()方法替换碎片,传入容器id和待添加碎片
        fragmentTransaction.addToBackStack(null);
        //模拟返回栈,传入描述栈的状态的参数,一般传入null就行
        fragmentTransaction.commit();
        //提交事务
    }
}

效果图:
点击前
点击前
点击后
点击后

**

四、碎片和活动 与 碎片和碎片之间的通信

**
①碎片和活动之间的通信
a、活动中调用碎片里的方法

为了方便碎片和活动进行通信,FragmentManager提供了一个类似于findViewById(()的方法,专门从布局文件中获取碎片实例

RightFragment rightFragment=(RightFragment)getFragmentManager().findFragmentById(R.id.right_fragment);

经过上面的代码,就可以在活动中得到相应的碎片实例,然后轻松调用碎片里的方法了

b、碎片调用活动里的方法
每个碎片都可以通过调用getActivity()方法来得到和当前碎片相关的活动实例

MainActivity activity=(MainActivity)getActivity();

②碎片和碎片之间的通信

首先在一个碎片中得到与他相关的活动,然后再通过这个活动去获得另一个碎片的实例即可

**

五、限定符的使用

**
在平板电脑上很多应用都采用的是双页模式(程序在左侧的面板上显示一个包含子项的列表,二在右侧显示内容),因为平板电脑屏幕足够大,可以容下两页内容。但手机屏幕一次只能显示一页的内容,因此两个页面需要分开显示。
那么怎么样才能判断程序应该使用双页模式还是单页模式呢?这就要借助限定符(Qualifiers)来实现了

1、限定符的简单范例
1、继续使用上文的代码,修改FragmentTest项目中的Activity_main文件,将多余代码删掉,只留下一个左侧碎片,并让他充满整个父布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.kim.fragmenttest.LeftFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

2、在res目录下新建layout-large文件夹,在这文件夹下新建布局,名也为activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.kim.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.kim.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"/>
</LinearLayout>

可以看到 layout/activity_main布局只包含了一个碎片,为单页模式,而layout-large/activity_main却包含了两个碎片,为双页模式,其中large就是一个限定符,屏幕被认为是large的设备就会自动加载layout-large文件夹下的布局,而小屏幕设备则还是会加载layout文件夹下的布局。

3、将replaceFragment()方法内的代码注释掉,并分别在平板和手机模拟器上运行
在平板模拟器上运行效果如下
这里写图片描述
在手机模拟器上运行效果如下
这里写图片描述

2、常见的限定符
根据屏幕大小分类
这里写图片描述
根据屏幕分辨率分类
这里写图片描述
根据屏幕方向分类
这里写图片描述

3、最小宽度限定符
上面我们用限定符成功解决了单双页的判断问题,不过large到底是指多大我们并不知道,有时我们需要更灵活的为不同设备加载布局,不管他们是否被判断为large,这时就需要使用最小宽度限定符(Smallest_width Qualifier),
最小宽度限定符允许我们对屏幕宽度指定一个最小值(以dp为单位),然后以这个最小值为临界点,屏幕宽度大于这个值的设备就加载一个布局,屏幕宽度小于这个值的设备加载另一个布局。

范例:在res目录下新建layout-sw600dp文件夹,然后在这个文件夹下新建activity_main.xml布局。

这就意味着程序运行在屏幕宽度大于600dp的设备上时,会加载layout-sw600dp/activity_main布局,当程序运行在屏幕宽度小于600dp的设备上时,则仍然加载默认的layout/activity_main布局

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值