如何在Groupie Item()中加载片段?

时间:2019-09-03 20:22:16

标签: kotlin

我正在尝试将片段加载到Groupie Item()中,并且崩溃时 java.lang.IllegalStateException:活动已被破坏

我用于加载片段的代码在常规活动中有效,但在Groupie库中Item()对象的bind()方法中无效。我以为是因为该容器需要引用,所以我进行了一些测试,然后在bind()方法中可以访问该容器

import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity
import com.xwray.groupie.kotlinandroidextensions.Item
import com.xwray.groupie.kotlinandroidextensions.ViewHolder
import kotlinx.android.synthetic.main.activity_set_frequency.frequency_options
import kotlinx.android.synthetic.main.rx_section_frequency.*


class RxSectionFrequency: Item() {


    companion object comp : AppCompatActivity(){


    }

    lateinit var radioGroup : RadioGroup
    lateinit var radioButton: RadioButton

    override fun bind(viewHolder: ViewHolder, position: Int) {

        fun checkBtn(){
            val frequency_fragment = FrgFrequencyInterval()

            var radioId = radioGroup.checkedRadioButtonId
            radioButton = radioGroup.findViewById(radioId)

            var radio_name = radioButton.text

            var transaction = supportFragmentManager.beginTransaction().add(R.id.fragment_holder,frequency_fragment).commit()
            var removeFragment = supportFragmentManager.beginTransaction().remove(frequency_fragment)


        if(radio_name=="Interval"){ try {transaction} catch (e: Exception){
            println(e)}}

        }

        radioGroup = viewHolder.frequency_options
        radioGroup.setOnCheckedChangeListener{ radioGroup, i -> checkBtn() }

    }

    override fun getLayout()=R.layout.rx_section_frequency
}```


On the radioGroup selection I expect to load a fragment called FrgFrequencyInterval() into a FrameLayout called fragment_holder within rx_section_frequency layout. As the code is throws the following exception and no further explanation as what is causing the error:
java.lang.IllegalStateException: Activity has been destroyed

1 个答案:

答案 0 :(得分:0)

我收到了Groupie的Valentin Hinov的回复,他提到不应将片段用作RecyclerView或LIst视图,这些片段当然是Groupie库的一部分,而是建议创建自定义视图并使用适配器getViewTypeCount和getView。