无涯教程-Android - List fragments函数

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

List fragment 的基本实现是用于创建fragment中的项目列表

List in Fragments

示例

本示例将向您说明如何基于arrayAdapter创建自己的列表片段,在开始编码之前,我将在 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 文件的内容。它包含线性布局和fragment标签。

复制代码
<?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.learnfk7.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 array R.array.planet,您可以在string.xml中找到并将此适配器设置为listview并还要设置onItem点击监听器。

  • 在 OnItemClickListener()方法内部,显示一条带有被单击的商品名称的消息。

复制代码
package com.example.learnfk7.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.learnfk7.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.learnfk7.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运行该应用程序,请打开您项目的Activity文件之一,然后单击运行工具栏。 Android将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在"Emulator"窗口下方-

Android 中的 List fragments函数 - 无涯教程网无涯教程网提供框架的ListFragment的静态库支持版本,用于编写在Android 3.0之前的平台上运行的应用...https://www.learnfk.com/android/android-list-fragment.html

相关推荐
编程圈子26 分钟前
操作系统学习14 IDT + PIC + 键盘中断
android·学习·计算机外设
齊家治國平天下7 小时前
Android 14 Vehicle HAL (VHAL) 设计与实现深度解析
android·aaos·carservice·车载开发·android 14·vhal·vehicle hal
AFinalStone13 小时前
Android 7系统网络(八)应用API层—ConnectivityManager使用与实战调试
android·网络
plainGeekDev14 小时前
kapt 替换为 KSP
android·java·kotlin
蜡台14 小时前
通过Gradle脚本声明更改Java变量
android·java·开发语言·python·kotlin·gradle·groovy
阿pin17 小时前
Android随笔-SharedPreferences
android·sp
阿pin18 小时前
Android随笔-DataStore
android·datastore
茶栀(*´I`*)18 小时前
Android 测试入门指南:ADB 基础配置与常用设备管理命令解析
android·adb
ihuyigui20 小时前
海外酒店预订短信接口
android·java·运维·服务器·前端·后端·架构
gxgldyh21 小时前
Android Framework源码解析(七):BootAnimation 启动流程解析——开机动画是如何显示出来的?
android