突出显示recyclerview的选定项目
highlight the selected item of recyclerview
请不要将其标记为重复我指的是
出现异常:
kotlin.KotlinNullPointerException at package.name.ProductDetails.Custom_detail_Adapter.onBindViewHolder(Custom_detail_Adapter.kt:57)
这是行:
holder.relativeitem.setSelected(selectedItems?.get(position, false)!!);
而且我也想知道如何在加载时 setSelected
位于第一个位置 activity。 (如何预填充SparseBooleanArray
?)
这是我的适配器:
class Custom_detail_Adapter(context: Context, dataList: List<Product_images_response>) :
RecyclerView.Adapter<Custom_detail_Adapter.CustomViewHolder>() {
private val dataList: List<Product_images_response>
private val context: Context
private val selectedItems: SparseBooleanArray?=null
inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),View.OnClickListener {
val mView: View
val avatar: ImageView
val relativeitem:RelativeLayout
init {
mView = itemView
avatar = mView.findViewById(R.id.view3)
relativeitem=mView.findViewById(R.id.relativeitem)
}
override fun onClick(v: View?) {
if (selectedItems?.get(getAdapterPosition(), false)!!) {
selectedItems?.delete(getAdapterPosition());
relativeitem.setSelected(false);
}
else {
selectedItems?.put(getAdapterPosition(), true);
relativeitem.setSelected(true);
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view: View = layoutInflater.inflate(R.layout.product_detail_item, parent, false)
return CustomViewHolder(view)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
holder.relativeitem.setSelected(selectedItems?.get(position, false)!!);
Glide.with(context).load(dataList[position].image)
.thumbnail(0.5f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.avatar)
holder.avatar.setOnClickListener(View.OnClickListener {
val intent = Intent("custom-message")
intent.putExtra("item", dataList.get(position).image)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
})
}
override fun getItemCount(): Int {
return dataList.size
//return (dataList == null) ? 0 : dataList.size();
}
init {
this.context = context
this.dataList = dataList
}
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/relativeitem"
android:background="@drawable/activated_color_selector"
android:layout_height="wrap_content">
<ImageView
android:layout_width="235px"
android:layout_height="207px"
android:id="@+id/view3"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
/>
</RelativeLayout>
需要帮助!谢谢。
首先,给selectedItems
变量一个初始值。也没有必要让它成为可选的,从而消除了用问号打开可选值的需要(这个 selectedItems?
变成了这个 selectedItems
):
private val selectedItems = SparseBooleanArray()
如果要在加载适配器之前设置选定位置,请将此变量设为构造函数参数之一。使用此主要构造函数,您可以删除 init
块:
class Custom_detail_Adapter(private val context: Context, private val dataList: List<Product_images_response>, private val selectedItems: SparseBooleanArray) :
RecyclerView.Adapter<Custom_detail_Adapter.CustomViewHolder>() {
inner class CustomViewHolder ...
此 SparseBooleanArray
构造函数参数可以预先填充指示选择了哪些项目的值。
请不要将其标记为重复我指的是
出现异常:
kotlin.KotlinNullPointerException at package.name.ProductDetails.Custom_detail_Adapter.onBindViewHolder(Custom_detail_Adapter.kt:57)
这是行:
holder.relativeitem.setSelected(selectedItems?.get(position, false)!!);
而且我也想知道如何在加载时 setSelected
位于第一个位置 activity。 (如何预填充SparseBooleanArray
?)
这是我的适配器:
class Custom_detail_Adapter(context: Context, dataList: List<Product_images_response>) :
RecyclerView.Adapter<Custom_detail_Adapter.CustomViewHolder>() {
private val dataList: List<Product_images_response>
private val context: Context
private val selectedItems: SparseBooleanArray?=null
inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),View.OnClickListener {
val mView: View
val avatar: ImageView
val relativeitem:RelativeLayout
init {
mView = itemView
avatar = mView.findViewById(R.id.view3)
relativeitem=mView.findViewById(R.id.relativeitem)
}
override fun onClick(v: View?) {
if (selectedItems?.get(getAdapterPosition(), false)!!) {
selectedItems?.delete(getAdapterPosition());
relativeitem.setSelected(false);
}
else {
selectedItems?.put(getAdapterPosition(), true);
relativeitem.setSelected(true);
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view: View = layoutInflater.inflate(R.layout.product_detail_item, parent, false)
return CustomViewHolder(view)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
holder.relativeitem.setSelected(selectedItems?.get(position, false)!!);
Glide.with(context).load(dataList[position].image)
.thumbnail(0.5f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.avatar)
holder.avatar.setOnClickListener(View.OnClickListener {
val intent = Intent("custom-message")
intent.putExtra("item", dataList.get(position).image)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
})
}
override fun getItemCount(): Int {
return dataList.size
//return (dataList == null) ? 0 : dataList.size();
}
init {
this.context = context
this.dataList = dataList
}
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/relativeitem"
android:background="@drawable/activated_color_selector"
android:layout_height="wrap_content">
<ImageView
android:layout_width="235px"
android:layout_height="207px"
android:id="@+id/view3"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
/>
</RelativeLayout>
需要帮助!谢谢。
首先,给selectedItems
变量一个初始值。也没有必要让它成为可选的,从而消除了用问号打开可选值的需要(这个 selectedItems?
变成了这个 selectedItems
):
private val selectedItems = SparseBooleanArray()
如果要在加载适配器之前设置选定位置,请将此变量设为构造函数参数之一。使用此主要构造函数,您可以删除 init
块:
class Custom_detail_Adapter(private val context: Context, private val dataList: List<Product_images_response>, private val selectedItems: SparseBooleanArray) :
RecyclerView.Adapter<Custom_detail_Adapter.CustomViewHolder>() {
inner class CustomViewHolder ...
此 SparseBooleanArray
构造函数参数可以预先填充指示选择了哪些项目的值。