Implementation of the first letter of listview sliding in Android

Source: Internet
Author: User

 

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/

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.