ViewPager + FragmentPagerAdapter,片段不可见

时间:2020-08-06 10:44:27

标签: android android-viewpager fragment

我在FragmentPagerAdapter中的片段不可见,我不知道为什么会这样。之后,我搜索此问题的解决方案。主要有两种方法可以解决这种问题。

  1. 使用childFragmentManager
  2. 不要覆盖isViewFromObject

我遵守上述规则。但仍然无法正常工作。 以下是我的代码。

适配器

  mBinding.viewpager.adapter = object : FragmentPagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getItem(position: Int): Fragment {
                return TestFragment()
            }

            override fun getCount(): Int {
                return 4
            }
        }

片段

class TestFragment : Fragment(){
        private lateinit var mRootView: View


        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            if (!this::mRootView.isInitialized) {
                mRootView = inflater.inflate(R.layout.homepage_videoview_fragment, container, false)
            }
            return mRootView
        }
    }

布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">
    <com.zhixin.wedeep.homepage.ui.view.CustomVideoView
        android:id="@+id/custom_video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        />
    <ImageView
        android:id="@+id/image_view_cover"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/cover_1"/>
</FrameLayout>

对这个问题有什么想法吗?谢谢。

0 个答案:

没有答案