如何制作按字母顺序滚动的列表?

时间:2017-05-14 16:53:25

标签: java android android-recyclerview fast-scroller

如何使用recyclerview创建最简单的列表并添加字母滚动(如联系人中)?请向我描述或给我一个如何做的例子。

P.S:我是Android开发的新手,我之前从未使用过RecyclerView。

1 个答案:

答案 0 :(得分:0)

在res / layout中打开activity_main.xml文件并复制以下内容。

var timer2 = "1:30";
$('.countdown').html(timer2);

var interval = setInterval(function() {

var timer = timer2.split(':');
//by parsing integer, I avoid all extra string processing
var minutes = parseInt(timer[0], 10);
var seconds = parseInt(timer[1], 10);
--seconds;
minutes = (seconds < 0) ? --minutes : minutes;
if (minutes < 0){
    clearInterval(interval);
    $('input[name=ansSubmit]').trigger("click");
}else{
    seconds = (seconds < 0) ? 59 : seconds;
    seconds = (seconds < 10) ? '0' + seconds : seconds;
    //minutes = (minutes < 10) ?  minutes : minutes;
    $('.countdown').html(minutes + ':' + seconds);
    timer2 = minutes + ':' + seconds;
}
}, 1000);

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

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

这是主要的活动类。

    <?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>
  • displayIndex()在右侧显示字母索引器并设置 TextView的OnClickListener。

  • 当选择来自字母索引器的字母时,会显示 相应的清单项目。

为了更加理解,我会建议谷歌一段时间,你会得到很多教程。这是我可以推荐的那个:http://www.brightec.co.uk/ideas/android-listview-alphabet-scroller