Android Learning Series (15)-cursor listview (index listview) in App list)

Source: Internet
Author: User

The cursor listview provides index tags so that you can quickly locate list items.
It can also be called index listview. Some people call it tweaked listview, which may be more vivid.
I understand everything in the figure:

1. cursor (fast scroll thumb)
It is the drag block on the right, which is very simple:

 
<Listview Android: Id = "@ + ID/tweaked_list" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: fastscrollenabled = "true"/>

It can also be written in the Java Background:

 
Tweakedlistview. setfastscrollenabled (true );

When the data volume is large, the so-called "cursor" on the right will appear in the sliding list.
Simple, which is why I like to write controls myself in private, but I like to use general controls at work.
Let's take a look.Source codeIn fact, it is to enable the fastscroller object:

// Enable the fastscroller object public void setfastscrollenabled (Boolean enabled) {mfastscrollenabled = enabled; If (Enabled) {If (mfastscroller = NULL) {mfastscroller = new fastscroller (getcontext (), this) ;}} else {If (mfastscroller! = NULL) {mfastscroller. Stop (); mfastscroller = NULL ;}}}

2. letter Index
In Android Learning Series (10) -- drag and drop the App list listview (top) Windowmanager adds some custom images to listview. I think this method is feasible.
However, the android system provides us with a simpler method: Use alphabetindexer.
Alphabetindexer implements the sectionindexer interface, It is an auxiliary class of the adapter. It helps to display index letters when sliding fast.
If you use a letter index, you must ensure that the data list is sorted alphabetically so that alphabetindexerh can be quickly located using the binary search method.

/*** Cursor indicates the number of column * alphabet letters in the Data cursor * sortedcolumnindex dataset. The most used column is "abcdefghijklmnopqrstuvwxyz" **/Public alphabetindexer (cursor, int sortedcolumnindex, charsequence alphabet ){}

Three methods are used:

 
// These three methods correspond to and locate the index data and list data. Public int getpositionforsection (INT section) {} public int getsectionforposition (INT position) {} public object [] getsections (){}

3. Implementation of cursor
The cursor interface has two options:
(1) directly use the cursor returned by the database query
(2) define a new class to implement the cursor Interface
The first method is simple. query the database and return the cursor.
Here we use the second method to disguise a cursor and implement three methods:
(1). getcount ()
(2). movetoposition ()
(3). getstring ()

/*** Disguise a cursor for alphabetindexer as the data index source */private class indexcursor implements cursor {private listadapter adapter; private int position; private Map <string, string> map; public indexcursor (listadapter adapter) {This. adapter = adapter;} @ override public int getcount () {return this. adapter. getcount ();}/*** obtain the index letter. This method is very important. Handle it according to the actual situation. */@ suppresswarnings ("unchecked ") @ override Public String getstring (INT columnindex) {map = (hashmap <string, string>) adapter. getitem (position); Return map. get (key ). substring (0, 1) ;}@ override public Boolean movetoposition (INT position) {If (position <-1 | position> getcount () {return false;} This. position = position; // if the location is not satisfactory, the following rows are displayed. Code Is to fix the problem of locating the index value as the top item value // If (Position + 2> getcount () {// This. position = position; //} else {// This. position = Position + 2; //} return true;} @ override public void close () {}@ override public void copystringtobuffer (INT arg0, chararraybuffer arg1) {}@ override public void deactivate () {}@ override public byte [] getblob (INT arg0) {return NULL ;}@ override public int getcolumncount () {return 0 ;} @ override public int getcolumnindex (string columnname) {return 0 ;}@ override public int getcolumnindexorthrow (string columnname) throws rows {return 0 ;}@ override Public String getcolumnname (INT columnindex) {return NULL ;}@ override Public String [] getcolumnnames () {return NULL ;}@ override public double getdouble (INT columnindex) {return 0 ;}@ override public bundle getextras () {return NULL ;}@ override public float getfloat (INT columnindex) {return 0 ;}@ override public int getint (INT columnindex) {return 0 ;} @ override public long getlong (INT columnindex) {return 0 ;}@ override public int getposition () {return position ;}@ override public short getshort (INT columnindex) {return 0 ;} @ override public Boolean getwantsallonmovecils () {return false ;}@ override public Boolean isafterlast () {return false ;}@ override public Boolean isbeforefirst () {return false ;} @ override public Boolean isclosed () {return false ;}@ override public Boolean isfirst () {return false ;}@ override public Boolean islast () {return false ;} @ override public Boolean isnull (INT columnindex) {return false ;}@ override public Boolean move (INT offset) {return false ;}@ override public Boolean movetofirst () {return false ;} @ override public Boolean movetolast () {return false ;}@ override public Boolean movetonext () {return false ;}@ override public Boolean movetoprevious () {return false ;} @ override public void registercontentobserver (contentobserver observer) {}@ override public void registerdatasetobserver (datasetobserver observer) {}@ override public Boolean requery () {return false ;} @ override public bundle respond (bundle extras) {return NULL ;}@ override public void setnotificationuri (contentresolver Cr, Uri URI) {}@ override public void unregistercontentobserver (contentobserver observer) {}@ override public void unregisterdatasetobserver (datasetobserver observer ){}}

The instance of this class can be used as the first parameter data cursor of the alphaindexer constructor.

4. Implementation of custom Adapter
Using the content described above, we can implement the final indexadapter:

 class indexadapter extends simpleadapter implements sectionindexer {private alphabetindexer; Public indexadapter (context, list 
   Data, int resource, string [] From, int [] to) {super (context, Data, resource, from, ); // set data cursor // SET index letter list alphabetindexer = new alphabetindexer (New indexcursor (this), 0, "abcdefghijklmnopqrstuvwxyz");} @ override public object [] getsections () {return alphabetindexer. getsections () ;}@ override public int getpositionforsection (INT section) {return alphabetindexer. getpositionforsection (section);} @ override public int getsectionforposition (INT position) {return alphabetindexer. getsectionforposition (position) ;}

5. Run
The sample data is as follows:

 
Public list <Map <string, string> getdata () {list <Map <string, string> itemlist = new arraylist <Map <string, string> (); string Alphas = "abcdefghijklmnopqrstuvwxyz"; Map <string, string> map = NULL; For (char C: Alphas. tochararray () {for (INT I = 0; I <10; I ++) {map = new hashmap <string, string> (); map. put ("itemtext", "" + C + I); itemlist. add (MAP) ;}} return itemlist ;}

Layout file of the sub-item:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "50dip" Android: gravity = "center_vertical"> <textview Android: Id = "@ + ID/tweaked_item_text" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> </linearlayout>

Use and run:

Protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. tweake_list); tweakedlistview = (listview) findviewbyid (R. id. tweaked_list); // obtain the data list <Map <string, string> itemlist = getdata (); listadapter adapter = new indexadapter (this, itemlist, R. layout. tweake_list_item, new string [] {"itemtext"}, new int [] {R. id. tweaked_item_text}); tweakedlistview. setadapter (adapter );}

The effect is as follows:

6. Conclusion
This index is very useful in displaying large data volumes and is a common knowledge for Android development.
This document is just a simple sample, and the definition must be further expanded in actual work:
(1 ). for processing complex types, you can use the Map Extend the custom object class and use it through adapter conversion.
(2 ). you can dynamically set the index letter list. For example, your list contains only four letters (ABCD). If you still set the index letter list to "abcdefghijklmnopqrstuvwxyz, there will be an index offset problem.
(3). For complex interface display, you can rewrite the getview method of the adapter to customize the view.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.