【翻译】Android-ListView实现SectionIndexer

【翻译】Android-ListView实现SectionIndexerThis small tutorial will show you how to create a ListView, enable fast scrolling, and create a alphabetical section list that displays the letter as you quickly scroll the list.

这个简单的教程会教你创建一个ListView,实现快速滑动,并且创建一个按字母排序的分类的列表。当你快速滑动的时候,就会显示一个字母,表示当前滚动的位置。

First lets create a layout file with a ListView in it list this

首先创建一个存在ListView的布局文件

代码如下:


Notice I am putting a background image on the layout and setting the ListView to be transparent with the cacheColorHint. I also set fastScrollEnabled to true. Now I have a store object to store the attributes about a store 请注意我为整个布局设置了一个背景图片,用cacheColorHint属性吧Listview设置为透明。我也把fastScrollEnabled 属性设置为true了。 接下来我用一个store对象存储了一些关于store的属性。

public class Store {

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

finally we need to create the Activity. We will assign a custom ArrayAdapter to the list and the custom ArrayAdapter will implement SectionIndexer
最后,我们需要创建一个Activity。我们需要指派一个实现了SectionIndexer接口的自定义的ArrayAdapter给这个ListView。

 

/**
 * The Store List Activity
 */
public class StoreListActivity extends Activity {
    private DBAdapter db;

    private LinkedList<Store> storeList = new LinkedList<Store>();
    private StoreListAdaptor storeListAdaptor;

    private ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.storelist);

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

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

        // create an adaptor with the store list
        storeListAdaptor = new StoreListAdaptor(this,storeList);

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

    }

    /**
     * The List row creator
     */
    class StoreListAdaptor extends ArrayAdapter<Store> implements SectionIndexer{

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

        public StoreListAdaptor(Context context, LinkedList<Store> 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;
       }
    }
}

原文地址:
http://twistbyte.com/tutorial/android-listview-with-fast-scroll-and-section-index
Liudroid翻译
转载请注明出处
Liudroid博客
http://www.liuzhaocn.com

  • 标签:
分类:Android
点赞: 0

6条评论

  1. Liudroid说道:

    我在这篇文章之后,写了一个Demo实现了通讯录快速滑动的时候提示滚动位置。
    可以提示中文第一个汉字(程序加载迅速),也可以提示汉字拼音首字母(程序加载慢,因为要根据汉字获得拼音首字母)
    有需要的给我留言

    1. Knowto说道:

      @Liudroid @Liudroid: 能否提供一份?先谢过,我邮箱:knowto@yeah.net

      1. Liudroid说道:

        @Knowto 请移驾:http://www.liuzhaocn.com/android/liudroid-sectionindexer%e8%a7%a3%e9%87%8a%e5%8f%8a%e7%a4%ba%e4%be%8b%e4%b8%8b%e8%bd%bd/

  2. longlong说道:

    请给我发一份吧longlong850525@hotmail.com,学习下,谢谢了

    1. tudou说道:

      @longlong 可否给我发一份?感谢 562098405@qq.com

  3. Liudroid说道:

    请到:http://www.liuzhaocn.com/android/liudroid-sectionindexer%e8%a7%a3%e9%87%8a%e5%8f%8a%e7%a4%ba%e4%be%8b%e4%b8%8b%e8%bd%bd/

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  • © 2011 知研片语
  • 京ICP备16042882号