zoukankan      html  css  js  c++  java
  • SectionIndexer示例

    This 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的布局文件

    代码如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2  
     3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     4                 android:layout_width="fill_parent"
     5                 android:layout_height="fill_parent"
     6                 android:background="@drawable/background">
     7  
     8     <ListView
     9             android:id="@+id/thelist"
    10             android:layout_width="wrap_content"
    11             android:layout_height="wrap_content"
    12             android:scrollbars="vertical"
    13             android:fadingEdge="vertical"
    14             android:cacheColorHint="#00000000"
    15             android:fastScrollEnabled="true"
    16             android:padding="2dp">
    17     </ListView>
    18 </LinearLayout>

    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;
     
        /**
         * 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
            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;
           }
        }
    }
  • 相关阅读:
    iOS 沙盒机制 持久存储 读写文件 NSFileManager
    关键字@synchronized
    整理UIImagePickerController、保存图片到相册问题
    关于MJRefresh的下拉加载数据bug
    tableView显示第一个cell有偏移问题
    iOS比较常用的第三方框架
    iOS 10 下的用户隐私访问相册等权限问题!
    diff: /../Podfile.lock: No such file or directory diff: /Manifest.lock: No such file or directory error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods in
    iOS的坑:ERRORITMS-90096: "Your binary is not optimized for iPhone 5
    CocoaPods 1.0之前版本无法pod install和pod update! 更新后CocoaPods 1.1.1 Podfile新的写法.
  • 原文地址:https://www.cnblogs.com/sdrzlyz/p/3707527.html
Copyright © 2011-2022 走看看