Android implementation of the imitation Address Book side sidebar sliding Siderbar effect code _android

Source: Internet
Author: User

This article describes the Android implementation of the imitation Address Book side sidebar sliding Siderbar effect code. Share to everyone for your reference, specific as follows:

See some of the application before the sidebar to do a good job, think of yourself to get a out, now share out, of course, there are deficiencies in the place, please forgive me.

First figure:

The code for the implementation is as follows:

Package com.freesonfish.listview_index; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import Android.util.AttributeSet; 
Import android.view.MotionEvent; 
Import Android.view.View; 
  public class Mysidebar extends View {private Ontouchingletterchangedlistener touchlistener; 26 letters public static string[] B = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", " 
  P "," Q "," R "," S "," T "," U "," V "," W "," X "," Y "," Z "}; 
  Private Boolean showbkg = false; 
  int choose =-1; 
  int scrollchoose =-1; 
  Paint Paint = new Paint (); 
  Paint rectpaint = new Paint (); 
  float rectwidth = 0f; 
    Public Mysidebar (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
  Init (); 
    Public Mysidebar (context, AttributeSet attrs) {Super (context, attrs); 
  Init (); 
   Public Mysidebar { Super (context); 
  Init (); 
    private void Init () {Rectpaint.setcolor (Color.parsecolor ("#CCCCCC")); 
  Rectwidth = Paint.measuretext ("#"); 
    /** * Rewrite this method * * protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
    if (showbkg) {Canvas.drawcolor (Color.parsecolor ("#CCCCCC")); 
    Final int height = getheight (); 
    Final int width = getwidth (); 
    Final int singleheight = height/b.length; 
    Final float Xrectpos = ((float) width-paint.measuretext ("#"))/2.0f-rectwidth; 
    Final float XRectPos2 = xrectpos + 3.0f * rectwidth; 
      for (int i = 0; i < b.length; i++) {Paint.setfakeboldtext (true); 
      Paint.setantialias (TRUE);
      Final float xpos = ((float) width-paint.measuretext (b[i]))/2.0f; 
      Final float YPos = singleheight * i + singleheight; 
        if (i = = choose) {Paint.setcolor (color.red); Canvas.drawrect (Xrectpos, ypos-singleheight/2.0f, XRectPos2, YPos + rectWidth, Rectpaint); 
      } canvas.drawtext (B[i], xpos, YPos, paint); 
    Paint.reset (); 
    @Override public boolean dispatchtouchevent (Motionevent event) {Final int action = Event.getaction (); 
    Final float y = event.gety (); 
    Final int c = (int) (Y/getheight () * b.length); 
      Switch (action) {case MotionEvent.ACTION_DOWN:showBkg = true; 
      if (choose!= C && touchlistener!= null) {Doonactiondown (c); 
    } break; 
      Case MotionEvent.ACTION_MOVE:if (choose!= C && touchlistener!= null) {Doonactiondown (c); 
    } break; 
      Case MotionEvent.ACTION_UP:showBkg = false; 
      Invalidate (); 
    Break 
  return true; /** * ListView is called when rolling * * @param c/public void setcolorwhenlistviewscrolling (int c) {if ( 
      Scrollchoose!= c) {scrollchoose = C; 
    String string = ListContantsUtil.AbcList.get (c);  for (int i = c; i < B.length ++i) {if (String.Equals (B[i])) {choose = i; 
          Invalidate (); 
        Break }}/** * When the sidebar is pressed action * @param c */private void Doonactiondown (int c) {if (c &G T 0 && C < b.length) {if (ListContantsUtil.indexPositionMap.containsKey (B[c])) {TOUCHLISTENER.O 
        Ntouchingletterchanged (B[c]); 
        choose = C; 
      Invalidate (); 
        else {c = c-1; 
      Doonactiondown (c); 
  @Override public boolean ontouchevent (Motionevent event) {return super.ontouchevent (event); } public void Setontouchingletterchangedlistener (Ontouchingletterchangedlistener touchlistener) {This.touchListen 
  ER = Touchlistener; /** * is used to notify the activity that the selected letter * @author Freeson */public interface Ontouchingletterchangedlistener 
  {public void ontouchingletterchanged (String s);

 } 
}

Then the Listcontantsutil class is a class that stores the pinyin of the Address book name, as follows:

Package com.freesonfish.listview_index; 
Import java.util.ArrayList; 
Import Java.util.HashMap; 
Import java.util.List; 
  public class Listcontantsutil {static final list<integer> indexpositionlist = new arraylist<integer> (); 
  Static final list<string> abclist = new arraylist<string> (); 
  Static final hashmap<string, integer> indexpositionmap = new hashmap<string, integer> (); static void Putnameindextomap (List<string> List, hashmap<string, string> nameandpinyin) {int lenght = Li 
    St.size (); 
      for (int i = 0; I < lenght ++i) {String name = Nameandpinyin.get (List.get (i)). substring (0, 1); 
      Determines whether the character belongs to a letter or a number or another int ASCII = Name.tochararray () [0]; if (ASCII >= && ASCII <=) {if (!indexpositionmap.containskey (name)) {Indexpositi 
          Onmap.put (name, i); 
          Abclist.add (name); 
        Indexpositionlist.add (i); } else {if (!inDexpositionmap.containskey ("#")) {Indexpositionmap.put ("#", I); 
          Abclist.add ("#"); 
        Indexpositionlist.add (i);

 } 
      } 
    } 
  } 
}

Note that the above procedures are still some small problems, please pay attention to optimize the solution.

Full instance code code click here to download the site.

I hope this article will help you with the Android program.

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.