1.依赖 implementation ‘me.yokeyword:indexablerecyclerview:1.3.0’
2.Activity布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".indexablerecyclerview.IndexActivity">
<me.yokeyword.indexablerv.IndexableLayout
android:id="@+id/index"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:indexBar_background="#00000000"
app:indexBar_layout_width="24dp"
app:indexBar_selectedTextColor="#6D6E6D"
app:indexBar_textColor="#6D6E6D"
app:indexBar_textSize="12sp"
app:indexBar_textSpace="7dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.准备好Bean类
public class IndexBean implements IndexableEntity {
private String name;
private String pinyin;
public IndexBean(String name, String pinyin) {
this.name = name;
this.pinyin = pinyin;
}
@Override
public String getFieldIndexBy() {
return name;//需要根据该属性排序的field
}
@Override
public void setFieldIndexBy(String indexField) {
this.name=indexField;
}
@Override
public void setFieldPinyinIndexBy(String pinyin) {
this.pinyin=pinyin;
//保存排序field的拼音,在执行比如搜索等功能时有用,如不需要,此方法为空
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin= pinyin;
}
}
4.准备Adapter
public class IndexableLayoutAdapter extends IndexableAdapter<IndexBean> {
private Context mContext;
private List<IndexBean> mData;
public IndexableLayoutAdapter(Context mContext, List<IndexBean> data) {
this.mContext = mContext;
this.mData = data;
}
@Override
public RecyclerView.ViewHolder onCreateTitleViewHolder(ViewGroup parent) {
// 创建 TitleItem 布局
View view = LayoutInflater.from(mContext).inflate(R.layout.mine_head_item, parent,false);
return new IndexVH(view);
}
@Override
public RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent) {
// 创建 内容Item 布局
View view = LayoutInflater.from(mContext).inflate(R.layout.item_title, parent,false);
return new ContentVH(view);
}
@Override
public void onBindTitleViewHolder(RecyclerView.ViewHolder holder, String indexTitle) {
// 填充 TitleItem 布局
final IndexVH holderTemp =(IndexVH)holder;
holderTemp.tv.setText(indexTitle);
}
@Override
public void onBindContentViewHolder(RecyclerView.ViewHolder holder, IndexBean entity) {
// 填充 内容Item 布局
final ContentVH holderTemp =(ContentVH)holder;
holderTemp.title.setText(entity.getName());
}
//获取数据布局组件
public static class ContentVH extends RecyclerView.ViewHolder {
TextView title;
public ContentVH(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
}
}
//获取提示栏布局组件
private static class IndexVH extends RecyclerView.ViewHolder {
TextView tv;
public IndexVH(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.tv_index);
}
}
}
5.涉及布局
5.1.mine_head_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_index"
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="城市"
android:background="#ccc"
android:textSize="10sp"
android:textColor="@android:color/white"
android:gravity="center_vertical"
android:paddingLeft="13dp"/>
</LinearLayout>
5.2 item_title
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="20sp"/>
</LinearLayout>
6.主要Activity
public class IndexActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
IndexableLayout index = findViewById(R.id.index);
ArrayList<IndexBean> indexBeans = new ArrayList<>();
indexBeans.add(new IndexBean("a新疆","8"));
index.setLayoutManager(new LinearLayoutManager(this));
IndexableLayoutAdapter indexableLayoutAdapter = new IndexableLayoutAdapter(this,indexBeans );
index.setAdapter(indexableLayoutAdapter);
indexableLayoutAdapter.setDatas(indexBeans); //添加数据
}
}