android使用RecyclerView获取应用包名,按照首字母排序,支持字幕条索引,使用android系统自带的汉字转拼音HanziToPinyin类,不使用第三方库完整布局实现代码,包含import
时间: 2024-01-24 16:05:16 浏览: 96
Android代码-简单易用强大的RecyclerView库。
好的,以下是实现该功能的代码,包含import:
```
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.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
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.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private TextView mTvNotice;
private LinearLayout mLayoutIndex;
private IndexView mIndexView;
private List<AppInfo> mAppList = new ArrayList<>();
private Map<String, Integer> mIndexMap = new HashMap<>();
private List<String> mIndexList = new ArrayList<>();
private Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
showIndex();
break;
case 1:
mTvNotice.setVisibility(View.VISIBLE);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recyclerView);
mTvNotice = findViewById(R.id.tv_notice);
mLayoutIndex = findViewById(R.id.layout_index);
mIndexView = findViewById(R.id.indexView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
mRecyclerView.setAdapter(new AppAdapter());
new Thread(new Runnable() {
@Override
public void run() {
loadApps();
}
}).start();
}
private void loadApps() {
PackageManager pm = getPackageManager();
List<ApplicationInfo> appList = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo app : appList) {
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
String pkgName = app.packageName;
String appName = app.loadLabel(pm).toString();
if (!TextUtils.isEmpty(pkgName)) {
AppInfo appInfo = new AppInfo();
appInfo.pkgName = pkgName;
appInfo.appName = appName;
appInfo.pinyin = Pinyin.toPinyin(appName.charAt(0));
mAppList.add(appInfo);
}
}
}
Collections.sort(mAppList, new Comparator<AppInfo>() {
@Override
public int compare(AppInfo o1, AppInfo o2) {
return o1.pinyin.compareToIgnoreCase(o2.pinyin);
}
});
for (int i = 0; i < mAppList.size(); i++) {
String index = mAppList.get(i).pinyin.substring(0, 1);
if (!mIndexMap.containsKey(index)) {
mIndexMap.put(index, i);
}
}
mIndexList.addAll(mIndexMap.keySet());
Collections.sort(mIndexList);
mHandler.sendEmptyMessage(0);
}
private void showIndex() {
mLayoutIndex.removeAllViews();
for (int i = 0; i < mIndexList.size(); i++) {
View view = LayoutInflater.from(this).inflate(R.layout.item_index, null);
TextView tvIndex = view.findViewById(R.id.tv_index);
tvIndex.setText(mIndexList.get(i));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mIndexView.setSelectedIndex(mIndexList.indexOf(tvIndex.getText().toString()));
}
});
mLayoutIndex.addView(view);
}
mIndexView.setIndexList(mIndexList);
mIndexView.setOnIndexChangeListener(new IndexView.OnIndexChangeListener() {
@Override
public void onIndexChange(int index) {
if (mIndexMap.containsKey(mIndexList.get(index))) {
int position = mIndexMap.get(mIndexList.get(index));
mRecyclerView.scrollToPosition(position);
}
}
});
if (mAppList.size() == 0) {
mHandler.sendEmptyMessage(1);
}
}
private class AppInfo {
public String pkgName;
public String appName;
public String pinyin;
}
private class AppAdapter extends RecyclerView.Adapter<AppViewHolder> {
@NonNull
@Override
public AppViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app, parent, false);
return new AppViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AppViewHolder holder, int position) {
AppInfo appInfo = mAppList.get(position);
holder.tvName.setText(appInfo.appName);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 点击事件
}
});
String index = appInfo.pinyin.substring(0, 1);
if (position == mIndexMap.get(index)) {
holder.tvIndex.setVisibility(View.VISIBLE);
holder.tvIndex.setText(index);
} else {
holder.tvIndex.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return mAppList.size();
}
}
private static class AppViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
private TextView tvIndex;
public AppViewHolder(@NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
tvIndex = itemView.findViewById(R.id.tv_index);
}
}
}
```
需要注意的是,需要自己在布局文件中定义 `RecyclerView` 和相关的控件,并且需要自己实现 `IndexView` 和 `DividerItemDecoration`。
阅读全文