无涯教程-Android - List fragments函数

138 阅读3分钟

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

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

List 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" />

        </span><span class="tag">&lt;category</span><span class="pln"> </span><span class="atn">android:name</span><span class="pun">=</span><span class="atv">"android.intent.category.LAUNCHER"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
     </span><span class="tag">&lt;/intent-filter&gt;</span><span class="pln">
  </span><span class="tag">&lt;/activity&gt;</span><span class="pln">

</application> </manifest>

运行应用程序

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

Android list fragment

参考链接

www.learnfk.com/android/and…