首先看一下效果图
旁边是有一行的字母索引,当点击字母的时候同时列表也会随之改变
这用到了自定义View
下面看一下我的代码。
public class ContactListView extends RelativeLayout {
private RecyclerView recyclerView;
private SwipeRefreshLayout swipeRefreshLayout;
public ContactListView(Context context) {
this(context,null);
}
public ContactListView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public ContactListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.contact_list_layout, this, true);
recyclerView= findViewById(R.id.contact_recyclerView);
swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ContactListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
this(context, attrs, defStyleAttr);
}
public void setAdapter(RecyclerView.Adapter adapter){
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
}
public void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener onRefreshListener){
swipeRefreshLayout.setOnRefreshListener(onRefreshListener);
}
public void setRefre(boolean b) {
if (swipeRefreshLayout!=null){
swipeRefreshLayout.setRefreshing(b);
}
}
这里是封装了RecyclerView的刷新事件,下面才是重要的东西
public class Slidebar extends View {
private TextView tvFloat;
private RecyclerView contactRecyclerView;
private Paint paint;
private float avgHeight;
private static final String[] SECTIONS = {"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 int measuredWidth;
private int measuredHeight;
public Slidebar(Context context) {
super(context,null);
}
public Slidebar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public Slidebar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public Slidebar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
this(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
showToastAndScroll(event.getY());
break;
case MotionEvent.ACTION_UP:
if (tvFloat!=null){
tvFloat.setVisibility(GONE);
}
setBackgroundColor(Color.TRANSPARENT);
break;
}
return true;
}
private void showToastAndScroll(float y) {
if (tvFloat==null){
ViewGroup parent = (ViewGroup) getParent();
tvFloat = parent.findViewById(R.id.tv_float);
contactRecyclerView = parent.findViewById(R.id.contact_recyclerView);
}
tvFloat.setVisibility(VISIBLE);
setBackgroundResource(R.drawable.slidebar_bk);
int index= (int) (y/avgHeight);
if (index<0){
index=0;
}else if (index>SECTIONS.length-1){
index=SECTIONS.length-1;
}
String slideBarSection =SECTIONS[index];
tvFloat.setText(slideBarSection);
// ContactAdapter contactAdapter = (ContactAdapter) contactRecyclerView.getAdapter();
// List<String> data = contactAdapter.getDate();
// for (int i = 0; i < data.size() ; i++) {
// if (StringUtils.getInital(data.get(i)).equals(slideBarSection)){
// contactRecyclerView.smoothScrollToPosition(i);
// return;}
// }
RecyclerView.Adapter adapter=contactRecyclerView.getAdapter();
if (!(adapter instanceof SectionIndexer)){
return;
}
SectionIndexer sectionIndexer= (SectionIndexer) adapter;
//获取真实中的所有的分区
String[] sections= (String[]) sectionIndexer.getSections();
//当前用户点击的是J,然后需要找到J在真实分区占的角脚标
int sectionIndex= Arrays.binarySearch(sections,slideBarSection);
//根据sections的脚表找到条目的脚标
if (sectionIndex<0){
return;
}
int positionForSection=sectionIndexer.getPositionForSection(sectionIndex);
//根据条目的脚标让RecyclerView定位到该位置
contactRecyclerView.scrollToPosition(positionForSection);
}
private void initView(Context context) {
paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.parseColor("#8c8c8c"));
paint.setTextSize(DensityUtil.sp2px(getContext(),10));
paint.setTextAlign(Paint.Align.CENTER);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measuredWidth = getMeasuredWidth();
measuredHeight = getMeasuredHeight()-getPaddingBottom();
avgHeight = (measuredHeight + 0.f) / SECTIONS.length;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float x=measuredWidth/2;
for (int i = 0; i <SECTIONS.length ; i++) {
float y=avgHeight*(i+1);
canvas.drawText(SECTIONS[i],x,y,paint);
}
}