NerdLauncherActivity.java
package com.example.aster.nerdlauner;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NerdLauncherActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return NerdLauncherFragment.newInstance();
}
}
NerdLauncherFragment.java
package com.example.aster.nerdlauner;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class NerdLauncherFragment extends Fragment {
private static final String TAG = "NerdLauncherFragment";
private RecyclerView mRecyclerView;
public static NerdLauncherFragment newInstance() {
return new NerdLauncherFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_nerd_launcher, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.app_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
setupAdapter();
return v;
}
private void setupAdapter() {
Intent startupIntent = new Intent(Intent.ACTION_MAIN);
startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager pm = getActivity().getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(startupIntent, 0);
Collections.sort(activities, new Comparator<ResolveInfo>() {
public int compare(ResolveInfo a, ResolveInfo b) {
PackageManager pm = getActivity().getPackageManager();
return String.CASE_INSENSITIVE_ORDER.compare(
a.loadLabel(pm).toString(),
b.loadLabel(pm).toString());
}
});
Log.i(TAG, "Found" + activities.size() + " activities.");
mRecyclerView.setAdapter(new ActivityAdapter(activities));
}
private class ActivityHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
private ResolveInfo mResolveInfo;
private TextView mNameTextView;
public ActivityHolder(View itemView) {
super(itemView);
mNameTextView = (TextView) itemView;
mNameTextView.setOnClickListener(this);
}
public void bindActivity(ResolveInfo resolveInfo) {
mResolveInfo = resolveInfo;
PackageManager pm = getActivity().getPackageManager();
String appName = mResolveInfo.loadLabel(pm).toString();
mNameTextView.setText(appName);
}
@Override
public void onClick(View v) {
ActivityInfo activityInfo = mResolveInfo.activityInfo;
Intent i = new Intent(Intent.ACTION_MAIN)
.setClassName(activityInfo.applicationInfo.packageName,
activityInfo.name);
startActivity(i);
}
}
private class ActivityAdapter extends RecyclerView.Adapter<ActivityHolder> {
private final List<ResolveInfo> mActivities;
public ActivityAdapter(List<ResolveInfo> activities) {
mActivities = activities;
}
@Override
public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater
.inflate(android.R.layout.simple_list_item_1, parent, false);
return new ActivityHolder(view);
}
@Override
public void onBindViewHolder(ActivityHolder holder, int position) {
ResolveInfo resolveInfo = mActivities.get(position);
holder.bindActivity(resolveInfo);
}
@Override
public int getItemCount() {
return mActivities.size();
}
}
}
SingleFragmentActivity.java
package com.example.aster.nerdlauner;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@LayoutRes
protected int getLayoutResId() {
return R.layout.activity_fragment;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
}
activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
fragment_nerd_launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
可以启动对应的应用