原CSDN博客已弃用,文章会逐渐迁移过来。

一、ListView基础案例

首先定义一个接收每一天item的布局

item.app.list.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal">  

    <ImageView  
        android:id="@+id/app_icon_image_view"  
        android:layout_width="60dp"  
        android:layout_height="60dp"  
        android:src="@mipmap/ic_launcher"/>  
    <TextView  
        android:id="@+id/app_name_text_view"  
        android:layout_width="60dp"  
        android:layout_height="60dp"  
        android:paddingLeft="6dp"  
        android:textSize="20sp"  
        android:gravity="center_vertical"  
        android:text="@string/app_name"/>  
</LinearLayout>  

在activity_main.xml中定义一个ListView控件的摆放位置

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  

    <ListView  
        android:id="@+id/app_list_view"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent">  

    </ListView>  

</LinearLayout>  

创建一个Adapter适配器,将item不断的往ListView容器中填充

MainActivity.java

package com.administrator.listviewdemo;  

import android.content.Context;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.ListView;  
import android.widget.TextView;  

import java.util.ArrayList;  
import java.util.List;  

public class MainActivity extends AppCompatActivity {  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        ListView app_list_view = (ListView)findViewById(R.id.app_list_view);  
        List<String> appName = new ArrayList<>();  
        appName.add("QQ");  
        appName.add("微信");  
        appName.add("haha");  

        app_list_view.setAdapter(new AppListAdapter(appName));  
    }  

    /** 
     * 定义适配器操作数据 
     */  
    public class AppListAdapter extends BaseAdapter{  
        List<String> mAppNames;  
        public AppListAdapter(List<String> appName){  
            mAppNames = appName;  
        }  

        @Override  
        public int getCount() {  
            //有多少条数据  
            return mAppNames.size();  
        }  

        @Override  
        public Object getItem(int position) {  
            //获取当前position位置的这一条  
            return mAppNames.get(position);  
        }  

        @Override  
        public long getItemId(int position) {  
            //获取当前position位置的这一条的ID  
            return position;  
        }  

        /** 
         * 这里还应该写一个ViewHolder,考虑到初学者的理解方便,暂时不写 
         */  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            //处理 view--data 填充数据的一个过程  
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            convertView = layoutInflater.inflate(R.layout.item_app_list,null);  
            if(convertView != null){  
                ImageView appIconImageViw = (ImageView)convertView.findViewById(R.id.app_icon_image_view);  
                TextView appNameTextView = (TextView) convertView.findViewById(R.id.app_name_text_view);  

                appNameTextView.setText(mAppNames.get(position));  
            }  
            return convertView;  
        }  
    }  
}  

ViewHolder有什么好处呢,因为TextView appNameTextView写在了ViewHolder里面,这样,getView方法就不用每次都去布局文件中拿View,提高了效率
二、使用listView制作获取手机应用并快速打开功能

item_app_list.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal">  

    <ImageView  
        android:id="@+id/app_icon_image_view"  
        android:layout_width="60dp"  
        android:layout_height="60dp"  
        android:src="@mipmap/ic_launcher"/>  
    <TextView  
        android:id="@+id/app_name_text_view"  
        android:layout_width="60dp"  
        android:layout_height="60dp"  
        android:paddingLeft="6dp"  
        android:textSize="20sp"  
        android:gravity="center_vertical"  
        android:text="@string/app_name"/>  
</LinearLayout>  

header_list.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_width="match_parent"  
    android:layout_height="match_parent">  

    <ImageView  
        android:layout_width="match_parent"  
        android:layout_height="60dp"  
        android:src="@mipmap/ic_launcher"/>  
</LinearLayout>  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:orientation="vertical"  
android:layout_width="match_parent"  
android:layout_height="match_parent">  

<ListView  
    android:id="@+id/app_list_view"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  

</ListView>  

</LinearLayout>  

package com.administrator.listviewdemo;  

import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.content.pm.ComponentInfo;  
import android.content.pm.ResolveInfo;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.AdapterView;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.ListView;  
import android.widget.TextView;  
import android.widget.Toast;  

import java.util.ArrayList;  
import java.util.List;  

public class MainActivity extends AppCompatActivity {  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        ListView app_list_view = (ListView)findViewById(R.id.app_list_view);  


