Kitesurfer
Kitesurfer

Reputation: 3561

BottomSheetDialogFragment.viewBinding

I struggle to do a property delegate for ViewBinding on BottomSheets.

The general Idea is similar to this

For Fragments i use something like this

fun <T : ViewBinding> Fragment.viewBinding(viewBindingFactory: (View) -> T) =
FragmentViewBindingDelegate(this, viewBindingFactory)

On BottomSheetsDialogFragments which are Fragments it does not accept the delegate.

fun <T : ViewBinding> BottomSheetDialogFragment.viewBinding(viewBindingFactory: (View) -> T) =
FragmentViewBindingDelegate(this, viewBindingFactory)

The Lifecycle of BottomSheets would be the same as on regular Fragments, so i would not expect any problems.

Anyone came up with a solution on this ?

Upvotes: 3

Views: 7238

Answers (4)

francis
francis

Reputation: 4515

This works:

class CampaignBottomSheet : BottomSheetDialogFragment() {
    private var _binding: MenuCampaignsBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        _binding = MenuCampaignsBinding.inflate(layoutInflater)

        binding.startCampaign.setOnClickListener { println("__print::CampaignBottomSheet") }

        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

Upvotes: 1

Areeb Momin
Areeb Momin

Reputation: 477

Implement ViewBinding in BottomSheetFragment like this (Works for me):

class CustomBottomSheet : BottomSheetDialogFragment() {

private lateinit var binding: CustomBottomSheetBinding

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    binding = CustomBottomSheetBinding.inflate(inflater, container, false)
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    //onClick listener
    binding.button.setOnClickListener {
        Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show()
    }
}

}

Upvotes: 6

Markymark
Markymark

Reputation: 2989

You can still use the FragmentViewBindingDelegate and extension from Gabor. You just also need to inflate the view inside onCreateView().

For example:

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.zhuinden.fragmentviewbindingdelegatekt.viewBinding


class ExampleBottomSheet : BottomSheetDialogFragment() {

    //Using ::bind here since the view is already inflated in onCreateView()
    private val binding by viewBinding(YourCustomViewBinding::bind)


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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //Use binding here or wherever you need it
    }
}

Upvotes: 9

sdex
sdex

Reputation: 3871

Unlike Fragment class, BottomSheetDialogFragment doesn't have a constructor that accepts layout resource.
You can create a custom dialog for it. Basically, copy everything from AppCompatDialogFragment and BottomSheetDialogFragment.

Upvotes: -1

Related Questions