如何使用字母滚动列表? [英] How can I make a list with alphabetical scrolling?

查看:118
本文介绍了如何使用字母滚动列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用recyclerview制作最简单的列表,并在其中添加按字母顺序滚动(如联系人)?请向我描述一下或给我一个例子.

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:我是Android开发的新手,以前从未使用过RecyclerView.

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

推荐答案

在res/layout中打开activity_main.xml文件,然后复制以下内容.

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>

在res/layout中创建一个新的side_index_item.xml文件,然后复制以下内容.

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>

这是主要的活动类别.

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()在右侧显示字母索引器并进行设置 用于TextView的OnClickListener.

    • 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.

      有关更多信息,我建议您做一段时间Google,您会获得很多教程.这是我可以推荐的一种: http://www.brightec. co.uk/ideas/android-listview-alphabet-scroller

      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

      这篇关于如何使用字母滚动列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