Android编程权威指南第三版 第24章

挑战练习:应用图标

    前面,为在启动器应用中显示各个activity的名称,你使用了ResolveInfo.loadLabel(...) 方法。loadIcon()是ResolveInfo类的另一个方法,可以用它为每个应用加载显示图标。作为 练习,请给NerdLauncher应用中显示的所有应用添加图标。 

解决办法:

一、创建recyclerView中item的布局文件(item_recycler_view.xml),包含一个ImageView和一个TextView。



    
    

二、修改NerdLauncherFragment.java文件,从mResolveInfo中获取activity的图标。

package android.bignerdranch.com.nerdlauncher;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.ImageView;
import android.widget.TextView;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class NerdLauncherFragment extends Fragment {
    private RecyclerView mRecyclerView;

    private static final String TAG = "NerdLauncherFragment";

    public static NerdLauncherFragment newInstance(){
        return new NerdLauncherFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 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 activities = pm.queryIntentActivities(startupIntent, 0);
        Collections.sort(activities, new Comparator() {
            @Override
            public int compare(ResolveInfo o1, ResolveInfo o2) {
                PackageManager packageManager = getActivity().getPackageManager();
                return String.CASE_INSENSITIVE_ORDER.compare(
                        o1.loadLabel(packageManager).toString(),
                        o2.loadLabel(packageManager).toString()
                );
            }
        });
        Log.i(TAG, "Found " + activities.size() + " activities.");
        mRecyclerView.setAdapter(new ActivityAdapter(activities));
    }
    //Holder
    private class ActivityHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private ResolveInfo mResolveInfo;
        private TextView mNameTextView;
        //添加实例
        private ImageView mIconImageView;


        public ActivityHolder(@NonNull View itemView) {
            super(itemView);
            mNameTextView = (TextView)itemView.findViewById(R.id.item_text);
            //绑定资源id
            mIconImageView = (ImageView)itemView.findViewById(R.id.item_icon);
            mNameTextView.setOnClickListener(this);
            //设置setOnClickListener
            mIconImageView.setOnClickListener(this);
        }

        private void bindActivity(ResolveInfo resolveInfo) {
            mResolveInfo = resolveInfo;
            PackageManager packageManager = getActivity().getPackageManager();
            String appName = mResolveInfo.loadLabel(packageManager).toString();
            //使用loadIcon方法从mResolveInfo中获取activity的图标
            Drawable appIcon = mResolveInfo.loadIcon(packageManager);
            //setImageDrawable方法设置
            mIconImageView.setImageDrawable(appIcon);
            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)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        }
    }
    //Adapter
    private class ActivityAdapter extends RecyclerView.Adapter{

        private final List mActivities;


        public ActivityAdapter(List activities){
            mActivities = activities;
        }

        @NonNull
        @Override
        public ActivityHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            //视图修改为新创建的item的布局
            View view = layoutInflater
                    .inflate(R.layout.item_recycler_view,viewGroup,false);
            return new ActivityHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ActivityHolder activityHolder, int i) {
            ResolveInfo resolveInfo = mActivities.get(i);
            activityHolder.bindActivity(resolveInfo);

        }

        @Override
        public int getItemCount() {
            return mActivities.size();
        }
    }
}

三、修改结果如下图

Android编程权威指南第三版 第24章_第1张图片

你可能感兴趣的:(Android编程权威指南第三版 第24章)