RecyclerView未在片段中查看

时间:2018-05-20 18:39:08

标签: android android-fragments android-recyclerview

当我在视图中添加fragment时,它不会显示。只有当我点击OptionsButton时,fragment才会显示。但我不知道RecyclerView是否只是没有显示任何元素,或者fragment是否未启动。

MainActivity的代码

override fun onCreate(savedInstanceState: Bundle?) {
val fragment= ArticleFragment()
            var args=Bundle()
            args.putInt("id",0)
            fragment.arguments=args
            supportFragmentManager.beginTransaction().replace(R.id.fragment_area,fragment).addToBackStack(null).commit()
}

ArticleFragment的代码

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    val view= inflater.inflate(R.layout.fragment_article, container, false)

    articles=getArticleJson(url)

    val recyclerView=view.findViewById<RecyclerView>(R.id.articlesRecyclerView)

    recyclerView.setHasFixedSize(true)

    val lm = LinearLayoutManager(activity)
    recyclerView.layoutManager = lm

    val adapter = ArticleAdapter(context = context, articles = articles) {
        val intent = Intent(activity, ArticleWebActivity::class.java).apply
        {

             putExtra("article", Gson().toJson(it))

        }
        startActivity(intent)
    }

    recyclerView.adapter = adapter

    return view
}

0 个答案:

没有答案