Android 基础知识

Android 主页 Android 概述 Android 下载安装和开发环境配置 Android Studio IDE Android 架构 Android 应用程序组件 Android Hello World 示例 Android 资源 Android 活动 Android 服务 Android 广播接收器 Android 内容提供者 Android 片段 Android Intents/Filters

Android 用户界面

Android UI 布局 Android UI 控件 Android 事件处理 Android 样式和主题 Android 自定义组件

Android 高级概念

Android 拖放 Android 通知 Android 基于位置的服务 Android 发送电子邮件 Android 发送短信 Android 拨打电话 Android 发布应用程序

Android 实用示例

Android 警报对话框 Android 动画 Android 音频捕捉 Android 音频管理器 Android 自动完成 Android 最佳实践 Android 蓝牙 Android 相机 Android 剪贴板 Android 自定义字体 Android 数据备份 Android 开发者工具 Android 模拟器 Android Facebook 集成 Android 手势 Android 谷歌地图 Android 图像效果 Android 图像切换 Android 内部存储 Android JetPlayer Android JSON 解析器 Android Linkedin 集成 Android 旋转加载器 Android 本地化 Android 登录应用 Android 媒体播放器 Android 多点触控 Android 导航 Android 网络连接 Android NFC 指南 Android PHP/MySQL Android 进度圈 Android 进度条 Android 推送通知 Android 渲染脚本 Android RSS 阅读器 Android 屏幕投射 Android SDK 管理器 Android 传感器 Android 会话管理 Android 共享首选项 Android SIP 协议 Android 拼写检查器 Android SQLite 数据库 Android 支持库 Android 测试 Android 文字转语音 Android TextureView Android Twitter 集成 Android UI 设计 Android UI 模式 Android UI 测试 Android WebView 布局 Android Wi-Fi Android Widgets Android XML 解析器

Android 其他

Android 有用的资源 Android 测验


Android - 列表片段

框架的 ListFragment 的静态库支持版本。 用于编写在 Android 3.0 之前的平台上运行的应用程序。 在 Android 3.0 或更高版本上运行时,仍然使用此实现。

列表片段的基本实现是用于在片段中创建项目列表

列表片段

列表片段


示例

本示例将向您说明如何基于 arrayAdapter 创建自己的列表片段。 因此,让我们按照以下步骤类似于我们在创建 Hello World 示例时所遵循的步骤 −

步骤 描述
1 您将使用 Android Studio 创建一个 Android 应用程序,并将其命名为 SimpleListFragment,位于包 com.example.tutorialspoint7.myapplication 下,Activity 为空白。
2 修改已放置在 res/values/string.xml 的字符串文件以添加新的字符串常量
3 在目录 res/layout 下创建一个名为 list_fragment.xml 的布局来定义您的列表片段。 并将片段标签(<fragment>)添加到您的 activity_main.xml
4 创建一个 myListFragment.java,它位于 java/myListFragment.java 并包含 onCreateView()、onActivityCreated() 和 OnItemClickListener()
5 运行应用程序以启动 Android 模拟器并验证应用程序中所做更改的结果。

在开始编码之前,我将在 res/values 目录下的 string.xml 文件中初始化字符串常量

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">ListFragmentDemo</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="imgdesc">imgdesc</string>
   
   <string-array name="Planets">
      <item>Sun</item>
      <item>Mercury</item>
      <item>Venus</item>
      <item>Earth</item>
      <item>Mars</item>
      <item>Jupiter</item>
      <item>Saturn</item>
      <item>Uranus</item>
      <item>Neptune</item>
   </string-array>

</resources>

以下是 res/layout/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"
   android:orientation="vertical" >
   
   <fragment
      android:id="@+id/fragment1"
      android:name="com.example.tutorialspoint7.myapplication.MyListFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

</LinearLayout>

以下是 res/layout/list_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" >

   <ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </ListView>

   <TextView
      android:id="@android:id/empty"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </TextView>
</LinearLayout>

以下将是 src/main/java/myListFragment.java 文件的内容。在编写代码之前,需要按照如下所示的几个步骤进行操作

  • 创建一个 MyListFragment 类并将其扩展到 ListFragment。

  • 在 onCreateView() 方法中,使用上面定义的 list_fragment xml 布局扩展视图。

  • 在 onActivityCreated() 方法中,从资源创建一个arrayadapter,即使用可以在string.xml 中找到的字符串数组R.array.planet,并将此适配器设置为listview,并设置onItem 单击监听器。

  • 在 OnItemClickListener() 方法中,显示一条 Toast 消息,其中包含正在单击的项目名称。

package com.example.tutorialspoint7.myapplication;

import android.annotation.SuppressLint;
import android.app.ListFragment;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MyListFragment extends ListFragment implements OnItemClickListener {
   @Override
   public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.list_fragment, container, false);
      return view;
   }

   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), 
         R.array.Planets, android.R.layout.simple_list_item_1);
      setListAdapter(adapter);
      getListView().setOnItemClickListener(this);
   }

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
      Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
   }
}

以下代码是 MainActivity.java 的内容

package com.example.tutorialspoint7.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

以下代码将是 manifest.xml 的内容,它已放在 res/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.tutorialspoint7.myapplication">

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

运行应用程序

让我们尝试运行我们刚刚创建的 SimpleListFragment 应用程序。假设您在进行环境设置时已经创建了 AVD。 要从 Android Studio 运行应用程序,请打开项目的活动文件之一,然后单击工具栏中的 Run Eclipse 运行图标 图标。Android 会在您的 AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下 Emulator 窗口 −

Android 列表片段

❮ Android 片段



关于我们 | 隐私条款 | 学习路线
返回顶部