|
| 1 | +package org.billcarsonfr.jsonviewerexample |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Bundle |
| 5 | +import android.view.LayoutInflater |
| 6 | +import android.view.View |
| 7 | +import android.view.ViewGroup |
| 8 | +import android.widget.Button |
| 9 | +import androidx.fragment.app.Fragment |
| 10 | + |
| 11 | +/** |
| 12 | + * Activities that contain this fragment must implement the |
| 13 | + * [ChooseSampleFragment.OnFragmentMainNavigationListener] interface |
| 14 | + * to handle interaction events. |
| 15 | + * Use the [ChooseSampleFragment.newInstance] factory method to |
| 16 | + * create an instance of this fragment. |
| 17 | + */ |
| 18 | +class ChooseSampleFragment : Fragment() { |
| 19 | + |
| 20 | + |
| 21 | + private var mListener: OnFragmentMainNavigationListener? = null |
| 22 | + |
| 23 | + lateinit var showWrappedButton: Button |
| 24 | + |
| 25 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 26 | + super.onCreate(savedInstanceState) |
| 27 | + } |
| 28 | + |
| 29 | + override fun onCreateView( |
| 30 | + inflater: LayoutInflater, container: ViewGroup?, |
| 31 | + savedInstanceState: Bundle? |
| 32 | + ): View? { |
| 33 | + // Inflate the layout for this fragment |
| 34 | + return inflater.inflate(R.layout.fragment_choose_sample, container, false).also { view -> |
| 35 | + view.findViewById<Button>(R.id.showWrappedViewer)?.let { |
| 36 | + it.setOnClickListener { |
| 37 | + mListener?.navigateToWrapSample(true) |
| 38 | + } |
| 39 | + |
| 40 | + view.findViewById<Button>(R.id.showWrappedViewerScroll)?.let { |
| 41 | + it.setOnClickListener { |
| 42 | + mListener?.navigateToWrapSample(false) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + view.findViewById<Button>(R.id.showAsBottomSheet)?.let { |
| 48 | + it.setOnClickListener { |
| 49 | + mListener?.navigateToDialog() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + override fun onAttach(context: Context) { |
| 59 | + super.onAttach(context) |
| 60 | + if (context is OnFragmentMainNavigationListener) { |
| 61 | + mListener = context |
| 62 | + } else { |
| 63 | + throw RuntimeException("$context must implement OnFragmentMainNavigationListener") |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + override fun onDetach() { |
| 68 | + super.onDetach() |
| 69 | + mListener = null |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * This interface must be implemented by activities that contain this |
| 74 | + * fragment to allow an interaction in this fragment to be communicated |
| 75 | + * to the activity and potentially other fragments contained in that |
| 76 | + * activity. |
| 77 | + * |
| 78 | + * |
| 79 | + * See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information. |
| 80 | + */ |
| 81 | + interface OnFragmentMainNavigationListener { |
| 82 | + fun navigateToWrapSample(wrap: Boolean) |
| 83 | + fun navigateToDialog() |
| 84 | + } |
| 85 | + |
| 86 | + companion object { |
| 87 | + fun newInstance(): ChooseSampleFragment { |
| 88 | + return ChooseSampleFragment() |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments