繁体   English   中英

如何将 RecyclerView 和 CardView 实现到片段中?

[英]How can I implement a RecyclerView and a CardView into a fragment?

我对 Android Studio 非常陌生,我正在构建一个应用程序,该应用程序的侧边栏带有导致碎片的图标。 在学习了几个教程之后,实现了侧边栏,每个图标都指向一个片段。 在其中一个片段中,我想设置一个 RecyclerView 和一个 CardView ,它们能够创建新条目并删除它们。 我在这里尝试按照 Flow 的教程中的编码,但是,他在 MainActivity 中实现它,而不是片段。 我尝试使用相同的代码将它们放入一个片段中,这似乎工作正常,直到他写道
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent);

有没有另一种方法来定义这个充气机? 或者我也可以参考另一个教程?

attach to root参数设置为 false 膨胀您的视图,如下所示

View view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent,false);

语法有什么问题? 您还应该通过构造函数在适配器中传递上下文变量。 您可以使用该context变量而不是parent.getContext()来访问所有活动方法。 如果您仍然感到困惑,也许粘贴有问题的代码,您将帮助我们给出更好的解释。 您还说您的回收站和卡片视图有问题,可能您的意思是说您无法使用findViewById访问回收站视图或卡片视图? 如果是这种情况,您可以使用视图 object 访问它们。 例子:

View view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent);
RecyclerView recyclerview = view.findViewById(R.id.recycler);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM