Android GridView控件

  • home_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </GridView>

</RelativeLayout>
  • HomeActivity.java
package cccc.cccc;

import java.util.ArrayList;
import java.util.List;
import com.example.gridviewstudy.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;

public class HomeActivity extends Activity {

    private GridView mGridView;
    private List<AppInfo> appInfos;

    @SuppressLint("InlinedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏  
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明状态栏  
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//透明导航栏  
        setContentView(R.layout.home_activity);
        mGridView = (GridView)this.findViewById(R.id.gridView1);
        getData();
        mGridView.setNumColumns(6);
        GridViewAdapter adapter = new GridViewAdapter(appInfos,this);
        mGridView.setAdapter(adapter);
    }

    private void getData() {
        PackageManager packageManager = this.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
        appInfos = new ArrayList<AppInfo>();
        for(PackageInfo packageInfo:packageInfos){
            AppInfo appInfo = new AppInfo();
            appInfo.setAppName(packageInfo.applicationInfo.loadLabel(packageManager).toString());
            appInfo.setAppPackage(packageInfo.packageName);
            appInfo.setAppVersion(packageInfo.versionName);
            appInfo.setDrawable(packageInfo.applicationInfo.loadIcon(packageManager));
            appInfo.setUserApp(filterApp(packageInfo.applicationInfo));
            if(appInfo.isUserApp()){
                appInfos.add(appInfo);
            }
        }
    }

    public boolean filterApp(ApplicationInfo info) {
        if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
            return true;
        } else if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
            return true;
        }
        return false;

    }
}
  • AppInfo.java
package cccc.cccc;

import android.graphics.drawable.Drawable;

public class AppInfo {
    private String appName;    // 获取应用程序的名称,不是包名,而是清单文件中的labelname
    private String appVersion; // 获取应用程序的版本号码
    private Drawable drawable; // 获取应用程序的快捷方式图标
    private boolean isUserApp; // 获取应用程序是否是第三方应用程序
    private String appPackage; //给一同程序设置包名
    public String getAppName() {
        return appName;
    }
    public void setAppName(String appName) {
        this.appName = appName;
    }
    public String getAppVersion() {
        return appVersion;
    }
    public void setAppVersion(String appVersion) {
        this.appVersion = appVersion;
    }
    public Drawable getDrawable() {
        return drawable;
    }
    public void setDrawable(Drawable drawable) {
        this.drawable = drawable;
    }
    public boolean isUserApp() {
        return isUserApp;
    }
    public void setUserApp(boolean isUserApp) {
        this.isUserApp = isUserApp;
    }
    public String getAppPackage() {
        return appPackage;
    }
    public void setAppPackage(String appPackage) {
        this.appPackage = appPackage;
    }

}
  • GridViewAdapter.java
package cccc.cccc;

import java.util.List;

import com.example.gridviewstudy.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridViewAdapter extends BaseAdapter {

    private List<AppInfo> appInfos = null;
    private Context mContext = null;
    private LayoutInflater inflater = null;  

    public GridViewAdapter(List<AppInfo> appInfos,Context context) {
        this.appInfos = appInfos;
        this.mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return appInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.gridview_item,null);
            holder = new Holder();
            holder.icon = (ImageView)convertView.findViewById(R.id.imageView1);
            holder.name = (TextView)convertView.findViewById(R.id.textview1);
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag();
        }
        holder.icon.setImageDrawable(appInfos.get(position).getDrawable());
        holder.name.setText(appInfos.get(position).getAppName());
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startAPP(appInfos.get(position).getAppPackage());
            }
        });
        return convertView;
    }

    public class Holder{
        ImageView icon;
        TextView name;
    }

    public void startAPP(String appPackageName){
        try{
            Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(appPackageName);
            mContext.startActivity(intent);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
  • gridview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="110dp"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:text="textview1"
        android:maxLines="1"
        android:textSize="10sp"
        android:textColor="#ffffff"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>
  • AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gridviewstudy"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="cccc.cccc.HomeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页