Android GridView控件
2020-12-23
  • home_activity.xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <?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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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>