1 public class NerdLauncherFragment extends Fragment { 2 private static final String TAG = "NerdLauncherFragment"; 3 private RecyclerView mRecyclerView; 4 public static NerdLauncherFragment newInstance() { 5 return new NerdLauncherFragment(); 6 } 7 8 @Nullable 9 @Override 10 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 11 View v = inflater.inflate(R.layout.fragment_nerd_launcher, container, false); 12 mRecyclerView= (RecyclerView) v.findViewById(R.id.fragment_nerd_launcher_recycler_view); 13 mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 14 setupAdapter(); 15 return v; 16 } 17 18 private void setupAdapter() { 19 Intent startupIntent = new Intent(Intent.ACTION_MAIN); 20 startupIntent.addCategory(Intent.CATEGORY_LAUNCHER); 21 PackageManager pm = getActivity().getPackageManager(); 22 List<ResolveInfo> activities = pm.queryIntentActivities(startupIntent, 0); 23 Collections.sort(activities, new Comparator<ResolveInfo>() { 24 @Override 25 public int compare(ResolveInfo lhs, ResolveInfo rhs) { 26 PackageManager pm = getActivity().getPackageManager(); 27 return String.CASE_INSENSITIVE_ORDER.compare(lhs.loadLabel(pm).toString(),rhs.loadLabel(pm).toString()); 28 } 29 }); 30 Log.i(TAG, "Found " + activities.size() + " activities."); 31 mRecyclerView.setAdapter(new ActivityAdapter(activities)); 32 } 33 private class ActivityHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 34 35 private ResolveInfo mResolveInfo; 36 private TextView mNameTextView; 37 private ImageView mImageView; 38 39 public ActivityHolder(View itemView) { 40 super(itemView); 41 mNameTextView= (TextView) itemView.findViewById(R.id.textView); 42 mImageView= (ImageView) itemView.findViewById(R.id.imageView); 43 mNameTextView.setOnClickListener(this); 44 } 45 public void bindActivity(ResolveInfo resolveInfo){ 46 mResolveInfo=resolveInfo; 47 PackageManager pm = getActivity().getPackageManager(); 48 String appName = mResolveInfo.loadLabel(pm).toString(); 49 mNameTextView.setText(appName); 50 Drawable icon = mResolveInfo.loadIcon(pm); 51 mImageView.setImageDrawable(icon); 52 } 53 54 @Override 55 public void onClick(View v) { 56 ActivityInfo activityInfo = mResolveInfo.activityInfo; 57 Intent i = new Intent(Intent.ACTION_MAIN).setClassName(activityInfo.applicationInfo.packageName, activityInfo.name).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 58 startActivity(i); 59 } 60 } 61 private class ActivityAdapter extends RecyclerView.Adapter<ActivityHolder>{ 62 63 private final List<ResolveInfo> mActivities; 64 65 public ActivityAdapter(List<ResolveInfo> activities){ 66 mActivities = activities; 67 } 68 @Override 69 public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) { 70 View view = LayoutInflater.from(getActivity()).inflate(R.layout.list_item, parent, false); 71 return new ActivityHolder(view); 72 } 73 74 @Override 75 public void onBindViewHolder(ActivityHolder holder, int position) { 76 ResolveInfo resolveInfo = mActivities.get(position); 77 holder.bindActivity(resolveInfo); 78 } 79 80 @Override 81 public int getItemCount() { 82 return mActivities.size(); 83 } 84 } 85 }
fragment_nerd_launcher:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_nerd_launcher_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView>
list_item:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
首先写一个类集成Fragment
在onCreateView中用inflate添加一个布局文件,用布局文件找到对应的recycleview,并recycleview.setLayoutManager(new LinearLayoutManager(getActivity()));然后setadapter();
写一个内部类继承RecyclerView.ViewHolder,这个类的构造方法主要是找到控件并设置监听事件.然后写一个方法,这个方法主要是显示recycleview的条目.
然后再写一个内部类继承RecyclerView.Adapter<写上面写的那个viewholder>,重写三个方法onCreateViewHolder,onBindViewHolder,getItemCount.其中onCreateViewHolder方法中托管一个布局作为viewholfer的样式,就像listview的条目,
在onBindViewHolder方法中调用viewholder中的方法.在getItemCount方法中返回recycleview条目的个数.