gpt4 book ai didi

android - 使用自定义 ArrayAdapter 将 float 操作按钮添加到 ListFragment

转载 作者:行者123 更新时间:2023-11-29 01:25:13 26 4
gpt4 key购买 nike

我使用本指南创建了一个从 JSON 解析其数据的自定义 ListView。

https://www.bignerdranch.com/blog/customizing-android-listview-rows-subclassing/

我想向 ItemListFragment 添加一个 float 操作按钮,但不知道如何操作。

试图把它放在下面:

public ItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.item_view_children, this, true);
setupChildren();
}

但它会显示到我不想要的每个项目。

完整示例代码如下:

https://github.com/bignerdranch/android-listview-custom-view/tree/master/ListItemViewDemo/src/com/bignerdranch/android/listitemviewdemo

有什么办法吗?

试图修改 inflate 但不起作用:

public static ItemView inflate(ViewGroup parent) {
ItemView itemView = (ItemView)LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_view, parent, false);
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
FloatingActionButton fab = (FloatingActionButton) inflater.inflate(R.layout.fab_view, parent, false);
return itemView;
}

编辑:

如果我把它放在构造函数下,它就可以正常工作。但是因为每个 View 都会多次调用构造函数,所以我必须找到一种只调用一次的方法。

public ItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.item_view_children, this, true);
LayoutInflater.from(context).inflate(R.layout.fab_view, this, true);
setupChildren();
}

最佳答案

终于找到解决办法了。比我想的要简单得多。扩展 ListFragment onCreateView 方法的 ItemListFragment 调用覆盖的方法。

所以替换:

View v = super.onCreateView(inflater, container, savedInstanceState);

与:

View v = inflater.inflate(R.layout.list_view, null); 

成功了!

还必须设置list_vew.xml:

<FrameLayout>
<ListView android:id="@id/android:list" />
<Button />
</FrameLayout>

关于android - 使用自定义 ArrayAdapter 将 float 操作按钮添加到 ListFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34481770/

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