安卓。如何在回收器视图中显示 2 个项目,然后在每次单击按钮时添加一个项目
我想默认显示 2 个项目,并在单击主活动中的按钮时在回收器视图中添加项目。这些项目包含 edittext,因此我必须在主要活动中从 edittext 获取数据。
I want to show 2 items by default and add items in recycler view when we click button in main activity. Those item contains edittext so I have to get the data from edittext in my main activity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上你必须创建两个不同的 ViewType/Layouts 来实现这一点。
分享以下相关链接..
https:// /www.geeksforgeeks.org/how-to-create-recyclerview-with-multiple-viewtype-in-android/
如何创建具有多种视图类型的 RecyclerView
Basically you have to create two different ViewType/Layouts to implement this.
Sharing related links below..
https://www.geeksforgeeks.org/how-to-create-recyclerview-with-multiple-viewtype-in-android/
How to create RecyclerView with multiple view types
要在单击按钮时将项目添加到 Recycler 视图,您应该将项目添加到列表中,并使用
notifyItemInserted()
通知适配器新项目已添加到 recyclerview 的末尾对于将 edittext 值从适配器获取到活动的另一个问题,您可以创建一个在活动中实现的回调接口,并将引用传递给适配器,并在 Edittext 值更改时从适配器触发它
For adding items to Recycler view when you click in a button you should add the item in the list and notify adapter that new item added at the end of the recyclerview using
notifyItemInserted()
and for the other question for getting the edittext value from the adapter to the activity you can create a callback interface that implemented in the activity and pass the reference to the adapter and fire it from adapter when Edittext value is changed