gpt4 book ai didi

java - 如何在 Android 中填充 ListFragment?

转载 作者:行者123 更新时间:2023-11-29 05:57:40 24 4
gpt4 key购买 nike

我看过各种关于如何将 Activity 变成 ListActivity 以填充 ListView 的教程,但没有看到任何关于 ListFragments 的教程。我有两个选项卡,每个选项卡下都有一个 fragment 。这是我的 fragment 目前的样子......有人可以告诉我如何做到这一点吗?

public class MainFragment extends ListFragment {    

String[] items = { "12 Monkeys", "(500) Days of Summer", "Chariots of Fire" };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_main, container, false);
}
}

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

我正在尝试让 items 显示在 ListView 中。

最佳答案

您不需要在 ListFragment 中扩充包含 listviewview。只需创建一个 adapter,为其提供一些数据,然后在 fragmentonCreate() 中调用 setListAdapter()。然后在其他地方(大概在您的 Activity 中)您可以使用 fragmentmanager 添加此 fragment

关于java - 如何在 Android 中填充 ListFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11424384/

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