【问题标题】:How do I populate a ListFragment in Android?如何在 Android 中填充 ListFragment?
【发布时间】:2012-07-11 01:39:49
【问题描述】:

我看过各种关于如何将 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 中。

【问题讨论】:

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


    【解决方案1】:

    您无需在您的ListFragment 中填充包含listviewview。只需创建一个adapter,给它一些数据,然后在你的fragmentonCreate() 中调用setListAdapter()。然后在其他地方(大概在你的Activity)你可以添加这个fragmentfragmentmanager

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多