[Kotlin][ReSub] 1. RecyclerView+Adapter, ResolveInfo

tornbolo·2020년 11월 18일
0

1. 준비

Build:Gradle (Module:app)
dependency에 다음 추가, Sync now

    // Recycler View
    implementation 'com.android.support:recyclerview-v7:28.0.0'



2. 결과



3. 코드

1) activity_test.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="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".view.TestActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/app_total"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:text="총 X개"
        android:textSize="20sp"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/app_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

</LinearLayout>

2) TestActivity.kt

import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.*****.resub.R
import kotlinx.android.synthetic.main.activity_test.*

class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        val pakageManager : PackageManager = packageManager
        val intent = Intent(Intent.ACTION_MAIN,null)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)

        val resloveInfos : List<ResolveInfo> = pakageManager.queryIntentActivities(intent,0)

        app_total.text = "총 ${resloveInfos.size}개"
        val mainAdapter = TestAdapter(packageManager,this,resloveInfos)
        app_list.adapter = mainAdapter


    }
}

3) TestAdapter.kt

import android.content.Context
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.****.resub.R
import kotlinx.android.synthetic.main.recycler_list_item_app.view.*

class TestAdapter(private val packageManager: PackageManager, private val context : Context,
                  private var data:List<ResolveInfo>) : RecyclerView.Adapter<TestAdapter.ViewHolder>() {
    init{
        sortData()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.recycler_list_item_app,parent,false)
        return ViewHolder(view)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val icon = data[position].activityInfo.loadIcon(packageManager)
        holder.app_img.setImageDrawable(icon)
        holder.app_name.text = data[position].loadLabel(packageManager)
        holder.app_info.text = data[position].activityInfo.packageName
    }

    private fun sortData(){
        // 나중에.
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val app_name : TextView = itemView.item_app_name
        val app_info : TextView = itemView.item_app_info
        val app_img : ImageView = itemView.item_app_img
    }

}




4. 중요

5. 참고

http://blog.daum.net/andro_java/979
https://itmir.tistory.com/442
https://itmir.tistory.com/382

profile
develog

0개의 댓글