Android List Fragment
Android List Fragment
The basic implementation of a List Fragment is used to create a project list in a fragment

Instance
This instance explains how to create a List Fragment based on ArrayAdapter. Let's start with the following steps:
| Steps | Description |
|---|---|
| 1 | Create an Android application named List Fragment using Android Studio, with the package name cn.uprogrammer.listfragment |
| 2 | Modify the string file to add a new string constant in res/values/string.xml |
| 3 | Create a layout file named list_fragment.xml in res/layout to define the list fragment, and add it to activity_main.xml |
| 4 | Create MyListFragment.java file, which contains onCreateView(), onActivityCreated(), and OnItemClickListener() |
| 5 | Launch the Android emulator to run the application and verify the results of the changes made to the application. |
Initialize string constants in string.xml under the res/values directory before starting to code.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">listfragment</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_main">List Fragment Demo</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>
The content of res/layout/activity_main.xml is as follows, which includes a LinearLayout and a Fragment tag.
<?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="cn.uprogrammer.listfragment.MyListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
The content of res/layout/list_fragment.xml is as follows, which includes a LinearLayout, a ListView, and a TextView.
<?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>
The content of src/cn.uprogrammer.listfragment/MyListFragment.java is as follows. Before starting to code, you need to follow the following steps:
- Create the MyListFragment class, inheriting from ListFragment.
- In the onCreateView() method, inflate the view using the list_fragment xml layout defined above.
- In the onActivityCreated() method, create an ArrayAdapter using the string array resource R.array.planet defined in string.xml, set the adapter to the ListView, and set a click listener for the list items.
- In the OnItemClickListener() method, display the position of the clicked item in a Toast message.
package cn.uprogrammer.listfragment;
import android.app.ListFragment;
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();
}
}
The following is the content of MainActivity.java:
package cn.uprogrammer.listfragment;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The following is the content of AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.uprogrammer.listfragment">
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Let's run the modified List Fragment application. I assume you have created an AVD during the installation environment setup. Open the activity file in your project and click the icon in the toolbar to run the application in Android Studio. Android Studio will install and launch the application on the AVD. If everything goes well, it will be displayed as follows in the emulator window:
image
