【问题标题】:How pass data from parent fragment to child kotlin如何将数据从父片段传递给子 kotlin
【发布时间】:2021-01-17 14:53:30
【问题描述】:

我有父片段,我在其中执行 http 请求,但 wordpress api 只返回一页,所以我有子片段,它有按钮,可以前后移动页面。在父片段中我加载子片段,它要求父片段加载url。

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var view =  inflater.inflate(R.layout.fragment_itemnews_list, container, false)
        childFragmentManager.beginTransaction().replace(R.id.pagecontainer,fragment,"page").commit()
//initialization of views
        childFragmentManager.setFragmentResultListener("", this,{ key,b ->
            run(url,b)
        })
return view
    }

预期结果

我希望,我的父片段添加页面片段和页面片段将询问父片段加载 url 的第一页。但这不会发生。

在run(url)函数中我做http请求并在主线程中为子片段设置结果,通知它,成功加载了什么数据,我们可以移动页面。

private fun run(url: String,b:Bundle) {
//do http request via okhttpclient,and if all ok,do:
activity!!.runOnUiThread {
//do something with ui elements,and notify child fragment,what all ok.
                                childFragmentManager.setFragmentResult("", b)
}
}

在我的子片段 PageFragment 中,我向父片段发送请求,执行 http 请求。

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view:View =inflater.inflate(R.layout.fragment_page, container, false)
...
//to move on previous or on next page
        mPrev.setOnClickListener {
pressPrevOrNextButton(-1)
        }
        mNext.setOnClickListener {
pressPrevOrNextButton(1)
        }
        parentFragmentManager.setFragmentResultListener("a",this, { _,b ->
movePage(b.getInt("direction",0))
        }
return view
}

movePage 方法只应用结果,如果数据通过父片段加载成功。现在您将看到向父片段发送请求的函数,它对特定页面执行 http 请求。

fun pressPrevOrNextButton(direction: Int) {
    mPrev.isVisible=false
    mNext.isVisible=false
parentFragmentManager.setFragmentResult("a",bundleOf(Pair("page",page), Pair("per_page",per_page),Pair("direction",direction)))
}

现在您将看到代码,我如何在活动中调用父片段。

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_item_list)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        val name =intent.getStringExtra("name")
                if(name!=null &&name.length>0) supportActionBar?.title = getString(if(intent.getStringExtra("type").equals("tag")) R.string.title_tags else R.string.title_all) + ": " + name
//adding the parent fragment
        fragment= FragmentNews(intent.getStringExtra("url")!!,"desc","date",intent.getBooleanExtra("search",false))
       supportFragmentManager.beginTransaction().replace(R.id.mycontainer,fragment,"posts").commit()
    }

在 gradle 中我有:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.squareup.okhttp3:okhttp:3.12.12'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.21"
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.fragment:fragment-ktx:1.3.0-rc01'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

在我拥有所有这些逻辑之前,包括在一个活动中的页面之间移动,一切正常,但我想在几个地方实现页面移动,所以为了避免在几个地方重复几乎相同的代码,我决定移动它在单独的片段中。帮我解决问题,为什么我的父片段没有从侦听器获取事件?无论是 FragmentResult api 的问题,我应该立即向谷歌报告吗?如果是,我该怎么做,即我应该使用什么链接来报告这个问题?非常感谢大家的帮助。

【问题讨论】:

标签: android kotlin fragment listener bundle


【解决方案1】:

很难理解你的问题。不过我还是试着回答一下,希望对你有帮助。 您可以使用导航组件并将数据从子片段传递到父片段: https://developer.android.com/guide/navigation/navigation-pass-data#start

另一种解决方案,查看此文档以了解子片段和父片段之间传递数据的方式。 https://developer.android.com/guide/fragments/communicate#pass-parent-child

【讨论】:

  • 但是在你的片段结果 api 链接中,它可能是示例代码中的一些错字,因为当我尝试执行 parentFragmentManager.setFragmentResultListener("a") { _,b -> movePage(b.getInt ("direction",0)) },我得到编译错误:没有为参数 'p1' 传递值。或者是最新的kotlin问题,我应该立即向google报告。
  • 好的,我会再次尝试解释我的问题。我有父子片段。子片段向父片段发送数据。结果成功后,父fragment向子fragment发送数据,通知,父fragment可以做一些工作。我注意到,当我将数据发送到父片段时,父片段没有收到它,但在子片段监听器中工作。为什么父片段不接收数据,我不明白。我做了一个例子,它证明了这个问题。你可以在drive.google.com/file/d/1xjdt-RD2CRMu3WUmnazp0osUPNjJrBIx/…看到它。
  • 我向谷歌报告了这个问题,谷歌在issuetracker.google.com/issues/177734529显示了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 2018-06-24
  • 2021-08-23
  • 2020-08-06
  • 2017-08-03
相关资源
最近更新 更多