Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SideBarView extends View
- {
- private static final char[] alphabet = new char[] { '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', '#' };
- public SideBarView(Context context)
- {
- super(context);
- }
- public SideBarView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- }
- public SideBarView(Context context, AttributeSet attrs, int defStyle)
- {
- super(context, attrs, defStyle);
- }
- @Override
- public void draw(Canvas canvas)
- {
- super.draw(canvas);
- canvas.drawColor(getResources().getColor(R.color.szary_ciemny));
- Paint paint = new Paint();
- final int textHeight = getHeight() / alphabet.length;
- final int width = getWidth();
- for (int i = 0; i < alphabet.length; i++)
- {
- paint.setColor(getResources().getColor(R.color.niebieski));
- paint.setTypeface(Typeface.DEFAULT);
- paint.setAntiAlias(true);
- paint.setTextSize(14);
- final float xPos = width / 2
- - paint.measureText(String.valueOf(alphabet[i])) / 2;
- final float yPos = textHeight * i + textHeight;
- canvas.drawText(String.valueOf(alphabet[i]), xPos, yPos, paint);
- paint.reset();
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event)
- {
- super.onTouchEvent(event);
- final float y = (int) event.getY();
- int idx = (int) (y / getHeight() * alphabet.length);
- if (idx >= alphabet.length) {
- idx = alphabet.length - 1;
- } else if (idx < 0) {
- idx = 0;
- }
- if (event.getAction() == MotionEvent.ACTION_DOWN
- || event.getAction() == MotionEvent.ACTION_MOVE)
- {
- dialogText.setVisibility(View.VISIBLE);
- dialogText.setText(String.valueOf( alphabet[idx]));
- if (sectionIndexter == null)
- {
- sectionIndexter = (SectionIndexer) list.getAdapter();
- }
- int position = sectionIndexter.getPositionForSection(alphabet[idx]);
- if (position == -1)
- {
- return true;
- }
- list.setSelection(position);
- } else
- {
- dialogText.setVisibility(View.INVISIBLE);
- }
- return true;
- }
- private ListView list;
- private SectionIndexer sectionIndexter;
- private TextView dialogText;
- public void setListView(ListView list)
- {
- this.list = list;
- sectionIndexter = (SectionIndexer) list.getAdapter();
- }
- public void setTextView(TextView mDialogText)
- {
- this.dialogText = mDialogText;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement