gpt4 book ai didi

具有自定义项目复杂布局的 Android ExpandableListView

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:16 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,但我遇到了一个我想要实现的复杂布局层次结构的障碍。该应用程序将列出咖啡馆的菜单。截至目前,菜单如下所示:

enter image description here

该列表作为 RecyclerView 实现。 View 的自定义适配器负责每个元素的操作。触摸一个元素会增加订单价格并将所选元素总数加 1。您还可以单击该数字以获取提货对话框并更改金额。

你可以想象那里有很多代码。

这是我要实现的理论布局:

enter image description here

我专注于内部 ExpandableListView。基本上我想按食物、饮料等对菜单项进行分类。

我遇到的问题是我不知道如何才能保持项目布局的“复杂性”。我设法创建了一个简单的 ExpandableListView,它只包含项目名称,如下所示:

enter image description here

但我真的不知道如何在 ExpandableListView 中使用我为之前的布局创建的项目适配器。不幸的是,我不能分享太多代码,但我不是要代码 fragment ,而是更多关于如何实现它的方向、想法、技巧……这是我的第一个 Android 应用程序:-)

如有任何帮助,我们将不胜感激。

非常感谢!

最佳答案

好吧,我找到了解决问题的方法,最终并没有那么复杂。我会尽量总结一下,以防有人遇到同样的情况。

首先,您必须为您的 ExpandableListView 创建自定义适配器。我以前的 RecyclerView 确实有一个自定义适配器,它使用了 ViewHolder 模式,这让我很困惑。

This该示例实际上对于理解如何实现自定义适配器非常有用。要覆盖的重要方法是 getChildView。在该方法中,构建了整个 subview 。

@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final MenuItem item = (MenuItem) getChild(listPosition, expandedListPosition);

if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.menu_item, null);
}

// Save final reference to be able to access it in inner classes
final View finalConvertView = convertView;

// Set the item name
TextView itemName = (TextView) convertView.findViewById(R.id.item_menu_name);
itemName.setText(item.getName());

// Set item price
TextView itemPrice = (TextView) convertView.findViewById(R.id.item_menu_price);
itemPrice.setText(item.getFormattedPrice());

这不是完整的代码,但您可以看到如何在 menu_item.xml 布局中获取 View 并填充它们:-)

在我使用 Android data bingind 之前,但我不知道如何在没有 ViewHolder 的情况下使用它。

关于具有自定义项目复杂布局的 Android ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37228614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com