如何在列表视图中实现字母索引器?

时间:2015-03-10 12:11:45

标签: android

我想在包含字母表的Android音乐播放器中实现滚动视图。它应该显示从单击的相同字母开始的歌曲

3 个答案:

答案 0 :(得分:1)

您可以尝试使用QuickScroll库。

它有点新。

您应该阅读SectionIndexer in Android Developers

此外,您可以关注this tutorial

答案 1 :(得分:0)

尝试使用IndexableListView希望它会有所帮助

答案 2 :(得分:0)

尝试这样的事情 - 将自定义ArrayAdapter分配给列表,自定义ArrayAdapter将实现SectionIndexer。

首先,使用ListView创建一个xml布局:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/background">

    <ListView
            android:id="@+id/thelist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:fadingEdge="vertical"
            android:cacheColorHint="#00000000"
            android:fastScrollEnabled="true"
            android:padding="2dp">
    </ListView>
</LinearLayout>

第二次,创建一个对象来保存您的项目(包含您喜欢的所有属性):

public class Holder {

    public int id;
    public String name;
    public String direction;
    public String address;
    public String phone;
}

终于,使用实现SectionIndexer的自定义适配器创建您的活动

/**
 * The Holder List Activity
 */
public class HolderListActivity extends Activity {
    private DBAdapter db;

    private LinkedList<Holder> holderList = new LinkedList<Holder>();
    private HolderListAdaptor holderListAdaptor;

    private ListView list;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.storelist);

        list = (ListView) findViewById(R.id.thelist);

        // populate the store list
        holderList = db.getAllStoresOrderByName(holderList);

        // create an adaptor with the holder list
        holderListAdaptor = new HolderListAdaptor(this,holderList);

        // assign the listview an adaptor
        list.setAdapter(holderListAdaptor);

    }

    /**
     * The List row creator
     */
    class HolderListAdaptor extends ArrayAdapter<Holder> implements SectionIndexer{

        HashMap<String, Integer> alphaIndexer;
        String[] sections;

        public HolderListAdaptor(Context context, LinkedList<Holder> items) {
            super(context, R.layout.storerow, items);

            alphaIndexer = new HashMap<String, Integer>();
            int size = items.size();

            for (int x = 0; x < size; x++) {
                Store s = items.get(x);

        // get the first letter of the store
                String ch =  s.name.substring(0, 1);
        // convert to uppercase otherwise lowercase a -z will be sorted after upper A-Z
                ch = ch.toUpperCase();

        // HashMap will prevent duplicates
                alphaIndexer.put(ch, x);
            }

            Set<String> sectionLetters = alphaIndexer.keySet();

        // create a list from the set to sort
            ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); 

            Collections.sort(sectionList);

            sections = new String[sectionList.size()];

            sectionList.toArray(sections);
        }

       public View getView(int position, View convertView, ViewGroup parent) {
            // expand your row xml if you are using custom xml per row
       } 

       public int getPositionForSection(int section) {
           return alphaIndexer.get(sections[section]);
       }

       public int getSectionForPosition(int position) {
           return 1;
       }

       public Object[] getSections() {
            return sections;
       }
    }
}

你很高兴去!