Android加载APP列表并显示

Activity中定义个RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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.RecyclerView
        android:id="@+id/rvApps"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.lmm.launcherdemo

import android.content.Intent
import android.content.pm.ResolveInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import androidx.recyclerview.widget.GridLayoutManager
import com.lmm.launcherdemo.adapter.AppAdapter
import com.lmm.launcherdemo.databinding.ActivityMainBinding
import com.lmm.launcherdemo.entity.AppInfo

class MainActivity : AppCompatActivity() {

    private lateinit var mBinding:ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMainBinding.inflate(layoutInflater)
        val root = mBinding.root
        setContentView(root)
        initView()
    }

    private fun initView() {
        val apps = appsInfo()
        val adapter = AppAdapter(apps)
        mBinding.rvApps.layoutManager = GridLayoutManager(this,3)
        mBinding.rvApps.adapter = adapter
    }

    private fun appsInfo():List<AppInfo> {
        val list = ArrayList<AppInfo>()
        val apps = scanApps()
        for (app in apps) {
            val item = AppInfo()
            item.appName = app.loadLabel(packageManager).toString()
            item.icon = app.activityInfo.loadIcon(packageManager)
            list.add(item)
        }
        return list
    }

    private fun scanApps():List<ResolveInfo> {
        val intent = Intent(Intent.ACTION_MAIN,null).apply {
            addCategory(Intent.CATEGORY_LAUNCHER)
        }
        return packageManager.queryIntentActivities(intent,0)
    }
}

APP的Item

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="10dp">

    <ImageView
        android:id="@+id/ivIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="36dp"
        android:maxHeight="36dp"
        app:layout_constraintBottom_toTopOf="@id/tvName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:singleLine="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/ivIcon"
        tools:text="@string/app_name" />

</androidx.constraintlayout.widget.ConstraintLayout>

Adapter

package com.lmm.launcherdemo.adapter

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lmm.launcherdemo.databinding.AppItemLayoutBinding
import com.lmm.launcherdemo.entity.AppInfo
import kotlinx.android.synthetic.main.app_item_layout.view.*

class AppAdapter(private val apps:List<AppInfo>) : RecyclerView.Adapter<AppAdapter.AppHolder>() {

    private lateinit var mContext:Context

    class AppHolder(itemView:AppItemLayoutBinding) : RecyclerView.ViewHolder(itemView.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppHolder {
        mContext = parent.context
        val item = AppItemLayoutBinding.inflate(LayoutInflater.from(parent.context))
        return AppHolder(item)
    }

    override fun getItemCount(): Int {
        return apps.size
    }

    override fun onBindViewHolder(holder: AppHolder, position: Int) {
        val app = apps[position]
        holder.itemView.ivIcon.setImageDrawable(app.icon)
        holder.itemView.tvName.text = app.appName
        holder.itemView.setOnClickListener {
            val intent = Intent().apply {
                component = ComponentName(app.packageName,app.name)
            }
            mContext.startActivity(intent)
        }
    }
}
package com.lmm.launcherdemo.entity

import android.graphics.drawable.Drawable

class AppInfo {
    var appName:String? = null
    var icon:Drawable? = null
    var packageName:String = ""
    var name:String = ""
}
0 0 vote
Article Rating
Subscribe
提醒
guest
0 评论
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x