Screenshot_20220701_223011.jpg Screenshot_20220701_222927.jpg Screenshot_20220701_225004.jpg

1. 默认样式

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <me.luzhuo.indexbarviewdemo.IndexBarView
  9. android:id="@+id/indexBar"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. app:index_text_space="5dp"
  13. android:padding="5dp"
  14. android:layout_alignParentRight="true"/>
  15. <TextView
  16. android:id="@+id/index_tip"
  17. android:layout_width="80dp"
  18. android:layout_height="80dp"
  19. android:layout_centerInParent="true"
  20. android:background="@drawable/bg_index_tip"
  21. android:gravity="center"
  22. android:textColor="@android:color/white"
  23. android:textSize="48sp"
  24. android:visibility="gone"
  25. tools:text="A"
  26. tools:visibility="visible" />
  27. </RelativeLayout>
  1. IndexBarView indexBar = findViewById(R.id.indexBar);
  2. TextView indexBarTip = findViewById(R.id.index_tip);
  3. indexBar.setOnIndexBarCallback(new IndexBarView.OnIndexBarCallback() {
  4. @Override
  5. public void onPressing(int index, String indexStr) {
  6. Log.e(TAG, "index: " + index + " indexStr: " + indexStr);
  7. indexBarTip.setVisibility(View.VISIBLE);
  8. indexBarTip.setText(indexStr);
  9. }
  10. @Override
  11. public void onPressed() {
  12. Log.e(TAG, "onPressed");
  13. indexBarTip.setVisibility(View.GONE);
  14. }
  15. });

2. 使用样式

内置样式 CircleBackgroundStyle

Screenshot_20220701_223129.jpg

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <me.luzhuo.indexbarviewdemo.IndexBarView
  9. android:id="@+id/indexBar"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. app:index_text_size_normal="14sp"
  13. app:index_text_size_pressed="14sp"
  14. app:index_text_color_pressed="#ffffff"
  15. app:index_text_color_normal="#333333"
  16. app:index_text_space="5dp"
  17. android:padding="5dp"
  18. android:layout_alignParentRight="true"/>
  19. <TextView
  20. android:id="@+id/index_tip"
  21. android:layout_width="80dp"
  22. android:layout_height="80dp"
  23. android:layout_centerInParent="true"
  24. android:background="@drawable/bg_index_tip"
  25. android:gravity="center"
  26. android:textColor="@android:color/white"
  27. android:textSize="48sp"
  28. android:visibility="gone"
  29. tools:text="A"
  30. tools:visibility="visible" />
  31. </RelativeLayout>
  1. IndexBarView indexBar = findViewById(R.id.indexBar);
  2. TextView indexBarTip = findViewById(R.id.index_tip);
  3. indexBar.setPressedStyle(new CircleBackgroundStyle(indexBar.getPressedTextSize(), indexBar.getPressedTextColor(), 0xFF3700B3));
  4. indexBar.setOnIndexBarCallback(new IndexBarView.OnIndexBarCallback() {
  5. @Override
  6. public void onPressing(int index, String indexStr) {
  7. Log.e(TAG, "index: " + index + " indexStr: " + indexStr);
  8. indexBarTip.setVisibility(View.VISIBLE);
  9. indexBarTip.setText(indexStr);
  10. }
  11. @Override
  12. public void onPressed() {
  13. Log.e(TAG, "onPressed");
  14. indexBarTip.setVisibility(View.GONE);
  15. }
  16. });

3. 自定义数据

  1. indexBar.setIndexStrings(new String[]{"顶部", "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", "#"});

Screenshot_20220701_224602.jpg