        //添加标题的headerView  
        //解析LayoutInflater  
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        View headerView = layoutInflater.inflate(R.layout.header_list,null);  
        app_list_view.addHeaderView(headerView);  


        final List<ResolveInfo> appInfos = getAppInfos();  
        app_list_view.setAdapter(new AppListAdapter(getAppInfos()));  


        /** 
         * ListView在外面调用的方法 
         */  
        app_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
            @Override  
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
                    //拿到应用的包名  
                    String packageName = appInfos.get(position).activityInfo.packageName;  
                    //拿到主要的类名  
                    String classString = appInfos.get(position).activityInfo.name;  

                    ComponentName componentName = new ComponentName(packageName,classString);  
                    final Intent intent= new Intent();  
                    //通过Intent设置主键  
                    intent.setComponent(componentName);  
                    startActivity(intent);  
            }  
        });  

        /** 
         * 长按时候 
         */  
        app_list_view.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {  
            @Override  
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {  
                Toast.makeText(MainActivity.this, "长按", Toast.LENGTH_SHORT).show();  
                return false;  
            }  
        });  
    }  

    /** 
     * 获取所有的用户信息 
     * @return 
     */  
    private List<ResolveInfo> getAppInfos(){  
        Intent intent = new Intent(Intent.ACTION_MAIN,null);  
        intent.addCategory(Intent.CATEGORY_LAUNCHER);  
        return getPackageManager().queryIntentActivities(intent,0);  
    }  
    /** 
     * 定义适配器操作数据 
     */  
    public class AppListAdapter extends BaseAdapter{  
        List<ResolveInfo> mAppInfo;  
        public AppListAdapter(List<ResolveInfo> appName){  
            mAppInfo = appName;  
        }  

        @Override  
        public int getCount() {  
            //有多少条数据  
            return mAppInfo.size();  
        }  

        @Override  
        public Object getItem(int position) {  
            //获取当前position位置的这一条  
            return mAppInfo.get(position);  
        }  

        @Override  
        public long getItemId(int position) {  
            //获取当前position位置的这一条的ID  
            return position;  
        }  

        @Override  
        public View getView(final int position, View convertView, ViewGroup parent) {  
            //处理 view--data 填充数据的一个过程  
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            convertView = layoutInflater.inflate(R.layout.item_app_list,null);  
            if(convertView != null){  
                ImageView appIconImageView = (ImageView)convertView.findViewById(R.id.app_icon_image_view);  
                TextView appNameTextView = (TextView) convertView.findViewById(R.id.app_name_text_view);  

                appNameTextView.setText(mAppInfo.get(position).activityInfo.loadLabel(getPackageManager()));  
                appIconImageView.setImageDrawable(mAppInfo.get(position).activityInfo.loadIcon(getPackageManager()));  
            }  
//            //为点击的条目设置点击事件,在getView内部设置点击事件的方法  
//            convertView.setOnClickListener(new View.OnClickListener() {  
//                @Override  
//                public void onClick(View v) {  
//                    //拿到应用的包名  
//                    String packageName = mAppInfo.get(position).activityInfo.packageName;  
//                    //拿到主要的类名  
//                    String classString = mAppInfo.get(position).activityInfo.name;  
//  
//                    ComponentName componentName = new ComponentName(packageName,classString);  
//                    final Intent intent= new Intent();  
//                    //通过Intent设置主键  
//                    intent.setComponent(componentName);  
//                    startActivity(intent);  
//                }  
//            });  
            return convertView;  
        }  
    }  
}  

MainActivity.java

增加ViewHolder优化后,修改部分的代码如下:

MainActivity.java

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {  
    ViewHolder viewHolder =new ViewHolder();  
    //处理 view--data 填充数据的一个过程  
    if(convertView != null) {  
        //减少了每次都解析Layout转化为View,和findViewById查找的消耗  
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        convertView = layoutInflater.inflate(R.layout.item_app_list, null);  

        viewHolder.mAppIconImageView = (ImageView)convertView.findViewById(R.id.app_icon_image_view);  
        viewHolder.mAppNameTextView = (TextView) convertView.findViewById(R.id.app_name_text_view);  

        convertView.setTag(viewHolder);  
    }else{  
        viewHolder = (ViewHolder)convertView.getTag();  
    }  

    viewHolder.mAppIconImageView.setImageDrawable(mAppInfo.get(position).activityInfo.loadIcon(getPackageManager()));  
    viewHolder.mAppNameTextView.setText(mAppInfo.get(position).activityInfo.loadLabel(getPackageManager()));  
    return convertView;  
}  
public class ViewHolder{  
    public ImageView mAppIconImageView;  
    public TextView mAppNameTextView;  
}  

