Android列表片段

下面创建第二个片段WorkoutFragment,它包含不同训练项目构成的一个列表,用户可以从这个列表中选择训练项目。

列表视图是只包含一个列表的片段

列表片段是一种专门处理列表的片段,它会自动绑定到一个列表视图,所以不需要另外创建列表视图。使用列表片段显示数据有几个主要的有点:

1、不需要创建你自己的布局

列表片段会自动定义自己的布局,所以你不用创建或维护XML布局。列表片段生成的布局包括一个列表视图,可以在活动代码中使用列表片段的getListView()方法访问这个列表视图。要得到列表视图才能指定列表视图中应当显示什么数据。

2、不需要实现你自己的事件监听器。

ListFragment类自动实现了一个监听器,会监听什么时候单击了列表视图中的列表项。不用创建你自己的时间监听器并绑定到列表视图,而只需要实现列表片段的onListItemClick()方法。这样在用户单击列表视图中的列表项时可以更容易地让片段做出响应。

如何创建列表视图

在com.hfad.workout包中创建一个名为WorkoutListFragment.java的新片段Fragment(Blank)。

注意这里没有选择Fragment(List),因为这会生成更复杂的代码,其中大多数并不需要用到。Fragment(Blank)生成的代码更简单。将WorkoutListFragment.java替换为下面的代码:

java 复制代码
package com.hfad.workout;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class WorkoutListFragment extends ListFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

适配器

适配器可以将数据与列表视图连接起来,相当于视图与数据源之间的一个桥梁。数组适配器是专门处理数组的一种适配器类型。这里使用一个数组适配器将一个数组绑定到列表视图。

初始化数组适配器时,首先要指定绑定到列表的数组中包含什么类型的数组,然后要传入3个参数:一个Context(上下文,通常是当前活动)、一个布局资源(指定如何显示数组中的各项),以及数组本身。

Activity类是Context类的一个子类,但是Fragment类不是Context子类,不能访问全局信息,也不能用它向数组适配器传递当前上下文。一种办法是使用另一个对象getContext()方法来得到当前上下文的一个引用。如果你在片段的onCreateView方法中创建这个适配器,就可以使用onCreateView()中的LayoutInflator参数的getContext方法得到上下文。

一旦创建了适配器,要使用片段的setListAdapter方法把它绑定到ListView。

更新WorkoutListFragment如下:

java 复制代码
package com.hfad.workout;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class WorkoutListFragment extends ListFragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        String[] names = new String[Workout.workouts.length];
        for (int i = 0; i < names.length; i++){
            //创建一个包含训练xian
            names[i] = Workout.workouts[i].getName();
        }
        //创建一个数组适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<>(inflater.getContext(), android.R.layout.simple_list_item_1, names);
        //将数组适配器绑定到列表视图
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

更新布局

现在需要将布局中显示WorkoutListFragment,更新activity_main.xml。

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.hfad.workout.WorkoutListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

运行应用

相关推荐
林北北的霸霸2 小时前
django初识与安装
android·mysql·adb
Java 码农3 小时前
MySQL EXPLAIN 详解与优化指南
android·mysql·adb
stevenzqzq7 小时前
Android Hilt 入门教程_传统写法和Hilt写法的比较
android
wuwu_q8 小时前
用通俗易懂方式,详细讲讲 Kotlin Flow 中的 map 操作符
android·开发语言·kotlin
_李小白8 小时前
【Android FrameWork】第五天:init加载RC文件
android
2501_916007478 小时前
手机使用过的痕迹能查到吗?完整查询指南与步骤
android·ios·智能手机·小程序·uni-app·iphone·webview
黄毛火烧雪下9 小时前
React Native (RN)项目在web、Android和IOS上运行
android·前端·react native
下位子10 小时前
『OpenGL学习滤镜相机』- Day7: FBO(帧缓冲对象)
android·opengl
從南走到北10 小时前
JAVA国际版同城外卖跑腿团购到店跑腿多合一APP系统源码支持Android+IOS+H5
android·java·ios·微信小程序·小程序
空白格9710 小时前
组件化攻略
android