viewpager 中的片段不显示何时在 recyclerview 中

Fragment inside a viewpager doesnt show when is in recyclerview

也许这是一个愚蠢的问题,但这毁了我的一天...

我在片段中有一个 recyclerview

override fun setUpRecyclerView(pics: List<Pictures>) {
    recyclerView.setHasFixedSize(true)

    layoutManager = LinearLayoutManager(context)
    recyclerView.setLayoutManager(layoutManager)

    mAdapter = NewsAdapter(pics, childFragmentManager)
    recyclerView.setAdapter(mAdapter)
}

NewsAdapter 是:

class NewsAdapter
        (private val mDataset: List<Pictures>, private val fragmentManager: FragmentManager)
    : RecyclerView.Adapter<NewsAdapter.ViewHolder>() {

    class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        var authorTextView: TextView
        var viewPager: ViewPager
        var indicator: CircleIndicator

        init {
            authorTextView = v.findViewById<View>(R.id.tv_author) as TextView
            viewPager = v.findViewById<View>(R.id.viewPager) as ViewPager
            indicator = v.findViewById<View>(R.id.indicator) as CircleIndicator
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup,
                                    viewType: Int): NewsAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_preview, parent, false)

        return ViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val pictureList = mDataset.get(position)
        holder.titleTextView.text = pictureList.title
        holder.authorTextView.text = pictureList.author
        showContent(pictureList.content, holder.viewPager, holder.indicator)
    }

    fun showContent(contentModel: List<ContentModel>, viewPager: ViewPager, indicator: CircleIndicator) {
        if (contentModel.size <= 1) {
            indicator.hide()
        }
        viewPager.adapter = GalleryContentAdapter(fragmentManager, contentModel)
        indicator.setViewPager(viewPager)
    }

    override fun getItemCount(): Int = mDataset.size
}

GalleryContentAdapter 是:

class GalleryContentAdapter(fm: FragmentManager, val contentModels: List<ContentModel>) : FragmentPagerAdapter(fm) {
    val galleryContentFactory: GalleryContentFactory = GalleryContentFactory()

    override fun getItem(position: Int): Fragment = galleryContentFactory.getFragment(contentModels[position])

    override fun getCount(): Int = contentModels.size
}

从现在开始,我只处理一种内容,工厂很容易,但这是代码:

class GalleryContentFactory {
    fun getFragment(contentModel: ContentModel): Fragment {
        when (contentModel.type) {
            "PICTURE" -> return PictureFragment.newInstance(contentModel.value)
        }
        return PictureFragment.newInstance(contentModel.value)
    }
}

最后一个是PictureFragment:

class PictureFragment : NewsContentFragment() {

    companion object {
        val KEY = "PictureFragment.URL"

        fun newInstance(url: String): NewsContentFragment {
            val fragment = PictureFragment()
            val bundle = Bundle()
            bundle.putString(KEY, url)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_news_gif, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        arguments?.getString(KEY).let {
            view.image.loadUrl(it!!)
        }
    }
}

当我调试代码时,我检查 view.image.loadUrl(it!!) 行是否正在执行,但没有渲染任何内容。

loadUrl 方法是 imageviews 的扩展函数,可以像这样使用 Glide 加载图像

fun ImageView.loadUrl(url: String) {
    GlideApp.with(context).load(url).into(this)
}

并且被正确调用。

我也尝试使用 PagerStateAdapter 并考虑到我正在将 ChildFragmentManager 传递给 ViewPager 的适配器。

如果你能帮助我,你会拯救我的一天。提前致谢

这行不通。要走的路是将回收商变成回收商。如果你想模拟 ViewPager,你可以使用 https://github.com/rubensousa/RecyclerViewSnap 和 MATCH_PARENT 子视图。