Viewpager on fragment having issues with connecting pageadapter - android

class main_fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
lateinit var view: ViewPager
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main_fragment, container, false)
val viewPager = view.findViewById<ViewPager>(R.id.viewPager)
viewPager.adapter = PageAdapter(supportFragmentManager)
val tabLayout = view.findViewById<TabLayout>(R.id.tabLayout)
tabLayout.setupWithViewPager(viewPager)
}
}
i made a pageradapter but when i try and connect it doesn't work
i get an error saying unresolved referance:supportFragmentManager
if anyone could help me it would be much appreciated

first, you try to init your adapter after a return which means will never be executed.
the solution is:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
viewPager.adapter = PageAdapter(getChildFragmentManager());
}

Related

erorr when trying to reach view with findviewbyId

class main_fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main_fragment, container, false)
val viewPager = view.findViewById<ViewPager>(R.id.viewPager)
viewPager.adapter = PageAdapter(childFragmentManager)
val tabLayout = view.findViewById<TabLayout>(R.id.tabLayout)
tabLayout.setupWithViewPager(viewPager)
}
}
i am getting an erorr Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type View?
can anyone help me?
class main_fragment : Fragment() {
val view: View
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_main_fragment, container, false)
val viewPager = view.findViewById<ViewPager>(R.id.viewPager)
viewPager.adapter = PageAdapter(childFragmentManager)
val tabLayout = view.findViewById<TabLayout>(R.id.tabLayout)
tabLayout.setupWithViewPager(viewPager)
return view
}
}
override onViewCreated method
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
and then use view parameter from this function

Trying to make a spinner in a fragment and getting an error could youse a suggestion

The thing is when i try to set up a simple spinner into the second fragment of my application i get an error which i believe is appering because it has to do something with the main activity but im not sure what.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button_second).setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
setupspinner()
}
}
private fun setupspinner() {
val adapter =ArrayAdapter.createFromResource(
this,
R.array.zemlje1,
android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
}
}
The this, and the spinner are in the red sorry if i haven`t formated the question as i sould have had.

Numberpicker send data to HomeFragment (Fragment to Fragment)

I have app who have viewpagers and in inside is numberpicker. I try to send this data to my HomeFragment but crash my app. This is my source code. What i do wrong?
Create Interface for communication
interface Communicator {
fun passData(numberpicker: NumberPicker) {
}
This is my fragment with numberpicker
lateinit var comm: Communicator
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_period_length, container, false)
view.number_picker_period?.minValue = 21
view.number_picker_period?.maxValue = 35
view.number_picker_period?.wrapSelectorWheel = true
val viewPager = activity?.findViewById<ViewPager2>(R.id.viewPager)
view.next_txt_period.setOnClickListener {
viewPager?.currentItem = 1
comm.passData(view.number_picker_period)
}
return view
}
This is MainActivity
val fragment1 = DaysLength()
supportFragmentManager.beginTransaction().replace(R.id.content_id, fragment1).commit()
}
override fun passData(numberpicker: NumberPicker) {
val bundle = Bundle()
bundle.putString("input_txt", number_picker_period.toString())
val transaction = this.supportFragmentManager.beginTransaction()
val fragment2 = HomeFragment()
fragment2.arguments = bundle
}
My HomeFragment
var inputtext: String? = ""
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
inputtext = arguments?.getString("input_txt")
view.output_numberpicker.text = inputtext
return view
What i do wrong?
Error
Unable to start activity ComponentInfo{com.example.oapp/com.example.oapp.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080067 (com.example.oapp:id/content_id) for fragment DaysLength{aa77410}
your main activity is crashing on this line as it doesn't know what the view "number_picker_period" is
bundle.putString("input_txt", number_picker_period.toString())
try accessing the number picker through the parameter in the function called "numberPicker", Or alternatively, change your passData interface function to return the string value instead of the entire numberPicker

Why fragment based viewPager.setCurrentItem is not working inside an adapter?

I have a fragment in MainActivity that handles other fragments inside a view page.
This is that one:
class FragOrdersHolder : Fragment() {
lateinit var viewPager: ViewPager
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
val mainView = inflater.inflate(R.layout.orders_holder_frg, container, false)
return mainView
}
override fun onViewCreated(mainView: View, savedInstanceState: Bundle?) {
super.onViewCreated(mainView, savedInstanceState)
viewPager = mainView.findViewById(R.id.ordersViewPagerID)
viewPager.adapter = OrdersPagerAdapter(activity!!.supportFragmentManager)
val pagerTab = mainView.findViewById<PagerTabStrip>(R.id.pagerHeaderID)
pagerTab.tabIndicatorColor = Color.WHITE
pagerTab.setTextColor(Color.WHITE)
}
fun GoTo(pos: Int) {
Handler().post(Runnable {
viewPager.setCurrentItem(pos, true)
})
}
}
I have 3 fragments in my viewPager and one of them, have a recyclerview inside it.
the recyclerview has a custom adapter class.
I want to go to next tap in viewPager when clicking on an item in the recylerview.
I'm calling the GoTo method in adapter class like this:
val fm = (activity as MainActivity).supportFragmentManager
val fragm = fm.findFragmentByTag("HOLDER") as FragOrdersHolder
fragm.GoTo(2)
I'd tried everything viewPager.setCurrentItem not working!
please help me.

How do I correctly use Kotlin to call a blank fragment from the Home fragment?

class HomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val v = inflater.inflate(R.layout.fragment_home, container, false)
val fab = v.findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener {
val blankFragment = BlankFragment()
val manager = childFragmentManager
manager.beginTransaction().replace(R.id.frame_container, blankFragment, blankFragment.tag).commit()
// System.out.println("You have reached the floating action button")
}
return v
}
}
Getting a no view found error. I may have issues with the R.id.frame_content but Kotlin doesn't immediately identify all id values...
This may not be the best way and I apologize for the horrible formatting I does this while answer out on my phone and it's hard to get it perfect. Anyways In the activity that holds your fragments, such as MainActivity.kt add...
supportFragmentManager.beginTransaction.add(R.id.fragment_home, HomeFragment.newInstance, "HomeFragment").commit()
In your HomeFragment.kt change the following.
class HomeFragment: Fragment(){
companion object {
fun newInstance(): HomeFragment{
return HomeFragment() }
fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater!!.inflate(R.layout.fragment_home, container, false)
val activity = activity
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val clickListener = View.OnClickListener { view ->
when (view.getId()) {
R.id.fab -> NextFragment()
}
}
fab.setOnClickListener(clickListener)
}
NextFragment(){
val fragManager = activity?.supportFragmentManager
fragManager?.beginTransaction()?.replace(R.id.frame_container, BlankFragment.newInstance(), "blankFragment")?.commit()
}
Make sure you make the same changes to BlankFragment.kt
class BlankFragment: Fragment(){
companion object {
fun newInstance(): BlankFragment{
return BlankFragment() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater!!.inflate(R.layout.fragment_blank, container, false)
val activity = activity
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Do some stuff with your views in BlankFragment here
}
}

Categories

Resources