带有FrameLayout

时间:2019-05-06 17:06:55

标签: java android android-fragments kotlin android-framelayout

我正在创建一个DialogFragment,但在该Fragment中有一个 FrameLayout ,它将在各个片段之间动态更改。

为了更好地控制所有片段,我创建了一个实例类来处理名为 NavigationDelegate

的片段。
class NavigationDelegate(private val fragmentManager: FragmentManager, private val containerId: Int) {
    init {
        fragmentManager.addOnBackStackChangedListener {
            val currentFragment = fragmentManager.findFragmentById(containerId)
            currentFragment?.userVisibleHint = true
        }
    }

    fun navigateTo(to: Fragment, addToBackStack: Boolean = true){
        val transaction = fragmentManager.beginTransaction().replace(containerId, to)
        if(addToBackStack) transaction.addToBackStack(null)
        transaction.commit()
    }

    fun navigate(from: Fragment? = null, to: Fragment, addToBackStack: Boolean = true) {
        from?.let {
            from.userVisibleHint = false
            val transaction = fragmentManager.beginTransaction().replace(containerId, to)
            if(addToBackStack) transaction.addToBackStack(null)
            transaction.commit()
        }.let {
            val transaction = fragmentManager.beginTransaction().add(containerId, to)
            if(addToBackStack) transaction.addToBackStack(null)
            transaction.commit()
        }
    }
}

这是我称为要创建的DialogFragment的部分。

val purchase = inputFragmentView!!.findViewById<MaterialButton>(R.id.purchaseButton)
        purchase.setOnClickListener {
            val paymentSteps: PaymentStepsFragment?
            paymentSteps = PaymentStepsFragment()
                paymentSteps.isCancelable = false
                if (!paymentSteps.isVisible) paymentSteps.show(fragmentManager, "paymentSteps")
        }

这是 PaymentStepsFragment

class PaymentStepsFragment : DialogFragment() {

    private var viewer: View? = null
    lateinit var fragmentContainer: FrameLayout
    lateinit var navigationDelegate: NavigationDelegate

    override fun getTheme(): Int {
        return R.style.SlideUpDownDialog
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        when {
            viewer != null -> (viewer!!.parent as ViewGroup).removeView(viewer)
        }
        viewer = inflater.inflate(R.layout.fragment_steps_payment, container, false)

        dialog?.window?.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        return viewer
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val imageStep = view.imageSteps
        imageStep.setSteps(R.drawable.ic_wallet_step_1, R.drawable.ic_deliver_step_2, R.drawable.ic_review_step_3)
        imageStep.scaleUp = 2.0f
        imageStep.animationDuration = 500
        view.next.setOnClickListener {
            imageStep.next()
        }
        view.back.setOnClickListener {
            imageStep.previous()
        }
        fragmentContainer = view.framePayment
        navigationDelegate = NavigationDelegate(fragmentManager!!, fragmentContainer.id)
        navigate(to = PaymentFragment(), addToBackStack = false)
    }

    override fun onResume() {
        super.onResume()
        val decorView = dialog.window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        decorView.systemUiVisibility = uiOptions
    }

    private fun navigate(from: Fragment? = null, to: Fragment, addToBackStack: Boolean = true) {
        from?.let {
            navigationDelegate.navigate(from, to, addToBackStack)
        }.let {
            navigationDelegate.navigateTo(to, addToBackStack)
        }
    }
}

当我显示DialogFragment时,出现以下错误消息:

  

java.lang.IllegalArgumentException:找不到片段PaymentFragment {7b58f52#3 id = 0x7f08008f}的ID 0x7f08008f(ni.devotion.hisabapp:id / framePayment)的视图

但是视图存在于布局中,请任何人知道如何解决此问题...

<FrameLayout
            android:id="@+id/framePayment"
            android:layout_width="match_parent"
            android:layout_height="300dp"/>

1 个答案:

答案 0 :(得分:0)

要解决我要解决的问题,

navigationDelegate = NavigationDelegate(fragmentManager!!, fragmentContainer.id)

进入

navigationDelegate = NavigationDelegate(this.childFragmentManager, fragmentContainer.id)