Latest web development tutorials

Android fragmentation list

Basically a list of fragments is used to create a list of items in the debris

image


Examples

This example explains how to create a list based on ArrayAdapter debris. Let us follow the following steps to get started:

step description
1 Use Android Studio to create an Android application, named List Fragment, the package name cn.uprogrammer.listfragment
2 Modify the string file, add a new string constants in res / values ​​/ string.xml in
3 Create a file named list_fragment.xml layout in res / layout to define a list of pieces, and add in the activity_main.xml label
4 Create MyListFragment.java file that contains onCreateView (), onActivityCreated () and OnItemClickListener ().
5 Launch Android emulator to run the application and verify the results of changes made to the application.

Before you start coding, initialization string constants in string.xml res / values ​​directory under.

<?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 following is the content of res / layout / activity_main.xml file, which contains a linear layout and debris label.

<?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 following is the content of res / layout / list_fragment.xml file, which contains a linear layout, List View and 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 following is the content of src / cn.uprogrammer.listfragment / MyListFragment.java file. Before you start coding, you need to follow a few steps as follows:

  • Create MyListFragment class that inherits from ListFragment.
  • In onCreateView () method, using the above-defined list_fragment xml layout to fill the view.
  • In onActivityCreated () method, using an array of strings R.array.planet resources string.xml defined to create a ArrayAdapter, and the adapter is set to list view, and set the list of items Click Listener
  • In OnItemClickListener () method, by way of toast message to display the position of the item is clicked
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 code is the content 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 file:

<?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 just run the modified List Fragment application. I assume you've created AVD during the installation environment. Open your project in the active file, click on the toolbar image Icon to run the application In Android Studio. Android Studio install the application on the AVD and starts it. If all goes well, it will be displayed on the emulator window as follows:

image