2017-05-14 40 views
-8

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

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

+1

您可以在Google上找到大量资源开始使用。 http://www.androidhive.info/2016/01/android-working-with-recycler-view/ –

+0

试图寻找现有的教程? – GhostCat

+0

@GhostCat是的,我在找。但是我没有在任何地方找到正确的教程。 –

回答

0

在res/layout中打开activity_main.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> 

创建RES /布局的新side_index_item.xml文件,并复制下面的内容。

<?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()显示字母索引器在正确的,并设置 OnClickListener为TextView的。

  • 当选择字母索引器中的字母时,它会显示 对应的列表项。

为了更有把握,我会建议做谷歌的同时,你会得到很多教程。以下是我可以推荐的一个:http://www.brightec.co.uk/ideas/android-listview-alphabet-scroller

+0

我有办法使用android studio做到这一点,但实际上却在寻找一种使用离子科尔多瓦的方式。 – shameemreza

相关问题