@BindView(R.id.rcv)
RecyclerView mRcv;
private List<ActivityInfo> mActivityInfoList;
// List<String> list = new ArrayList<>();
@Override
protected int getLayout() {
return R.layout.activity_main;
}
@Override
protected void initView() {
initToolBar(mToolbar, true, "Study");
initData();
mRcv.setLayoutManager(new LinearLayoutManager(this));
mRcv.setHasFixedSize(true);
Adapter adapter = new Adapter();
mRcv.setAdapter(adapter);
}
private void initData() {
PackageManager pm = getPackageManager();
try {
String packageName = getPackageName();
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
mActivityInfoList = new ArrayList<>();
for (ActivityInfo activityInfo : packageInfo.activities) {
if (!activityInfo.name.contains(getPackageName())) continue;
mActivityInfoList.add(activityInfo);
}
Collections.sort(mActivityInfoList, new Comparator<ActivityInfo>() {
@Override
public int compare(ActivityInfo o1, ActivityInfo o2) {
return o1.name.substring(o1.name.lastIndexOf(".") + 1, o1.name.length()).replace("Activity", "")
.compareTo(o2.name.substring(o2.name.lastIndexOf(".") + 1, o2.name.length()).replace("Activity", ""));
}
});
} catch (PackageManager.NameNotFoundException e) {
}
}
class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Button btn = new Button(MainActivity.this);
ViewGroup.LayoutParams params =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(params);
btn.setPadding(0, 10, 0, 10);
return new RecyclerView.ViewHolder(btn) {
};
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final Button btn = (Button) holder.itemView;
final ActivityInfo activityInfo = mActivityInfoList.get(position);
String name = activityInfo.name;
name = name.substring(name.lastIndexOf(".") + 1, name.length()).replace("Activity", "");
btn.setText(name);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClassLoader classLoader = getClassLoader();
try {
Class<?> aClass = classLoader.loadClass(activityInfo.name);
startActivity(new Intent(MainActivity.this, aClass));
} catch (ClassNotFoundException e) {
}
}
});
}
@Override
public int getItemCount() {
return mActivityInfoList.size();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(false);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w("Fire", "MainActivity:127行:onCreate");
}
网友评论