In the quick scrolling example of listview, the first letter prompt is added to implement the listview slide and first letter prompt function. However, there are still some problems in practical application. for example, if you switch from another activity or switch to another activity, there will be a problem that the initial letter of the prompt does not disappear. Here, the first letter prompt is implemented in another way, and the above problem is solved, the effect is as follows:
The implementation method is as follows:
Project directory structure:
Overlay is the layout file indicating the letter view, as shown below:
<? XML version = "1.0" encoding = "UTF-8"?>
<Textview xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: minwidth = "100dp" Android: maxwidth = "100dp" Android: gravity = "center"
Android: textsize = "55sp" Android: textcolor = "# ffffff"
Android: Background = "@ drawable/blue_bg" Android: padding = "10dp"
Android: layout_margin = "8dp" Android: visibility = "invisible"/>
In mainactivity, The oncreate method is as follows:
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Mwindowmanager = (windowmanager) getsystemservice (context. window_service );
Setlistadapter (New arrayadapter <string> (this,
Android. R. layout. simple_list_item_1, mstrings ));
Getlistview (). setonscrolllistener (this );
Layoutinflater inflate = (layoutinflater) getsystemservice (context. layout_inflater_service );
Mdialogtext = (textview) inflate. Inflate (R. layout. overlay, null );
Mdialogtext. setvisibility (view. Invisible );
Mhandler. Post (New runnable (){
Public void run (){
Mready = true;
Windowmanager. layoutparams Lp = new windowmanager. layoutparams (
Layoutparams. wrap_content, layoutparams. wrap_content,
Windowmanager. layoutparams. type_application,
Windowmanager. layoutparams. flag_not_touchable
| Windowmanager. layoutparams. flag_not_focusable,
Pixelformat. translucent );
Mwindowmanager. addview (mdialogtext, LP );
}});
}
First, fill in data like in listview:
Setlistadapter (New arrayadapter <string> (this,
Android. R. layout. simple_list_item_1, mstrings ));
Load the view layout of the initial prompt:
Layoutinflater inflate = (layoutinflater) getsystemservice (context. layout_inflater_service );
Mdialogtext = (textview) inflate. Inflate (R. layout. overlay, null );
Mdialogtext. setvisibility (view. Invisible );
Mhandler. Post (New runnable (){
Public void run (){
Mready = true;
Windowmanager. layoutparams Lp = new windowmanager. layoutparams (
Layoutparams. wrap_content, layoutparams. wrap_content,
Windowmanager. layoutparams. type_application,
Windowmanager. layoutparams. flag_not_touchable
| Windowmanager. layoutparams. flag_not_focusable,
Pixelformat. translucent );
Mwindowmanager. addview (mdialogtext, LP );
}});
Register a sliding listener event:
Getlistview (). setonscrolllistener (this );
In the onscroll method of the sliding time, modify the first letter of the display prompt:
If (mready ){
Char firstletter = mstrings [firstvisibleitem]. charat (0 );
If (! Mshowing & firstletter! = Mprevletter ){
Mshowing = true;
Mdialogtext. setvisibility (view. Visible );
}
Mdialogtext. settext (character) firstletter). tostring ());
Mhandler. removecallbacks (mremovewindow );
Mhandler. postdelayed (mremovewindow, 1000 );
Mprevletter = firstletter;
}
Mhandler. postdelayed (mremovewindow, 1000 );
Delay one second execution. Project source code see: http://bigcateasymorse.googlecode.com/svn/trunk/listview_fristletter/