android使用RecyclerView获取应用包名,按照首字母排序,支持字幕条索引,使用android系统自带的汉字转拼音HanziToPinyin类,汉字转拼音不使用import第三方库,代码使用androidx库完整布局实现代码,包含import,实现setOnTouchLetterChangeListenner

时间: 2024-01-30 22:04:19 浏览: 22
好的,以下是完整的实现代码: 1. 在布局文件中添加 RecyclerView 和字母索引条: ```xml <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.example.indexbar.IndexBar android:id="@+id/index_bar" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentEnd="true" android:layout_marginEnd="10dp" android:background="@android:color/transparent" app:indexBarTextColor="@color/colorPrimary" app:indexBarTextSize="12sp" /> ``` 2. 在 Activity 或 Fragment 中,初始化 RecyclerView 和 IndexBar: ```java import android.os.Bundle; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import me.yokeyword.indexablerv.IndexableAdapter; import me.yokeyword.indexablerv.IndexableLayout; import me.yokeyword.indexablerv.SimpleHeaderAdapter; import me.yokeyword.indexablerv.SimpleIndexableAdapter; import me.yokeyword.indexablerv.SimpleLayoutManager; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private IndexBar mIndexBar; private List<AppInfo> mDatas = new ArrayList<>(); private List<IndexableEntity<AppInfo>> mIndexDatas = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.recycler_view); mIndexBar = findViewById(R.id.index_bar); // 初始化 RecyclerView 的布局管理器、适配器和数据源 mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mDatas = getData(); mIndexDatas = getIndexDatas(mDatas); mRecyclerView.setAdapter(new SimpleAdapter(this, R.layout.item_app, mDatas)); mRecyclerView.addItemDecoration(new SimpleItemDecoration(this)); mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // 根据 RecyclerView 的滚动位置更新字母索引条的选中状态 int position = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); mIndexBar.setSelectedIndex(mIndexDatas.indexOf(mDatas.get(position))); } }); // 初始化字母索引条的监听器 mIndexBar.setOnTouchLetterChangeListenner(new IndexBar.OnTouchLetterChangeListenner() { @Override public void onTouchLetterChange(String letter) { // 根据字母索引条的触摸事件,滚动 RecyclerView 到相应位置 for (int i = 0; i < mDatas.size(); i++) { if (TextUtils.equals(letter, mDatas.get(i).getSortLetters())) { ((LinearLayoutManager) mRecyclerView.getLayoutManager()) .scrollToPositionWithOffset(i, 0); return; } } } }); // 初始化字母索引条的数据源,并设置到 IndexBar 中 List<String> indexList = getIndexList(mIndexDatas); mIndexBar.setIndexList(indexList); } // 获取应用列表数据源 private List<AppInfo> getData() { List<AppInfo> list = new ArrayList<>(); PackageManager pm = getPackageManager(); List<ApplicationInfo> apps = pm.getInstalledApplications(0); for (ApplicationInfo app : apps) { if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { String name = app.loadLabel(pm).toString(); String packageName = app.packageName; list.add(new AppInfo(name, packageName)); } } Collections.sort(list, new Comparator<AppInfo>() { @Override public int compare(AppInfo o1, AppInfo o2) { return o1.getSortLetters().compareTo(o2.getSortLetters()); } }); return list; } // 将应用列表数据源转换成 IndexableEntity 数据源 private List<IndexableEntity<AppInfo>> getIndexDatas(List<AppInfo> list) { List<IndexableEntity<AppInfo>> indexList = new ArrayList<>(); for (AppInfo app : list) { String pinyin = HanziToPinyin.getPinYin(app.getName()); if (!TextUtils.isEmpty(pinyin)) { app.setPinyin(pinyin); String sortLetters = pinyin.substring(0, 1).toUpperCase(); if (sortLetters.matches("[A-Z]")) { app.setSortLetters(sortLetters); indexList.add(new IndexEntity<>(app)); } else { app.setSortLetters("#"); indexList.add(new IndexEntity<>(app)); } } else { app.setSortLetters("#"); indexList.add(new IndexEntity<>(app)); } } return indexList; } // 获取字母索引条的数据源 private List<String> getIndexList(List<IndexableEntity<AppInfo>> list) { List<String> indexList = new ArrayList<>(); for (IndexableEntity<AppInfo> entity : list) { indexList.add(entity.getFieldIndexBy()); } return indexList; } // IndexableAdapter 适配器 public static class SimpleAdapter extends IndexableAdapter<AppInfo> { private int mLayoutRes; private LayoutInflater mInflater; public SimpleAdapter(Context context, int layoutRes, List<AppInfo> datas) { super(datas); mLayoutRes = layoutRes; mInflater = LayoutInflater.from(context); } @Override public RecyclerView.ViewHolder onCreateTitleViewHolder(ViewGroup parent) { View view = mInflater.inflate(R.layout.item_index_title, parent, false); return new TitleViewHolder(view); } @Override public RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent) { View view = mInflater.inflate(mLayoutRes, parent, false); return new ContentViewHolder(view); } @Override public void onBindTitleViewHolder(RecyclerView.ViewHolder holder, String indexTitle) { TitleViewHolder viewHolder = (TitleViewHolder) holder; viewHolder.tvTitle.setText(indexTitle); } @Override public void onBindContentViewHolder(RecyclerView.ViewHolder holder, AppInfo item) { ContentViewHolder viewHolder = (ContentViewHolder) holder; viewHolder.tvName.setText(item.getName()); viewHolder.tvPackageName.setText(item.getPackageName()); } private static class TitleViewHolder extends RecyclerView.ViewHolder { TextView tvTitle; TitleViewHolder(View itemView) { super(itemView); tvTitle = itemView.findViewById(R.id.tv_title); } } private static class ContentViewHolder extends RecyclerView.ViewHolder { TextView tvName; TextView tvPackageName; ContentViewHolder(View itemView) { super(itemView); tvName = itemView.findViewById(R.id.tv_name); tvPackageName = itemView.findViewById(R.id.tv_package_name); } } } // IndexableEntity 实体类 public static class IndexEntity<T> implements IndexableEntity { private T data; public IndexEntity(T data) { this.data = data; } @Override public String getFieldIndexBy() { return ((AppInfo) data).getSortLetters(); } @Override public void setFieldIndexBy(String indexField) { ((AppInfo) data).setSortLetters(indexField); } @Override public void setFieldPinyinIndexBy(String pinyin) { ((AppInfo) data).setPinyin(pinyin); } public T getData() { return data; } } // AppInfo 实体类 public static class AppInfo { private String name; private String packageName; private String pinyin; private String sortLetters; public AppInfo(String name, String packageName) { this.name = name; this.packageName = packageName; } public String getName() { return name; } public String getPackageName() { return packageName; } public String getPinyin() { return pinyin; } public void setPinyin(String pinyin) { this.pinyin = pinyin; } public String getSortLetters() { return sortLetters; } public void setSortLetters(String sortLetters) { this.sortLetters = sortLetters; } } // HanziToPinyin 汉字转拼音工具类 public static class HanziToPinyin { private static final String TAG = "HanziToPinyin"; public static String getPinYin(String input) { StringBuilder sb = new StringBuilder(); char[] inputArray = input.toCharArray(); for (char c : inputArray) { String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c); if (pinyinArray != null && pinyinArray.length > 0) { sb.append(pinyinArray[0]); } } return sb.toString(); } } } ``` 其中,IndexBar 和 SimpleItemDecoration 可以在 Github 上的 IndexBar 项目中找到。具体使用方式可以参考该项目的 README.md 文件。