完整代码如下:

MainActivity.java

package com.administrator.listviewdemo;  

import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.content.pm.ComponentInfo;  
import android.content.pm.ResolveInfo;  
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.AdapterView;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.ListView;  
import android.widget.TextView;  
import android.widget.Toast;  

import java.util.ArrayList;  
import java.util.List;  

public class MainActivity extends AppCompatActivity {  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        ListView app_list_view = (ListView)findViewById(R.id.app_list_view);  


        //添加标题的headerView  
        //解析LayoutInflater  
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        View headerView = layoutInflater.inflate(R.layout.header_list,null);  
        app_list_view.addHeaderView(headerView);  


        final List<ResolveInfo> appInfos = getAppInfos();  
        app_list_view.setAdapter(new AppListAdapter(getAppInfos()));  


        /** 
         * ListView在外面调用的方法 
         */  
        app_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
            @Override  
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
                    //拿到应用的包名  
                    String packageName = appInfos.get(position).activityInfo.packageName;  
                    //拿到主要的类名  
                    String classString = appInfos.get(position).activityInfo.name;  

                    ComponentName componentName = new ComponentName(packageName,classString);  
                    final Intent intent= new Intent();  
                    //通过Intent设置主键  
                    intent.setComponent(componentName);  
                    startActivity(intent);  
            }  
        });  

        /** 
         * 长按时候 
         */  
        app_list_view.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {  
            @Override  
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {  
                Toast.makeText(MainActivity.this, "长按", Toast.LENGTH_SHORT).show();  
                return false;  
            }  
        });  
    }  

    /** 
     * 获取所有的用户信息 
     * @return 
     */  
    private List<ResolveInfo> getAppInfos(){  
        Intent intent = new Intent(Intent.ACTION_MAIN,null);  
        intent.addCategory(Intent.CATEGORY_LAUNCHER);  
        return getPackageManager().queryIntentActivities(intent,0);  
    }  
    /** 
     * 定义适配器操作数据 
     */  
    public class AppListAdapter extends BaseAdapter{  
        List<ResolveInfo> mAppInfo;  
        public AppListAdapter(List<ResolveInfo> appName){  
            mAppInfo = appName;  
        }  

        @Override  
        public int getCount() {  
            //有多少条数据  
            return mAppInfo.size();  
        }  

        @Override  
        public Object getItem(int position) {  
            //获取当前position位置的这一条  
            return mAppInfo.get(position);  
        }  

        @Override  
        public long getItemId(int position) {  
            //获取当前position位置的这一条的ID  
            return position;  
        }  

        @Override  
        public View getView(final int position, View convertView, ViewGroup parent) {  
            ViewHolder viewHolder =new ViewHolder();  
            //处理 view--data 填充数据的一个过程  
            if(convertView != null) {  
                //减少了每次都解析Layout转化为View,和findViewById查找的消耗  
                LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
                convertView = layoutInflater.inflate(R.layout.item_app_list, null);  

                viewHolder.mAppIconImageView = (ImageView)convertView.findViewById(R.id.app_icon_image_view);  
                viewHolder.mAppNameTextView = (TextView) convertView.findViewById(R.id.app_name_text_view);  

                convertView.setTag(viewHolder);  
            }else{  
                viewHolder = (ViewHolder)convertView.getTag();  
            }  

            viewHolder.mAppIconImageView.setImageDrawable(mAppInfo.get(position).activityInfo.loadIcon(getPackageManager()));  
            viewHolder.mAppNameTextView.setText(mAppInfo.get(position).activityInfo.loadLabel(getPackageManager()));  
            return convertView;  
        }  
        public class ViewHolder{  
            public ImageView mAppIconImageView;  
            public TextView mAppNameTextView;  
        }  
    }  
}