This chapter uses the Resolveinfo.loadlabel (...) method to display the name of each activity in the Launcher app. The ResolveInfo class also provides another method named LoadIcon (). You can use this method to load display icons for each app. The challenge is to add a corresponding icon for all the apps shown in the Nerdlauncher app.
First add a Recyclerview entry layout with the following code:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns: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 <TextView8 Android:id= "@+id/item_text"9 Tools:text= "ASD"Ten android:textsize= "20SP" One android:layout_gravity= "Center" A Android:layout_width= "0DP" - Android:layout_height= "Wrap_content" - Android:layout_weight= "2"/> the - <ImageView - Android:id= "@+id/item_icon" - Android:layout_width= "0DP" + Android:layout_height= "Wrap_content" - Android:layout_weight= "1"/> + </LinearLayout>
Only an icon that ImageView used to display the app was added before.
The next step is to modify the Activityholder class and the Activityadapter class in nerdlauncherfragment directly. The code is as follows:
1 //used to display the activity tag name2 Private classActivityholderextendsRecyclerview.viewholderImplementsview.onclicklistener{3 4 PrivateResolveInfo Mresloveinfo;5 PrivateTextView Mnametextview;6 PrivateImageView Miconimageview;7 8 PublicActivityholder (View itemview) {9 Super(Itemview);TenMnametextview =(TextView) Itemview.findviewbyid (r.id.item_text); OneMiconimageview =(ImageView) Itemview.findviewbyid (R.id.item_icon); AMnametextview.setonclicklistener ( This); -Miconimageview.setonclicklistener ( This); - } the - Public voidbindactivity (ResolveInfo resloveinfo) { -Mresloveinfo =Resloveinfo; -Packagemanager pm =getactivity (). Getpackagemanager (); +String AppName =Mresloveinfo.loadlabel (PM). toString (); -drawable AppIcon =Mresloveinfo.loadicon (PM); + miconimageview.setimagedrawable (AppIcon); A Mnametextview.settext (appName); at } - - @Override - Public voidOnClick (View v) { -Activityinfo Activityinfo =Mresloveinfo.activityinfo; - /* in * Action_main operation was sent. Whether the sent intent contains operations is no different for most apps. - * However, some apps may have a different startup behavior. Depending on the startup requirements, the same activity may show a different user interface. It is a good idea for a developer to explicitly start the intent so that the activity can accomplish the task it should be doing. to */ +Intent i =NewIntent (Intent.action_main) - . Addflags (intent.flag_activity_new_task) the . Setclassname (activityinfo.applicationinfo.packagename,activityinfo.name); * $ startactivity (i);Panax Notoginseng } - } the + Private classActivityadapterextendsRecyclerview.adapter<activityholder> { A the Private FinalList<resolveinfo>mactivities; + - PublicActivityadapter (list<resolveinfo>activities) { $Mactivities =activities; $ } - - @Override the PublicActivityholder Oncreateviewholder (viewgroup parent,intViewType) { -Layoutinflater Layoutinflater =Layoutinflater.from (Getactivity ());WuyiView view = Layoutinflater.inflate (R.layout.nerd_launcher_item,parent,false); the return Newactivityholder (view); - } Wu - @Override About Public voidOnbindviewholder (Activityholder Activityholder,intposition) { $ResolveInfo ResolveInfo =Mactivities.get (position); - activityholder.bindactivity (resolveInfo); - } - A @Override + Public intGetItemCount () { the returnmactivities.size (); - } $ } the}
Android authoritative Programming Guide Challenge Exercise Chapter 22 app icon