如何在网格视图中显示所有已安装的应用程序,如何在网格视图中显示应用程序图标列表
主要活动:
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
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.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<AppList> installedApps;
private AppAdapter installedAppAdapter;
ListView userInstalledApps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userInstalledApps = (ListView) findViewById(R.id.installed_app_list);
installedApps = getInstalledApps();
installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
String[] colors = {" Open App", " App Info"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose Action")
.setItems(colors, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position of the selected item
if (which==0){
Intent intent = getPackageManager().getLaunchIntentForPackage(installedApps.get(i).packages);
if(intent != null){
startActivity(intent);
}
else {
Toast.makeText(MainActivity.this, installedApps.get(i).packages + " Error, Please Try Again...", Toast.LENGTH_SHORT).show();
}
}
if (which==1){
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + installedApps.get(i).packages));
Toast.makeText(MainActivity.this, installedApps.get(i).packages, Toast.LENGTH_SHORT).show();
startActivity(intent);
}
}
});
builder.show();
}
});
//Total Number of Installed-Apps(i.e. List Size)
String abc = userInstalledApps.getCount()+"";
TextView countApps = (TextView)findViewById(R.id.countApps);
countApps.setText("Total Installed Apps: "+abc);
Toast.makeText(this, abc+" Apps", Toast.LENGTH_SHORT).show();
}
private List<AppList> getInstalledApps() {
PackageManager pm = getPackageManager();
List<AppList> apps = new ArrayList<AppList>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
//List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_PERMISSIONS);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((!isSystemPackage(p))) {
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
String packages = p.applicationInfo.packageName;
apps.add(new AppList(appName, icon, packages));
}
}
return apps;
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
}
public class AppAdapter extends BaseAdapter {
public LayoutInflater layoutInflater;
public List<AppList> listStorage;
public AppAdapter(Context context, List<AppList> customizedListView) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
}
@Override
public int getCount() {
return listStorage.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);
listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.list_app_name);
listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);
listViewHolder.packageInListView=(TextView)convertView.findViewById(R.id.app_package);
listViewHolder.GridView = (GridView)convertView.findViewById(R.id.gridview);
convertView.setTag(listViewHolder);
}else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getName());
listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
listViewHolder.packageInListView.setText(listStorage.get(position).getPackages());
// listViewHolder.GridView.setAdapter((ListAdapter) listStorage.get(position).getIcon());
return convertView;
}
class ViewHolder{
public GridView GridView;
TextView textInListView;
ImageView imageInListView;
TextView packageInListView;
}
}
public class AppList {
private String name;
Drawable icon;
private String packages;
public AppList(String name, Drawable icon, String packages) {
this.name = name;
this.icon = icon;
this.packages = packages;
}
public String getName() {
return name;
}
public Drawable getIcon() {
return icon;
}
public String getPackages() {
return packages;
}
}
}
活动_主要. xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backround"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/countApps"
android:text="Count: "
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/installed_app_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
已安装应用程序列表. xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="100dp"
android:layout_height="70dp"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:id="@+id/app_icon"
android:layout_width="70dp"
android:layout_height="43dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical">
<TextView
android:id="@+id/app_package"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="App Package Name"
android:textSize="0dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="15dp">
<TextView
android:id="@+id/list_app_name"
android:layout_width="match_parent"
android:layout_height="25dp"
android:lineSpacingExtra="0sp"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
你能给予我一些简单的网格视图的源代码吗?我想在网格视图中安装所有应用程序
谢谢你..................
1条答案
按热度按时间e3bfsja21#
为此,您可以使用回收器视图,并将其布局设置为网格布局管理器
只需添加回收器视图依赖性
只需创建一个新活动并测试此示例代码https://abhiandroid.com/materialdesign/recyclerview-gridview.html