android使用RecyclerView获取应用包名,按照首字母排序,支持字幕条索引,使用android系统自带的汉字转拼音HanziToPinyin类,不使用第三方库完整布局实现代码,包含import
时间: 2024-01-09 20:04:38 浏览: 80
好的,以下是实现的代码:
```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.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
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 MyAdapter mAdapter;
private List<AppInfo> mDataList;
private static final int MSG_LOAD_APP_LIST = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_LOAD_APP_LIST:
loadAppList();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mDataList = new ArrayList<>();
mAdapter = new MyAdapter(this, mDataList);
mRecyclerView.setAdapter(mAdapter);
mHandler.sendEmptyMessage(MSG_LOAD_APP_LIST);
}
private void loadAppList() {
new Thread(new Runnable() {
@Override
public void run() {
PackageManager pm = getPackageManager();
List<ApplicationInfo> list = pm.getInstalledApplications(0);
for (ApplicationInfo info : list) {
if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
String appName = info.loadLabel(pm).toString();
String packageName = info.packageName;
if (!TextUtils.isEmpty(appName) && !TextUtils.isEmpty(packageName)) {
AppInfo appInfo = new AppInfo();
appInfo.setAppName(appName);
appInfo.setPackageName(packageName);
mDataList.add(appInfo);
}
}
}
sortDataList();
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}
}).start();
}
private void sortDataList() {
Collections.sort(mDataList, new Comparator<AppInfo>() {
@Override
public int compare(AppInfo o1, AppInfo o2) {
String pinyin1 = Pinyin.toPinyin(o1.getAppName(), "");
String pinyin2 = Pinyin.toPinyin(o2.getAppName(), "");
return pinyin1.compareToIgnoreCase(pinyin2);
}
});
}
private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private Context mContext;
private List<AppInfo> mDataList;
public MyAdapter(Context context, List<AppInfo> dataList) {
mContext = context;
mDataList = dataList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, viewGroup, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
AppInfo appInfo = mDataList.get(i);
myViewHolder.tvAppName.setText(appInfo.getAppName());
myViewHolder.tvPackageName.setText(appInfo.getPackageName());
if (i == 0) {
myViewHolder.tvIndex.setVisibility(View.VISIBLE);
myViewHolder.tvIndex.setText(Pinyin.toPinyin(appInfo.getAppName().substring(0, 1), "").toUpperCase());
} else {
String pinyin1 = Pinyin.toPinyin(mDataList.get(i - 1).getAppName().substring(0, 1), "");
String pinyin2 = Pinyin.toPinyin(appInfo.getAppName().substring(0, 1), "");
if (pinyin1.equalsIgnoreCase(pinyin2)) {
myViewHolder.tvIndex.setVisibility(View.GONE);
} else {
myViewHolder.tvIndex.setVisibility(View.VISIBLE);
myViewHolder.tvIndex.setText(Pinyin.toPinyin(appInfo.getAppName().substring(0, 1), "").toUpperCase());
}
}
}
@Override
public int getItemCount() {
return mDataList.size();
}
}
private static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvIndex;
private TextView tvAppName;
private TextView tvPackageName;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tvIndex = itemView.findViewById(R.id.tv_index);
tvAppName = itemView.findViewById(R.id.tv_app_name);
tvPackageName = itemView.findViewById(R.id.tv_package_name);
}
}
private static class AppInfo {
private String appName;
private String packageName;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}
}
```
布局文件 item_layout.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/tv_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:background="@drawable/bg_index"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="@android:color/white"
android:textSize="14sp"
android:visibility="gone" />
<TextView
android:id="@+id/tv_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tv_index"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_package_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_app_name"
android:layout_toRightOf="@id/tv_index"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
</RelativeLayout>
```
其中,bg_index.xml 用于实现索引字母的背景效果,代码如下:
```xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#80000000" />
<corners android:radius="5dp" />
</shape>
```
阅读全文