当我在视图中添加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
}