我看过各种关于如何将 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
中扩充包含 listview
的 view
。只需创建一个 adapter
,为其提供一些数据,然后在 fragment
的 onCreate()
中调用 setListAdapter()
。然后在其他地方(大概在您的 Activity
中)您可以使用 fragmentmanager
添加此 fragment
。
关于java - 如何在 Android 中填充 ListFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11424384/