如何在Android中填充ListFragment?

时间:2012-07-11 01:39:49

标签: java android eclipse android-listview android-fragments

我已经看过各种关于如何将Activity转换为ListActivity来填充ListView的教程,但是ListFragments却没有。我有两个标签,每个标签下都有一个片段。这是我的片段目前的样子....有人可以告诉我如何做到这一点吗?

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中。

1 个答案:

答案 0 :(得分:3)

您无需在view内部夸大listview ListFragmentadapter。只需创建onCreate(),在fragment来电setListAdapter()的{​​{1}}内提供一些数据。然后在其他地方(可能在您的Activity中),您可以使用fragment添加此fragmentmanager