2017-05-14 49 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