android使用RecyclerView获取应用包名,按照首字母排序,支持字幕条索引,使用android系统自带的汉字转拼音HanziToPinyin类,不使用第三方库完整布局实现代码,包含import
时间: 2024-01-30 10:04:32 浏览: 27
好的,以下是示例代码,可以参考:
```java
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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 com.github.promeg.pinyinhelper.Pinyin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private AppListAdapter mAdapter;
private List<AppInfo> mAppList = new ArrayList<>();
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message message) {
switch (message.what) {
case 0:
mAdapter.setData(mAppList);
break;
}
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mAdapter = new AppListAdapter(this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
new Thread(new Runnable() {
@Override
public void run() {
mAppList = getAppList(MainActivity.this);
Collections.sort(mAppList, new Comparator<AppInfo>() {
@Override
public int compare(AppInfo appInfo, AppInfo t1) {
return Pinyin.toPinyin(appInfo.appName.charAt(0)).compareTo(Pinyin.toPinyin(t1.appName.charAt(0)));
}
});
mHandler.sendEmptyMessage(0);
}
}).start();
}
private List<AppInfo> getAppList(Context context) {
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> appList = pm.getInstalledApplications(0);
List<AppInfo> result = new ArrayList<>();
for (ApplicationInfo applicationInfo : appList) {
if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
AppInfo appInfo = new AppInfo();
appInfo.appName = applicationInfo.loadLabel(pm).toString();
appInfo.packageName = applicationInfo.packageName;
result.add(appInfo);
}
}
return result;
}
private static class AppInfo {
String appName;
String packageName;
}
private static class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.ViewHolder> {
private Context mContext;
private List<AppInfo> mData = new ArrayList<>();
private List<String> mIndexList = new ArrayList<>();
public AppListAdapter(Context context) {
mContext = context;
}
public void setData(List<AppInfo> data) {
mData.clear();
mData.addAll(data);
generateIndexList();
notifyDataSetChanged();
}
private void generateIndexList() {
mIndexList.clear();
for (AppInfo appInfo : mData) {
String pinyin = Pinyin.toPinyin(appInfo.appName.charAt(0));
if (!TextUtils.isEmpty(pinyin)) {
String index = pinyin.substring(0, 1).toUpperCase();
if (!mIndexList.contains(index)) {
mIndexList.add(index);
}
}
}
Collections.sort(mIndexList);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_app, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
AppInfo appInfo = mData.get(position);
holder.tvName.setText(appInfo.appName);
holder.itemView.setTag(appInfo);
if (position == 0) {
holder.tvIndex.setVisibility(View.VISIBLE);
holder.tvIndex.setText(Pinyin.toPinyin(appInfo.appName.charAt(0)).substring(0, 1).toUpperCase());
} else {
AppInfo preAppInfo = mData.get(position - 1);
String preIndex = Pinyin.toPinyin(preAppInfo.appName.charAt(0)).substring(0, 1).toUpperCase();
String index = Pinyin.toPinyin(appInfo.appName.charAt(0)).substring(0, 1).toUpperCase();
if (preIndex.equals(index)) {
holder.tvIndex.setVisibility(View.GONE);
} else {
holder.tvIndex.setVisibility(View.VISIBLE);
holder.tvIndex.setText(index);
}
}
}
@Override
public int getItemCount() {
return mData.size();
}
private static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
TextView tvIndex;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
tvIndex = itemView.findViewById(R.id.tv_index);
}
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
}
}
```
布局文件 `activity_main.xml`:
```xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
```
其中,列表项布局文件 `item_app.xml`:
```xml
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_index"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="16sp"
android:textColor="@color/black"
android:background="@color/gray"
android:visibility="gone"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="16sp"
android:textColor="@color/black"/>
</LinearLayout>
```
注意:这里使用了 `PinyinHelper` 库,需要在 `build.gradle` 文件中添加依赖:
```groovy
implementation 'com.github.promeg:pinyinhelper:1.0.2'
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)