Android 通知数据更改后,正在剪切项的Recyclerview右边框

Android 通知数据更改后,正在剪切项的Recyclerview右边框,android,android-recyclerview,tabs,adapter,Android,Android Recyclerview,Tabs,Adapter,我正在尝试使用循环视图构建自定义选项卡功能 因此,将有多个项目/类别水平滚动。 如果您单击任何项目,文本颜色和背景将根据活动/非活动项目而变化。这一部分实际上是在adapter中完成的 nonActiveDrawable = context?.getDrawable(R.drawable.rounded_corner_black) activeDrawable = context?.getDrawable(R.drawable.rounded_corner_white) 在onBindView

我正在尝试使用循环视图构建自定义选项卡功能

因此,将有多个项目/类别水平滚动。 如果您单击任何项目,文本颜色和背景将根据活动/非活动项目而变化。这一部分实际上是在adapter中完成的

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()
,然后根据活动/非活动项目重置项目边界。但项目显示如下图所示

我的项目布局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>
我要的是完整的边框背景,而不是最后一条。但如果我将这个水平滚动到最后,边界将再次变好。但当您单击项目并启动
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()
    }
}
}
类新闻分类适配器(
水平列表:,
上下文:上下文,
私有val主题:AppSettings.theme,
private val repoClickCallback:((AppSettings.Category)->单位)?
):androidx.recyclerview.widget.recyclerview.Adapter(){
var horizontalList:List=Collections.emptyList()
内部变量上下文:上下文
私有变量activeDrawable:Drawable?=null
私有变量NonActivateDrawable:可提取?=null
私有变量mLastPosition:Int=0
初始化{
this.horizontalList=horizontalList
this.context=context
NonActivateDrawable=上下文?.getDrawable(R.drawable.rounded\u corner\u white)
activeDrawable=context?.getDrawable(R.drawable.rounded\u corner\u black)
}
重写getItemCount():Int{
返回水平列表.size
}
内部类MyViewHolder(视图:视图):androidx.recyclerview.widget.recyclerview.ViewHolder(视图){
内部变量txtview:TextView=view.findViewById(R.id.txtview)作为TextView
内部var类别选项卡:LinearLayout=view.findViewById(R.id.categoryTab)作为LinearLayout
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):MyViewHolder{
val itemView=LayoutInflater.from(parent.context).充气(R.layout.news\u categories\u滑块,parent,false)
返回MyViewHolder(项目视图)
}
覆盖onBindViewHolder(holder:MyViewHolder,位置:Int){
val layoutParams=LinearLayout.layoutParams(
LinearLayout.LayoutParams.WRAP_内容,
LinearLayout.LayoutParams.WRAP_内容)
如果(位置!=mLastPosition){
holder.categoryTab.background=不可激活
holder.txtview.setTextColor(Color.parseColor(theme.accentColor))
}否则{
holder.categoryTab.background=activeDrawable
holder.txtview.setTextColor(Color.parseColor(theme.accentTextColor))
}
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.N){
holder.txtview.text=Html.fromHtml(水平列表[position].name,Html.FROM\u Html\u MODE\u COMPACT)
}否则{
holder.txtview.text=Html.fromHtml(水平列表[position].name)
}
//holder.txtview.measure(0,0);//必须调用measure!
//Timber.d(“measuredWidth文本”+水平列表[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=位置
水平列表[位置]。让{
repoClickCallback?调用(it)
}
notifyDataSetChanged()
}
}
}
我的可绘图文件是

<?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>


在您的
onCreateViewHolder
onBindViewHolder
中共享完整的代码。我还想看看可绘制的资源。这些是9块补丁还是形状资源还是什么?我更新了我的问题,提供了更多信息
<?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>