首页 > 解决方案 > 仅针对整个应用程序的 recyclerview 项目中的 textviews 字体不会更改

问题描述

我的问题是所有recyclerview的任何项目中的任何文本视图都不会从XML中获取其字体,只显示android的原始字体

只有所有回收站视图的项目有这个问题,其他文本视图工作正常

我在 23 rv 附近有很多回收站视图,而且还有更多,所以以编程方式更改每个文本字体并不是一件好事我尝试了很多事情但没有运气在这里有任何帮助吗?

这是一个例子:适配器视图

import android.content.Context
import android.util.Log

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.ecom.R
import com.ecom.data.model.FabricProduct
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.item_fabric_product.view.*


class CollectionAdaptor(var mList: List<FabricProduct>, var context: Context, val listener: (FabricProduct) -> Unit
) : RecyclerView.Adapter<CollectionViewHolder>() {

    fun updateList(newitems: List<FabricProduct>) {
        mList = newitems
        notifyDataSetChanged()
   }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollectionViewHolder {return CollectionViewHolder( LayoutInflater.from(context).inflate(R.layout.item_fabric_product,parent,false))
}

    override fun onBindViewHolder(holder: CollectionViewHolder, position: Int) {
        holder.bind(mList[position], listener)

    }

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

class CollectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bind(item: FabricProduct, listener: (FabricProduct) -> Unit) = with(itemView) {
        itemView.text_subcategorie_slogan.text = item.slogan
        itemView.text_subcategorie_title.text = item.title
        setOnClickListener { listener(item) }
    }}

回收站查看设置

mFabricProductlist = FakeData.getFabricCollection()

            rootView.rv_collection_subcategory.layoutManager = GridLayoutManager(activity!!.applicationContext, 2)

            mCollectionAdaptor =
                CollectionAdaptor(mFabricProductlist as List<FabricProduct>, activity!!.applicationContext) { //get the selected item here}

     rootView.rv_collection_subcategory.adapter = mCollectionAdaptor

在 fragment_collection.xml 中

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_collection_subcategory"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
    />

项目.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="220dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="15dp"
    android:layout_marginBottom="4dp"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="10dp"
        android:layout_marginStart="4dp"
        android:orientation="vertical">


        <TextView
            android:id="@+id/text_subcategorie_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/montserrat_bold"
            android:shadowColor="#000"
            android:shadowDy="1"
            android:shadowRadius="1"
            android:text="000"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/text_subcategorie_slogan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/montserrat"
            android:shadowColor="#000"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:text="000"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="12sp" />


    </LinearLayout>

</FrameLayout>

构建.gradle

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0-alpha03'
    androidTestImplementation 'androidx.test.espresso:espresso- 
    core:3.2.0-alpha03'
    //Navigation
    implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

标签: androidandroid-recyclerviewfontsandroidx

解决方案


现在可以了,问题是

mCollectionAdaptor =
                CollectionAdaptor(mFabricProductlist as List<FabricProduct>, activity!!.applicationContext) { //get the selected item here}

我通过了活动的 applicationContext 所以解决方案只是通过了活动

 mCollectionAdaptor =
                    CollectionAdaptor(mFabricProductlist as List<FabricProduct>, activity!!) { //get the selected item here}

谢谢


推荐阅读