Fragment's RecycleView inherits bottomNavigationBar from the context and causes duplication - android

I have a fragment which uses recycle view inside it. The context that holds this fragment contains a bottom navigation bar.
When using recycle view of the fragment, it inherits the bottom navigation bar which causes two duplicated bottom navigation bars as the below picture. How do I fix this, so the fragment does not have the bottom navigation bar?
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var view = inflater.inflate(R.layout.fragment_article, container, false)
view.rcvArticle.layoutManager = LinearLayoutManager(view.context)
view.rcvArticle.adapter = ArticleAdapter(Article.articles.shuffled())
return view
}

Related

How to use Bottom Navigation within a Fragment rather than Main Activity

I'm trying to use Bottom Navigation in my app. Most explanations online are about using Bottom Navigation to navigate a fragment container from WITHIN the MainActivity. However, I am trying to use the Bottom Navigation from within a fragment and not an activity. Can anybody help me? This is the code I have thus far, the line in bold represents the problem saying:
"Too many arguments for public fun Fragment.findNavController(): NavController defined in androidx.navigation.fragment"
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentHomePageBinding.inflate(inflater, container, false)
val bottomNavigationView = binding.bottomNavigation
val navController = findNavController(**R.id.homeScreenFragment**)
bottomNavigationView.setupWithNavController(navController)
return binding.root
}
}

How do i use findViewById in a fragment instead of an activity?

So I'm building this calculator and I used activities but now I have to use fragments instead.
I had these buttons implemented but now that I've moved them in a fragment I cant use findViewById. How can I assign the id to my variables?
Notice that onViewCreated has a view parameter. You can call it on that so like view.findViewById(R.id.text)
I would suggest to use viewBinding, and if you are not comfortable with that then you should do it in onCreateView() method of your fragment. Like this,
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
view = inflater.inflate(R.layout.your_layout_file, container, false)
val imageView = view.findViewById<ImageView>(R.id.id_of_imageview)
return view
}

How to draw shapes with OpenGl inside fragment instead of activity

I am using this instruction: https://developer.android.com/training/graphics/opengl/environment.
But I have to work with fragment instead of activity. Also I have some picture on main fragment and using OpenGl I have to draw some shapes. But, when I start app in becomes just empty dark screen.
Somewhere I found that I can write something like this for fragment, but If I remove this line of code my xml layout become on app, but I need to mix fragment and draw shapes over this fragment.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
lifecycle.addObserver(viewModel)
val rootView = inflater.inflate(R.layout.main_fragment, container, false)
val fL = rootView.findViewById<ConstraintLayout>(R.id.main)
customGLSurfaceView = CustomGLSurfaceView(requireContext())
fL.addView(customGLSurfaceView) // this line
return rootView
}

How to hide BottomSheetDialogFragment after navigating to another fragment

i have Fragment that include ImgaeView(s) in it's XML and i'm navigating from these images to another fragments but the problem is that the bottomsheet stays open, how to make it collapse when i navigate to another fragment?
here is a picture of the bottomsheet
and here i navigated to another fragment but the bottomsheet still appears on the screen
here is the code for inside the fragment
class MoreFragment : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_more, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
home_button.setOnClickListener{
val homeFragment = HomeFragment()
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.nav_host_fragment, homeFragment, "findThisFragment")
?.addToBackStack(null)
?.commit()
}
so my question is:
how to make it collapse down after i navigate to another fragment?
in Onclick listener before navigating to another fragment call dismiss() function
Just remove .addToBackStack(null) and call method dismiss()

Fragments Not Displaying After Switching To Different Fragment And Back

I have an activity with three different fragments that are switched to via a bottom navigation view. The middle fragment, TutorialFragment, has a viewpager that switches between two other fragments.
The problem is that if I switch from TutorialFragment to a different fragment and then back to TutorialFragment via the bottom navigation view, the fragments inside TutorialFragment's viewpager won't display.
Here's an example (notice how after I switch to "feedback" from "tutorial" and then back to "tutorial", the "Use Keyboard Fragment" and "Enable Keyboard Fragment" strings in the top right of the app no longer show):
Here is the code for TutorialFragment:
class TutorialFragment : Fragment() {
private lateinit var tutorialView : View
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
tutorialView = inflater.inflate(R.layout.fragment_tutorial, container, false )
tutorialView.tutorialViewPager.adapter = TutorialFragmentPagerAdapter(fragmentManager!!)
tutorialView.circleIndicator.setViewPager(tutorialView.tutorialViewPager)
return tutorialView
}
The solution turned out to be replacing
tutorialView.tutorialViewPager.adapter = TutorialFragmentPagerAdapter(fragmentManager!!)
with
tutorialView.tutorialViewPager.adapter = TutorialFragmentPagerAdapter(childFragmentManager)

Categories

Resources