Как получить FragmentManager для DialogFragment в Kotlin с помощью Androidx?

Я следую руководству, в котором инструктор использует библиотеку поддержки Android v7. В своем приложении я использую версию androidx (как это предлагается на веб-сайте разработчика Android). Когда я набираю строки кода в соответствии с инструкциями, Android Studio зачеркивает часть кода, в которой я пытаюсь получить FragmentManager, и говорит: «Получатель для fragmentManager: FragmentManager! Устарел. Не рекомендуется в Java». Я просмотрел так много сообщений, в которых у людей были похожие проблемы, но предложенные решения не применимы к моему случаю. У некоторых пользователей были несоответствующие версии библиотеки поддержки, а у других не было соответствующих зависимостей в файле gradle. Насколько я могу судить, эти проблемы здесь не применяются.

Согласно документации androidx для FragmentManager, в нем говорится: «Чтобы использовать это, ваше действие должно быть производным от FragmentActivity. Из такого действия вы можете получить FragmentManager, вызвав FragmentActivity # getSupportFragmentManager». Однако я не использую Activity, код находится внутри внутреннего класса, расширяющего класс 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
      }
    }
  }
}

DialogFragment класс:

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment

class AmountFragment : DialogFragment() {
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                            savedInstanceState: Bundle? ): View? {
    return inflater.inflate(R.layout.fragment_amount, container, false)
  }
}

build.gradle (Модуль: приложение)

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'
}

person gig6    schedule 19.04.2020    source источник


Ответы (1)


Согласно документации androidx для FragmentManager, в нем говорится: «Чтобы использовать это, ваше действие должно быть производным от FragmentActivity. Из такого действия вы можете получить FragmentManager, вызвав FragmentActivity # getSupportFragmentManager». Однако я не использую Activity

Да, вы:

var fragmentManager = (itemView.context as Activity).fragmentManager

Единственная проблема заключается в том, что вы выполняете приведение к корневому типу Activity и используете устаревший getFragmentManager (). Вам нужно сделать именно то, что указано в документации: используйте FragmentActivity и getSupportFragmentManager():

var fragmentManager = (itemView.context as FragmentActivity).supportFragmentManager

Тогда ваш show() вызов будет работать, потому что вы передаете правильный тип диспетчера фрагментов.

Надеюсь, это поможет!

person dominicoder    schedule 19.04.2020