字体不会针对recyclerview项目内的textview更改,仅适用于整个应用

时间:2019-05-31 05:02:19

标签: android android-recyclerview fonts androidx

我的问题是,所有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) }
    }}

recyclerview设置

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"
    />

item.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>

build.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'

2 个答案:

答案 0 :(得分:0)

它现在正在工作,问题是

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}

谢谢

答案 1 :(得分:0)

使用 AppCompatTextView 而不是 TextView

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


        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/text_subcategorie_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app: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" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/text_subcategorie_slogan"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app: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>