Advertisement
Guest User

Untitled

a guest
Jan 29th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. public class SideBarView extends View
  2. {
  3.     private static final char[] alphabet = new char[] { 'A', 'B', 'C', 'D',
  4.             'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  5.             'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '#' };
  6.    
  7.     public SideBarView(Context context)
  8.     {
  9.         super(context);
  10.     }
  11.  
  12.     public SideBarView(Context context, AttributeSet attrs)
  13.     {
  14.         super(context, attrs);
  15.     }
  16.  
  17.     public SideBarView(Context context, AttributeSet attrs, int defStyle)
  18.     {
  19.         super(context, attrs, defStyle);
  20.     }
  21.  
  22.     @Override
  23.     public void draw(Canvas canvas)
  24.     {
  25.         super.draw(canvas);
  26.  
  27.         canvas.drawColor(getResources().getColor(R.color.szary_ciemny));
  28.         Paint paint = new Paint();
  29.  
  30.         final int textHeight = getHeight() / alphabet.length;
  31.         final int width = getWidth();
  32.         for (int i = 0; i < alphabet.length; i++)
  33.         {
  34.  
  35.             paint.setColor(getResources().getColor(R.color.niebieski));
  36.             paint.setTypeface(Typeface.DEFAULT);
  37.             paint.setAntiAlias(true);
  38.             paint.setTextSize(14);
  39.  
  40.             final float xPos = width / 2
  41.                     - paint.measureText(String.valueOf(alphabet[i])) / 2;
  42.  
  43.             final float yPos = textHeight * i + textHeight;
  44.            
  45.             canvas.drawText(String.valueOf(alphabet[i]), xPos, yPos, paint);
  46.             paint.reset();
  47.         }
  48.     }
  49.  
  50.     @Override
  51.     public boolean onTouchEvent(MotionEvent event)
  52.     {
  53.         super.onTouchEvent(event);
  54.         final float y = (int) event.getY();
  55.  
  56.         int idx = (int) (y / getHeight() * alphabet.length);
  57.         if (idx >= alphabet.length) {
  58.             idx = alphabet.length - 1;
  59.         } else if (idx < 0) {
  60.             idx = 0;
  61.         }
  62.         if (event.getAction() == MotionEvent.ACTION_DOWN
  63.                 || event.getAction() == MotionEvent.ACTION_MOVE)
  64.         {
  65.             dialogText.setVisibility(View.VISIBLE);
  66.             dialogText.setText(String.valueOf( alphabet[idx]));
  67.             if (sectionIndexter == null)
  68.             {
  69.                 sectionIndexter = (SectionIndexer) list.getAdapter();
  70.             }
  71.             int position = sectionIndexter.getPositionForSection(alphabet[idx]);
  72.             if (position == -1)
  73.             {
  74.                 return true;
  75.             }
  76.             list.setSelection(position);
  77.         } else
  78.         {
  79.             dialogText.setVisibility(View.INVISIBLE);
  80.         }
  81.         return true;
  82.     }
  83.  
  84.     private ListView list;
  85.     private SectionIndexer sectionIndexter;
  86.     private TextView dialogText;
  87.    
  88.     public void setListView(ListView list)
  89.     {
  90.         this.list = list;
  91.         sectionIndexter = (SectionIndexer) list.getAdapter();
  92.     }
  93.    
  94.     public void setTextView(TextView mDialogText)
  95.     {
  96.         this.dialogText = mDialogText;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement