右侧字母索引(类似通讯录),快速定位。使用indexablerecyclerview一套简单高效解决

在这里插入图片描述
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); //添加数据


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值