通知数据更改后,物品的Recyclerview右边界正在切割

问题描述

我正在尝试使用回收视图来构建自定义选项卡功能

like this image

所以会有多个项目/类别可以水平滚动。 如果单击任何项​​目,则文本颜色和背景会根据活动/非活动项目而变化。所以这部分是在适配器中实用地完成的。

nonActiveDrawable = context?.getDrawable(R.drawable.rounded_corner_black)
activeDrawable = context?.getDrawable(R.drawable.rounded_corner_white)

onBindViewHolder函数中的以下代码中使用

if(position != mLastPosition){
   holder.categoryTab.background = nonActiveDrawable
   holder.txtview.setTextColor(ContextCompat.getColor(context,R.color.nonActiveColor))
} else {
   holder.categoryTab.background =  activeDrawable
   hholder.txtview.setTextColor(ContextCompat.getColor(context,R.color.activeColor))
}

当单击项目时,mLastPosition会更改并调用notifyDataSetChanged(),然后根据有效/无效项目重置项目的边框。但是项目显示如下图

enter image description here

我的项目布局xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_marginEnd="15dp"
    android:layout_width="wrap_content"
    android:background="@drawable/rounded_corner_white"
    android:id="@+id/categoryTab"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:gravity="center_horizontal|center_vertical"
        android:id="@+id/txtview"/>

    <LinearLayout
        android:layout_width="10dp"
        android:layout_height="wrap_content"/>

</LinearLayout>

在分配所有文本和背景后,我在onBindViewHolder上尝试了以下解决方案,但仍然无法使用。.

holder.txtview.measure(0,0);       
layoutParams.width = holder.txtview.measuredWidth + context?.convertDPToPixel(10)
layoutParams.marginEnd = context?.convertDPToPixel(15)
holder.categoryTab.layoutParams = layoutParams

我想要带有完整项目的边框背景,而不是最后一个项目。但是,如果我将此水平滚动到终点,边框将再次很好。但是当您单击项目并触发notifyDataSetChanged() 时不会。

适配器代码

class NewscategoriesAdapter(
    horizontallist: List<AppSettings.Category>,context: Context,private val theme: AppSettings.Theme,private val repoClickCallback: ((AppSettings.Category) -> Unit)?
): androidx.recyclerview.widget.RecyclerView.Adapter<NewscategoriesAdapter.MyViewHolder>() {
    var horizontallist : List<AppSettings.Category> = Collections.emptyList()

internal var context: Context
private var activeDrawable:Drawable? = null
private var nonActiveDrawable:Drawable? = null
private var mLastPosition:Int = 0

init{
    this.horizontallist = horizontallist
    this.context = context
    nonActiveDrawable = context?.getDrawable(R.drawable.rounded_corner_white)
    activeDrawable = context?.getDrawable(R.drawable.rounded_corner_black) 

}

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

inner class MyViewHolder(view: View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view) {
    internal var txtview: TextView = view.findViewById(R.id.txtview) as TextView
    internal var categoryTab: LinearLayout = view.findViewById(R.id.categoryTab) as LinearLayout
}
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): MyViewHolder {
    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.news_categories_slider,parent,false)
    return MyViewHolder(itemView)
}
override fun onBindViewHolder(holder: MyViewHolder,position: Int) {

    val layoutParams = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)

    if(position != mLastPosition){
        holder.categoryTab.background = nonActiveDrawable
         holder.txtview.setTextColor(Color.parseColor(theme.accentColor))
    } else {
        holder.categoryTab.background =  activeDrawable
        holder.txtview.setTextColor(Color.parseColor(theme.accentTextColor))
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        holder.txtview.text = Html.fromHtml(horizontallist[position].name,Html.FROM_HTML_MODE_COMPACT)
    } else {
        holder.txtview.text = Html.fromHtml(horizontallist[position].name)
    }

//        holder.txtview.measure(0,0);       //must call measure!
//        Timber.d("measuredWidth text "+ horizontallist[position].name + "--"+holder.txtview.measuredWidth.toString())
//        layoutParams.width = holder.txtview.measuredWidth + context?.convertDPToPixel(10)
//
//        layoutParams.marginEnd = context?.convertDPToPixel(15)
//        holder.categoryTab.layoutParams = layoutParams
    holder.categoryTab.setonClickListener {
        mLastPosition = position
        horizontallist[position].let {
            repoClickCallback?.invoke(it)
        }
        notifyDataSetChanged()
    }
}
}

我的Drawable文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="@color/white" />
    <stroke android:color="@color/colorAccent" android:width="5px"/>

    <corners android:radius="@dimen/corner_radius" />

</shape>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)