标签:tin activiti ble apk main str classname gen imageview
本章使用了 ResolveInfo.loadLabel(...) 方法,在启动器应用中显示了各个activity的名
称。 ResolveInfo 类还提供了另一个名为 loadIcon() 的方法。可以使用该方法为每个应用加载
显示图标。你要接受的挑战就是,为NerdLauncher应用中显示的所有应用添加对应的图标。
首先增加一个RecyclerView的条目布局,代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:orientation="horizontal" 5 android:layout_width="match_parent" 6 android:layout_height="wrap_content"> 7 <TextView 8 android:id="@+id/item_text" 9 tools:text="asd" 10 android:textSize="20sp" 11 android:layout_gravity="center" 12 android:layout_width="0dp" 13 android:layout_height="wrap_content" 14 android:layout_weight="2"/> 15 16 <ImageView 17 android:id="@+id/item_icon" 18 android:layout_width="0dp" 19 android:layout_height="wrap_content" 20 android:layout_weight="1"/> 21 </LinearLayout>
较之前只增加了一个ImageView用来显示App的Icon。
接下来直接修改NerdLauncherFragment中的ActivityHolder类和ActivityAdapter类就可以了。代码如下:
1 //用来显示activity标签名 2 private class ActivityHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 3 4 private ResolveInfo mResloveInfo; 5 private TextView mNameTextView; 6 private ImageView mIconImageView; 7 8 public ActivityHolder(View itemView){ 9 super(itemView); 10 mNameTextView = (TextView)itemView.findViewById(R.id.item_text); 11 mIconImageView = (ImageView)itemView.findViewById(R.id.item_icon); 12 mNameTextView.setOnClickListener(this); 13 mIconImageView.setOnClickListener(this); 14 } 15 16 public void bindActivity(ResolveInfo resloveInfo){ 17 mResloveInfo = resloveInfo; 18 PackageManager pm = getActivity().getPackageManager(); 19 String appName = mResloveInfo.loadLabel(pm).toString(); 20 Drawable appIcon = mResloveInfo.loadIcon(pm); 21 mIconImageView.setImageDrawable(appIcon); 22 mNameTextView.setText(appName); 23 } 24 25 @Override 26 public void onClick(View v){ 27 ActivityInfo activityInfo = mResloveInfo.activityInfo; 28 /* 29 * 发送了ACTION_MAIN操作。发送的intent是否包含操作,对大多数app来说没什么区别。 30 * 不过,有些应用的启动行为可能会有所不同。取决于不同的启动要求,同样的activity可能会显示不同的用户界面。开发人员最好能明确启动意图,以便让activity完成它应该完成的任务。 31 */ 32 Intent i = new Intent(Intent.ACTION_MAIN) 33 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 34 .setClassName(activityInfo.applicationInfo.packageName,activityInfo.name); 35 36 startActivity(i); 37 } 38 } 39 40 private class ActivityAdapter extends RecyclerView.Adapter<ActivityHolder> { 41 42 private final List<ResolveInfo> mActivities; 43 44 public ActivityAdapter(List<ResolveInfo> activities){ 45 mActivities = activities; 46 } 47 48 @Override 49 public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType){ 50 LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); 51 View view = layoutInflater.inflate(R.layout.nerd_launcher_item,parent,false); 52 return new ActivityHolder(view); 53 } 54 55 @Override 56 public void onBindViewHolder(ActivityHolder activityHolder, int position){ 57 ResolveInfo resolveInfo = mActivities.get(position); 58 activityHolder.bindActivity(resolveInfo); 59 } 60 61 @Override 62 public int getItemCount(){ 63 return mActivities.size(); 64 } 65 } 66 }
标签:tin activiti ble apk main str classname gen imageview
原文地址:http://www.cnblogs.com/chase1/p/7202393.html