相关推荐

最新推荐

recommend-type

【车牌识别】 GUI BP神经网络车牌识别(带语音播报)【含Matlab源码 668期】.zip

Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描视频QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
recommend-type

【作业视频】六年级第1讲--计算专项训练(2022-10-28 22-51-53).mp4

【作业视频】六年级第1讲--计算专项训练(2022-10-28 22-51-53).mp4
recommend-type

3文件需求申请单.xls

3文件需求申请单.xls
recommend-type

【脑肿瘤检测】 GUI SOM脑肿瘤检测【含Matlab源码 2322期】.zip

【脑肿瘤检测】 GUI SOM脑肿瘤检测【含Matlab源码 2322期】
recommend-type

GOGO语言基础教程、实战案例和实战项目讲解

GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解GO语言基础教程、实战案例和实战项目讲解
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

云原生架构与soa架构区别?

云原生架构和SOA架构是两种不同的架构模式,主要有以下区别: 1. 设计理念不同: 云原生架构的设计理念是“设计为云”,注重应用程序的可移植性、可伸缩性、弹性和高可用性等特点。而SOA架构的设计理念是“面向服务”,注重实现业务逻辑的解耦和复用,提高系统的灵活性和可维护性。 2. 技术实现不同: 云原生架构的实现技术包括Docker、Kubernetes、Service Mesh等,注重容器化、自动化、微服务等技术。而SOA架构的实现技术包括Web Services、消息队列等,注重服务化、异步通信等技术。 3. 应用场景不同: 云原生架构适用于云计算环境下的应用场景,如容器化部署、微服务
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。