左一效果图。左2MainActivity
---------------------------------//app信息工具类---------------------------------------
import android.graphics.drawable.Drawable; import java.io.Serializable; public class AppInfo implements Serializable {/** 序列号 */private static final long serialVersionUID = -6660233212727684115L;/** 名称 */public String name;/** (路径) */public String path;/** 图标 */public Drawable icon;/** 包名 */public String packageName;public int a;public int b;public static long getSerialVersionUID() {return serialVersionUID;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}public Drawable getIcon() {return icon;}public void setIcon(Drawable icon) {this.icon = icon;}public String getPackageName() { return packageName; }public void setPackageName(String packageName) {this.packageName = packageName;}public int setProportion(int c){ return this.a=c;}public int getx(){ return a;}public int setliuliang(int d){ return this.b=d; }public String gety(){ return String.valueOf(b);} }
----------------------------------ReclyerView代码---------------------------------------
import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; import java.util.Collections; import java.util.List;public class RecyclerActivity extends AppCompatActivity {private List<PackageInfo> packageInfoList;private List<AppInfo> applicationInfoList;private RecyclerView mlistview;private RecyclerAdapter recyclerAdapter;private PackageManager pm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main3);mlistview = findViewById(R.id.mlistview);getAllAppNames();recyclerAdapter = new RecyclerAdapter(applicationInfoList);mlistview.setAdapter(recyclerAdapter);recyclerAdapter.notifyDataSetChanged();mlistview.setLayoutManager(new GridLayoutManager(this, 1));}public void getAllAppNames() {pm = getPackageManager();// 获取到所有安装了的应用程序的信息,包括那些卸载了的,但没有清除数据的应用程序packageInfoList=pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);applicationInfoList = new ArrayList<>();List<ApplicationInfo> list = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);Collections.sort(list, new ApplicationInfo.DisplayNameComparator(pm));// 排序applicationInfoList.clear();for (int i = 0; i < list.size(); i++) {//非系统程序int x=(int)(Math.random()*100);int y=(int)(Math.random()*100);if ((list.get(i).flags & ApplicationInfo.FLAG_SYSTEM) == 0) {applicationInfoList.add(getAppInfo(list.get(i),x,y));//如果非系统应用,则添加至appList} //系统程序}}private AppInfo getAppInfo(ApplicationInfo app,int a,int b) {AppInfo appInfo = new AppInfo();appInfo.setName(app.loadLabel(pm).toString());appInfo.setIcon(app.loadIcon(pm));appInfo.setPackageName(app.packageName);appInfo.setProportion(a);appInfo.setliuliang(b);return appInfo;}}
-----------------------------ReclyerAdapter代码-------------------------------------
import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List;public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHo> {private final List<AppInfo> list;public RecyclerAdapter(List<AppInfo> list) {this.list = list;}@NonNull@Overridepublic ViewHo onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.mlistview_layout, parent, false);return new ViewHo(inflate);}@Overridepublic void onBindViewHolder(@NonNull RecyclerAdapter.ViewHo holder, int position) {holder.img.setImageDrawable(list.get(position).getIcon());holder.tv_appname.setText(list.get(position).getName());holder.progressBar.setProgress(list.get(position).getx());holder.tv.setText(list.get(position).gety());}@Overridepublic int getItemCount() {return list.size();}public static class ViewHo extends RecyclerView.ViewHolder {ImageView img;TextView tv_appname;ProgressBar progressBar;TextView tv;public ViewHo(@NonNull View itemView) {super(itemView);img = itemView.findViewById(R.id.img);tv_appname = itemView.findViewById(R.id.tv_appname);progressBar=itemView.findViewById(R.id.progressBar);tv=itemView.findViewById(R.id.tv);}} }
-------------------------------------activity_main--------------------------------------
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></RelativeLayout>
-----------------------------activity_main3-------------------------------------------------------
<?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"tools:context=".MainActivity"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/mlistview"android:layout_width="match_parent"android:layout_height="wrap_content"/> </LinearLayout>
-------------------------------------mlistviw_layout---------------------------
<?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="wrap_content"android:orientation="horizontal"><ImageViewandroid:id="@+id/img"android:layout_width="80dp"android:background="#fff"android:layout_height="80dp" /><TextViewandroid:id="@+id/tv_appname"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:textColor="#000"android:layout_marginLeft="80dp"android:textSize="20dp" /><ProgressBarandroid:id="@+id/progressBar"android:layout_width="120dp"android:layout_height="20dp"style="?android:attr/progressBarStyleHorizontal"android:max="100"android:layout_marginLeft="80dp"android:layout_marginTop="30dp"android:progress="0"/><TextViewandroid:id="@+id/tv"android:layout_width="wrap_content"android:layout_height="30dp"android:textColor="#FFF79E38"android:text="@string/_222222"android:textSize="20sp"android:layout_marginLeft="200dp"android:layout_marginTop="30dp"/> </RelativeLayout>
------------------------------------manifest.xml-----------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.bitmap"><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.BitMap"><activity android:name=".RecyclerActivity"></activity><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>