如何使用Androidx为Kotlin中的DialogFragment获取碎片管理器?

如何使用Androidx为Kotlin中的DialogFragment获取碎片管理器?,kotlin,android-recyclerview,androidx,fragmentmanager,dialogfragment,Kotlin,Android Recyclerview,Androidx,Fragmentmanager,Dialogfragment,我正在学习一个教程,其中讲师正在使用Android支持库v7。在我的应用程序中,我使用的是androidx版本(正如Android开发者网站上建议的那样)。当我按照指示键入代码行时,Android Studio在我试图获取FragmentManager的部分代码上打了一个删除线,并说:“FragmentManager的getter:FragmentManager!已弃用。在Java中已弃用。”我已经搜索了很多人有类似问题的帖子,但提供的解决方案不适用于我的案例。一些用户的支持库版本不匹配,而其他

我正在学习一个教程,其中讲师正在使用Android支持库v7。在我的应用程序中,我使用的是androidx版本(正如Android开发者网站上建议的那样)。当我按照指示键入代码行时,Android Studio在我试图获取FragmentManager的部分代码上打了一个删除线,并说:“FragmentManager的getter:FragmentManager!已弃用。在Java中已弃用。”我已经搜索了很多人有类似问题的帖子,但提供的解决方案不适用于我的案例。一些用户的支持库版本不匹配,而其他用户在gradle文件中没有适当的依赖项。据我所知,这些问题在这里并不适用

根据FragmentManager的androidx文档,它指出,“您的活动必须派生自FragmentActivity才能使用此功能。从此类活动中,您可以通过调用FragmentActivity#getSupportFragmentManager来获取FragmentManager。”但是,我没有使用活动,代码位于扩展RecylcerView.ViewHolder类的内部类中,该类嵌套在扩展RecyclerView.Adapter的类中。是我使用android支持库v7的唯一选择吗

RecyclerView适配器类:

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.e_product_row.view.*

import androidx.fragment.app.DialogFragment  // greyed out as "unused import directive"
import androidx.fragment.app.FragmentManager // greyed out as "unused import directive"


class EProductAdapter(var context: Context, var arrayList : ArrayList<EProduct>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    val productView = LayoutInflater.from(context).inflate(R.layout.e_product_row, parent, false)
    return ProductViewHolder(productView)
  }

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

  override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    // downcast holder to ProductViewHolder
    (holder as ProductViewHolder).initializeRowComponents(arrayList.get(position).id,
                                                          arrayList[position].name,
                                                          arrayList[position].price,
                                                          arrayList[position].picture)
  }

  inner class ProductViewHolder(productView: View) : RecyclerView.ViewHolder(productView) {
    fun initializeRowComponents(id: Int, name: String, price: Int, pictureName: String) {
      itemView.txt_id.text = id.toString()
      itemView.txt_name.text = name
      itemView.txt_price.text = price.toString()
      var pictureUrl = "http://192.168.0.21/OnlineStoreApp/osimages/$pictureName"
      pictureUrl = pictureUrl.replace(" ", "%20")
      Picasso.get().load(pictureUrl).into(itemView.img_product)

      // initialize add item imageView
      itemView.img_add_item.setOnClickListener {
        var amountFragment = AmountFragment()
        var fragmentManager = (itemView.context as Activity).fragmentManager // fragmentManager strikethrough text
         amountFragment.show(fragmentManager, "TAG") // show function cannot be called with arguments supplied and won't compile
      }
    }
  }
}
build.gradle(模块:应用程序)

根据FragmentManager的androidx文档,它 声明:“您的活动必须派生自FragmentActivity才能使用此功能。 从这样的活动中,您可以通过调用 FragmentActivity#getSupportFragmentManager。“但是,我没有使用 活动

是的,你是:

var fragmentManager = (itemView.context as Activity).fragmentManager
唯一的问题是,您正在强制转换到根
活动
类型并使用不推荐的。您需要完全按照文档中的说明执行:使用
FragmentActivity
getSupportFragmentManager()

然后,您的
show()
调用将起作用,因为您正在传入正确的片段管理器类型

希望有帮助

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.bryontaylor.onlinestoreapp"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.volley:volley:1.1.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.fragment:fragment:1.2.4'
    implementation 'androidx.fragment:fragment-ktx:1.2.4'
}
var fragmentManager = (itemView.context as Activity).fragmentManager
var fragmentManager = (itemView.context as FragmentActivity).supportFragmentManager