解决适配器checkbox勾选问题、上滑动会自动取消选择问题、获取手机已安装应用列表
引入依赖
1、implementation 'com.drakeet.multitype:multitype:4.2.0'
清单文件添加权限
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
2、activity代码
参数
//对应自己的集合
private val appBeanList: ArrayList<AppInfo?> = ArrayList()
//适配器
private val adapter = MultiTypeAdapter()
//recycler id = rv_apply
rv_apply.layoutManager = LinearLayoutManager(this)
adapter.register(DataModel::class)
.to(BrandModelItemDelegate1())
.withKotlinClassLinker { _, item ->
when (item.type) {
1 -> BrandModelItemDelegate1::class
else -> throw IllegalArgumentException("未知的类型,type=${item.type}")
}
}
val list = appBeanList
val dataModel = mutableListOf<DataModel>()
list.forEachIndexed { index, memoData ->
var label = memoData?.label
var checkApply = memoData?.checkApply
var packageName = memoData?.package_name
var icon = memoData?.icon
//赋值
var appInfo = AppInfo()
appInfo.label = label.toString()
appInfo.checkApply = checkApply == true
appInfo.package_name = packageName.toString()
appInfo.icon = icon
dataModel.add(DataModel(1, appInfo))
}
adapter.items = dataModel
rv_apply.adapter = adapter
adapter.notifyDataSetChanged()
rv_apply.scrollToPosition(0)
一个新的方法在activity最外层
data class DataModel(val type: Int, val data: Any)
//放在DataModel下面即可
class BrandModelItemDelegate1 : ViewDelegate<DataModel, HomeItemView>() {
override fun onCreateView(context: Context): HomeItemView {
return HomeItemView(context).apply {
layoutParams =
FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
}
override fun onBindView(view: HomeItemView, item: DataModel) {
view.setData(item.data as AppInfo)
}
}
//fragment页面
class HomeItemView : FrameLayout {
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
initView()
}
private fun initView() {
inflate(context, R.layout.item_app_info, this)
}
fun setData(apply: AppInfo) {
tv_name.text = apply.label
iv_img.setImageDrawable(apply.icon)
//解决适配器勾选轮换问题
cb_apply.isChecked = apply.checkApply
cb_apply.setOnClickListener {
apply.checkApply = !apply.checkApply
}
}
}
对应自己的xml展示的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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="94dp">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_img"
android:layout_width="59dp"
android:layout_height="59dp"
android:layout_marginStart="20dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:text="android"
android:textColor="#000"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/iv_img"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/cb_apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="27dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
//实体类根据自己的需求
class AppInfo {
var uid = 0
var checkApply = false
var label //应用名称
= ""
var package_name //应用包名
= ""
var icon //应用icon
: Drawable? = null
}
/**
* 获取手机已安装应用列表
* @param ctx
* @param isFilterSystem 是否过滤系统应用
* @return
*/
private fun getAllAppInfo(ctx: Context, isFilterSystem: Boolean): ArrayList<AppInfo?>? {
var bean: AppInfo? = null
val packageManager: PackageManager = ctx.getPackageManager()
val list = packageManager.getInstalledPackages(0)
for (p in list) {
bean = AppInfo()
bean.icon = p.applicationInfo.loadIcon(packageManager)
bean.label = packageManager.getApplicationLabel(p.applicationInfo).toString()
bean.package_name = p.applicationInfo.packageName
val flags = p.applicationInfo.flags
// 判断是否是属于系统的apk
if (flags and ApplicationInfo.FLAG_SYSTEM != 0 && isFilterSystem) {
// bean.setSystem(true);
} else {
appBeanList.add(bean)
}
}
return appBeanList
}
activity代码
<?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=".activity.ApplicationModeMainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_apply"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="90dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_apply_check_finish"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="17dp"
android:paddingEnd="17dp"
android:src="@drawable/model_back_v1" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="68dp"
android:layout_weight="5"
android:gravity="center"
android:paddingStart="30dp"
android:text="@string/mode_selection_detail_page_text"
android:textColor="@color/black"
android:textSize="17sp"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/iv_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/apply_ok_bg"
android:gravity="center"
android:paddingStart="17dp"
android:layout_marginEnd="10dp"
android:paddingEnd="17dp"
android:src="@drawable/model_back_v1"
android:text="@string/about_ok_check_text"
android:textColor="@color/white" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>