How can I make a list with alphabetical scrolling?

2019-09-22 10:49发布

问题:

How can I make the simplest list using the recyclerview and add there alphabetical scrolling (as in contacts)? Please describe it to me or give me an example of how to do it.

P.S: I'm new in Android development and I've never worked with the RecyclerView before.

回答1:

Open activity_main.xml file in res/layout and copy the following content.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="5dp"
tools:context=".MainActivity"
android:baselineAligned="false" >

<ListView
    android:id="@+id/list_fruits"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:paddingRight="5dp" >
</ListView>

<LinearLayout
    android:id="@+id/side_index"
    android:layout_width="50dp"
    android:layout_height="fill_parent"
    android:background="#c3c3c3"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
</LinearLayout>

Create a new side_index_item.xml file in res/layout and copy the following content.

    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/side_list_item"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:padding="3dp"
    android:textSize="14sp" />

Open res/values/strings.xml and edit to have the content as shown below. A string array is defined to have list of fruits.

    <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndroidListViewIndex</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string-array name="fruits_array">
        <item>Apples</item>
        <item>Apricots</item>
        <item>Avocado</item>
        <item>Annona</item>
        <item>Banana</item>
        <item>Blueberry</item>
        <item>Blackberry</item>
        <item>Blackapple</item>
        <item>Custard Apple</item>
        <item>Clementine</item>
        <item>Cantalope</item>
        <item>Date</item>
        <item>Elderberry</item>
        <item>Fig</item>
        <item>Grapefruit</item>
        <item>Grape</item>
        <item>Gooseberry</item>
        <item>Guava</item>
        <item>Honeydew melon</item>
        <item>Jackfruit</item>
        <item>Juniper Berry</item>
        <item>Kiwi</item>
        <item>Kumquat</item>
        <item>Lemons</item>
        <item>Limes</item>
        <item>Lychee</item>
        <item>Mango</item>
        <item>Mandarin</item>
        <item>Nectaraine</item>
        <item>Orange</item>
        <item>Olive</item>
        <item>Prunes</item>
        <item>Pears</item>
        <item>Plum</item>
        <item>Pineapple</item>
        <item>Peach</item>
        <item>Papaya</item>
        <item>Pomelo</item>
        <item>Raspberries</item>
        <item>Rambutan</item>
        <item>Strawberries</item>
        <item>Sweety</item>
        <item>Salmonberry</item>
        <item>Tangerines</item>
        <item>Tomato</item>
        <item>Ugli</item>
        <item>Watermelon</item>
        <item>Woodapple</item>
    </string-array>

</resources>

This is the main activity class.

private void getIndexList(String[] fruits) {
        mapIndex = new LinkedHashMap<String, Integer>();
        for (int i = 0; i < fruits.length; i++) {
            String fruit = fruits[i];
            String index = fruit.substring(0, 1);

            if (mapIndex.get(index) == null)
                mapIndex.put(index, i);
        }
    }
  • displayIndex() displays alphabetic indexer at the right and sets OnClickListener for TextView.

  • When a letter from alphabet indexer is selected, it displays corresponding list item.

For more understaing, i will suggest to do google for while, you will get a lot of tutorial. Here is the one I can recommend: http://www.brightec.co.uk/ideas/android-listview-alphabet-scroller