0

I have Implemented TabLayout. It contains three tabs. The first tab contains Listing of data. when User clicks on list item I want to show another Fragment. I have tried but not got success. Please help for find solution

ViewPager Adapter

inner class ViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
        val mFragmentList: ArrayList<Fragment> = ArrayList()
        private val mFragmentTitleList: ArrayList<String> = ArrayList()

        override fun getItem(position: Int): Fragment {
            return mFragmentList.get(position)
        }

        override fun getCount(): Int {
            return mFragmentList.size
        }

        fun addFrag(fragment: Fragment, title: String) {
            mFragmentList.add(fragment)
            mFragmentTitleList.add(title)
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mFragmentTitleList.get(position)
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val createdFragment = super.instantiateItem(container, position) as Fragment
            mFragmentList[position] = createdFragment
            return createdFragment
        }
    }

Fragment Adapter

class MyAdapter(private val mActivity: FragmentActivity?, private var mMyModelList: ArrayList<MyModel>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
    private var mItemBinding: ItemBinding? = null

    inner class MyViewHolder(val mBinding: ItemBinding) : RecyclerView.ViewHolder(mBinding.getRoot())

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val inflater = LayoutInflater.from(mActivity)

        mItemBinding = DataBindingUtil.inflate(inflater, R.layout.item, null, false)

        return MyViewHolder(mItemBinding!!)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val mMyModel: MyModel = mMyModelList[position]

            holder.mBinding.txtTitle.setTextColor(mActivity!!.resources.getColor(R.color.light_green))
            holder.mBinding.txtTitle.setText(mMyModel.lbl)
            holder.mBinding.txtDateDay.setText(mMyModel.Day + " - " + mMyModel.date)
            holder.mBinding.txtCircle.setBackgroundResource(R.drawable.green_circle)

        holder.mBinding.constraintLayout.setOnClickListener {
            var mSecondFragment: SecondFragment = SecondFragment()
           //Below code is not working
            mSecondFragment.onClickItem(mMyModel.id)

            /*Here I want to show another Fragment*/ 
        }
    }

    override fun getItemCount(): Int {
        return mMyModelList.size
    }
}
Fragment should open from Viewpager's Fragment.
Komal
  • 79
  • 1
  • 10
  • Possible duplicate of [How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)](https://stackoverflow.com/questions/13379194/how-to-add-a-fragment-inside-a-viewpager-using-nested-fragment-android-4-2) – Aseem Sharma Aug 09 '18 at 09:34
  • If I'm adding Like below FrameLayout fl = new FrameLayout(getActivity()); fl.setId(10000); SecondFragment iif = new SecondFragment (); Bundle args = new Bundle(); args.putInt("position", getArguments().getInt("position")); iif.setArguments(args); getChildFragmentManager().beginTransaction() .add(10000, iif, "initialTag").commit(); the app is crashing showing in log java.lang.IllegalStateException: Fragment has not been attached yet. – Komal Aug 09 '18 at 10:06

2 Answers2

0

there is no need for fragment adapter. just setup the view pager in activity. then initiate your fragment in activity as well. There is no need on click listener in fragment adapter. Hope it helps you !

HistoryOnGoingFragment historyOnGoingFragment = new HistoryOnGoingFragment();
    HistoryPastFragment historyPastFragment = new HistoryPastFragment();

private void setUpViewPager(int tabPosition) {
        TabLayoutViewPagerAdapter adapter = new TabLayoutViewPagerAdapter(getFragmentManager());
        adapter.addFragment(historyOnGoingFragment,"UPCOMING");
        adapter.addFragment(historyPastFragment,"COMPLETED");

        historyViewPager.setAdapter(adapter);
        historyViewPager.setCurrentItem(tabPosition);
        historyTabLayout.setupWithViewPager(historyViewPager);
    }
  • I have already added fragment like you've mentioned. private fun setupViewPager(viewPager: ViewPager) { adapter = ViewPagerAdapter(supportFragmentManager) adapter.addFrag(FirstFragment(), "ONE") adapter.addFrag(SecondFragment(), "TWO") adapter.addFrag(ThirdFragment(), "THREE") viewPager.adapter = adapter } That's fine But I want to open fragment when user click on one of it. We can say that Nested Fragment. – Komal Aug 09 '18 at 09:16
  • If user click your view pager the fragment, it opens the fragment right ? my code should have match your requirement then – Daniel Wijono Aug 09 '18 at 09:29
0

After many tried I found custom Navigation Bar and applied changes which I needed for call nested fragment. It's working perfectly for me. Hope It'll help others too.

https://github.com/adib2149/BottomNavBar

Komal
  • 79
  • 1
  • 10