如何从View Pager Fragment打开片段

时间:2018-08-09 09:00:57

标签: android kotlin android-viewpager fragment

我已经实现了TabLayout。它包含三个选项卡。第一个选项卡包含数据清单。当用户单击列表项时,我想显示另一个片段。我尝试过但没有成功。请帮忙寻找解决方法

ViewPager适配器

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
        }
    }

片段适配器

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.

2 个答案:

答案 0 :(得分:0)

不需要片段适配器。只需在活动中设置视图寻呼机即可。 然后在活动中启动您的片段。片段适配器中无需单击监听器。希望对您有帮助!

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);
    }

答案 1 :(得分:0)

经过多次尝试,我找到了自定义导航栏,并应用了调用嵌套片段所需的更改。它对我来说很完美。希望它也会帮助其他人。

https://github.com/adib2149/BottomNavBar