Android search by letter

Source: Internet
Author: User

When reading Oplayer, You can see Sliding letters to search for listView content. Therefore, the core function deduction is used as a demo, which is divided into two parts: layout, the other is the code. The specific layout is as follows: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1.0" android: orientation = "horizontal"> <FrameLayout android: layout_width = "0dip" android: layout_height = "fill_parent" android: layout_weight = "1.0" android: orientation = "vertical"> <ListView android: id = "@ + id/list" android: layout_width =" Fill_parent "android: layout_height =" fill_parent "android: cacheColorHint =" @ android: color/transparent "android: divider =" @ drawable/ic_line "android: drawSelectorOnTop =" false "android: fadingEdge = "none" android: listSelector = "@ android: color/transparent" android: scrollbars = "none"/> <TextView android: id = "@ + id/first_letter_overlay" android: layout_width = "wrap_content" android: layout_height = "wrap_content" Droid: layout_gravity = "right" android: layout_marginRight = "501_dip" android: layout_marginTop = "49366dip" android: background = "@ drawable/strong" android: gravity = "center" android: paddingBottom = "5.0dip" android: scaleType = "center" android: textSize = "43.0dip" android: visibility = "gone"/> </FrameLayout> <ImageView android: id = "@ + id/alphabet_scroller" android: layout_width = "wrap_content" android: l Ayout_height = "match_parent" android: layout_gravity = "center" android: paddingRight = "0dip" android: scaleType = "fitXY" android: src = "@ drawable/alphabet_scroller_bg"/> </LinearLayout> <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1.0" android: orientation = "horizontal"> <FrameLayout android: layout_width = "0dip" android: layout_height = "fill_parent" android: layout_weight = "1.0" android: orientation = "vertical"> <ListView android: id = "@ + id/list" android: layout_width = "f Ill_parent "android: layout_height =" fill_parent "android: cacheColorHint =" @ android: color/transparent "android: divider =" @ drawable/ic_line "android: drawSelectorOnTop =" false "android: fadingEdge = "none" android: listSelector = "@ android: color/transparent" android: scrollbars = "none"/> <TextView android: id = "@ + id/first_letter_overlay" android: layout_width = "wrap_content" android: layout_height = "wrap_content" and Roid: layout_gravity = "right" android: layout_marginRight = "501_dip" android: layout_marginTop = "491_dip" android: background = "@ drawable/strong" android: gravity = "center" android: paddingBottom = "5.0dip" android: scaleType = "center" android: textSize = "43.0dip" android: visibility = "gone"/> </FrameLayout> <ImageView android: id = "@ + id/alphabet_scroller" android: layout_width = "wrap_content" android: la Yout_height = "match_parent" android: layout_gravity = "center" android: paddingRight = "0dip" android: scaleType = "fitXY" android: src = "@ drawable/alphabet_scroller_bg"/> </LinearLayout> contains an ImageView image [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Selector xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: state_pressed = "true" android: drawable = "@ drawable/contact_list_scroll_pressed"/> <item android: drawable = "@ drawable/contact_list_scroll_normal"/> </selector> <? Xml version = "1.0" encoding = "UTF-8"?> <Selector xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: state_pressed = "true" android: drawable = "@ drawable/contact_list_scroll_pressed"/> <item android: drawable = "@ drawable/contact_list_scroll_normal"/> </selector> the code is as follows: [java] package com. example. test; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import androi D. util. log; import android. view. motionEvent; import android. view. view; import android. view. viewGroup; import android. view. view. onTouchListener; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. listView; import android. widget. textView; public class Main6 extends Activity {ListView iListView; TextView showChar; ImageView myChar; List <String> iList; MAdapter m Adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main5); iListView = (ListView) findViewById (R. id. list); showChar = (TextView) findViewById (R. id. first_letter_overlay); myChar = (ImageView) findViewById (R. id. alphabet_scroller); iList = initChar (); Log. e ("t", "" + iList. size (); mAdapter = new MAdapter (); iListView. setAda Pter (mAdapter); myChar. setClickable (true); myChar. setOnTouchListener (asOnTouch);}/*** A-Z */private OnTouchListener asOnTouch = new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: // 0 myChar. setPressed (true); showChar. setVisibility (View. VISIBLE); mathScrollerPosition (event. getY (); break; case MotionEv Ent. ACTION_UP: // 1 myChar. setPressed (false); showChar. setVisibility (View. GONE); break; case MotionEvent. ACTION_MOVE: mathScrollerPosition (event. getY (); break;} return false ;};/*** display character ** @ param y */private void mathScrollerPosition (float y) {int height = myChar. getHeight (); float charHeight = height/28366f; char c = 'a'; if (y <0) y = 0; else if (y> height) y = height; int index = (I Nt) (y/charHeight)-1; if (index <0) index = 0; else if (index> 25) index = 25; String key = String. valueOf (char) (c + index); showChar. setText (key); int position = 0; if (index = 0) iListView. setSelection (0); else if (index = 25) iListView. setSelection (mAdapter. getCount ()-1); else {if (mAdapter! = Null) {for (int I = 0; I <iList. size (); I ++) {if (iList. get (I ). startsWith (key) {iListView. setSelection (position); break;} position ++; }}}/*** ListView data * @ return */public List <String> initChar () {List <String> list = new ArrayList <String> (); char c = 'a'; for (int I = 0; I <26; I ++) {list. add (String. valueOf (char) (c + I); list. add (String. valueOf (char) (c + I); list. add (Strin G. valueOf (char) (c + I);} return list;} class MAdapter extends BaseAdapter {@ Override public int getCount () {return iList. size () ;}@ Override public Object getItem (int position) {return iList. get (position) ;}@ Override public long getItemId (int position) {return position ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {TextView text = new TextView (getApp LicationContext (); text. setText (iList. get (position); return text ;}} package com. example. test; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. motionEvent; import android. view. view; import android. view. viewGroup; import android. view. view. onTouchListener; import android. widget. baseAdapter; import android Oid. widget. imageView; import android. widget. listView; import android. widget. textView; public class Main6 extends Activity {ListView iListView; TextView showChar; ImageView myChar; List <String> iList; MAdapter mAdapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main5); iListView = (ListView) findViewById (R. id. list); showChar = (Tex TView) findViewById (R. id. first_letter_overlay); myChar = (ImageView) findViewById (R. id. alphabet_scroller); iList = initChar (); Log. e ("t", "" + iList. size (); mAdapter = new MAdapter (); iListView. setAdapter (mAdapter); myChar. setClickable (true); myChar. setOnTouchListener (asOnTouch);}/*** A-Z */private OnTouchListener asOnTouch = new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent) {Switch (event. getAction () {case MotionEvent. ACTION_DOWN: // 0myChar. setPressed (true); showChar. setVisibility (View. VISIBLE); mathScrollerPosition (event. getY (); break; case MotionEvent. ACTION_UP: // 1myChar. setPressed (false); showChar. setVisibility (View. GONE); break; case MotionEvent. ACTION_MOVE: mathScrollerPosition (event. getY (); break;} return false ;};/*** display character ** @ param y */private void mathScrollerPos Ition (float y) {int height = myChar. getHeight (); float charHeight = height/28366f; char c = 'a'; if (y <0) y = 0; else if (y> height) y = height; int index = (int) (y/charHeight)-1; if (index <0) index = 0; else if (index> 25) index = 25; String key = String. valueOf (char) (c + index); showChar. setText (key); int position = 0; if (index = 0) iListView. setSelection (0); else if (index = 25) iListView. se TSelection (mAdapter. getCount ()-1); else {if (mAdapter! = Null) {for (int I = 0; I <iList. size (); I ++) {if (iList. get (I ). startsWith (key) {iListView. setSelection (position); break;} position ++; }}}/*** ListView data * @ return */public List <String> initChar () {List <String> list = new ArrayList <String> (); char c = 'a'; for (int I = 0; I <26; I ++) {list. add (String. valueOf (char) (c + I); list. add (String. valueOf (char) (c + I); list. add (String. valueOf (char) (c + I);} return list;} class MAdapter extends BaseAdapter {@ Overridepublic int getCount () {return iList. size () ;}@ Overridepublic Object getItem (int position) {return iList. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {TextView text = new TextView (getApplicationContext (); text. setText (iList. get (position); return text ;}}}

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.