很多项目里都会用到快速定位侧边栏,好比如联系人列表界面、城市列表等;
所以为了方便,我就自己打包了一个控件依赖库,方便自己使用也方便别人使用;
普通图片:
GIF图:
Step 1. Add the JitPack repository to your build file
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Step 2. Add the dependency
dependencies { implementation 'com.github.D10NGYANG:DL10SideBar:1.0.0' }
Step 3. 在布局中使用
<com.dlong.rep.dlsidebar.DLSideBar android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignParentRight="true" app:dialogTextBackground="@drawable/ic_launcher_background" app:dialogTextBackgroundHeight="100dp" app:dialogTextBackgroundWidth="100dp" app:dialogTextColor="@color/colorAccent" app:dialogTextSize="25sp" app:sideBackground="@android:color/holo_orange_light" app:sideTextArray="@array/dl_side_bar_def_list" app:sideTextColor="@color/colorPrimaryDark" app:sideTextSelectColor="@color/colorAccent" app:sideTextSize="16sp" />
其中可以设置的参数包含以下:
<declare-styleable name="DLSideBar"> <attr name="sideTextArray" format="reference" /> <!--sidebar的字符列表--> <attr name="sideTextColor" format="color" /> <!--sidebar的字符颜色--> <attr name="sideTextSelectColor" format="color" /> <!--sidebar的字符选中时的颜色--> <attr name="sideTextSize" format="dimension" /> <!--sidebar的字符大小--> <attr name="sideBackground" format="reference|color" /> <!--sidebar的背景颜色--> <attr name="dialogTextColor" format="color" /> <!--选中弹窗字符颜色--> <attr name="dialogTextSize" format="dimension" /> <!--选中弹窗字符大小--> <attr name="dialogTextBackground" format="reference|color" /> <!--选中弹窗字符背景颜色--> <attr name="dialogTextBackgroundWidth" format="dimension" /> <!--选中弹窗字符背景宽度--> <attr name="dialogTextBackgroundHeight" format="dimension" /> <!--选中弹窗字符背景高度--> </declare-styleable>
Step 4. 在Activity中监听
private DLSideBar sbIndex;
sbIndex = findViewById(R.id.sb_index); // 配置右侧index sbIndex.setOnTouchingLetterChangedListener(mSBTouchListener);
/** * 右侧index选项监听 */ private DLSideBar.OnTouchingLetterChangedListener mSBTouchListener = new DLSideBar.OnTouchingLetterChangedListener() { @Override public void onTouchingLetterChanged(String str) { // 返回的是字母A~Z // TODO: 2019/4/1 这里更新你的列表 } };
完事。
整个项目的源码在GitHub上,欢迎查看和提交意见
D10NGYANG/DL10SideBar