无法在片段上显示listView

时间:2016-03-19 00:44:30

标签: android

我有一个xml布局文件( ListLayout.xml ),我在其中创建了listView,我希望这个布局显示在main_activity_layout上的一个片段上。所以转到 MainActivity_layout 并添加了一个片段,当我建议选择一个类时,我选择了类 Listfrg ,其中我返回了OnCreateView方法中listLayout.xml的布局,但没有显示为什么?

ListLayout.xml

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

    <LinearLayout
        android:id="@+id/progress_container_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone" >

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/list_container_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/empty_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false" >
        </ListView>
    </FrameLayout>
</FrameLayout>

MainActivityLayout

  <fragment
        android:layout_width="162dp"
        android:layout_height="match_parent"
        android:name="com.example.pack2.t9ahbin.Listfrg"
        android:id="@+id/fragment3" />

Class Listfrg:

public class Listfrg extends ListFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frglist, container, false);
    return view;
}

}

1 个答案:

答案 0 :(得分:0)

  

没有显示为什么?

因为列表中没有数据。

请使用ArrayAdapter或某些适配器变体,使用adapter.add将一些数据放入其中,然后使用listView.setAdapter。实际上为Listview提供数据。

您可以在onCreateView中执行此操作,并使用getListView()或此方法获取ListView。

ListView listView = (ListView) view.findViewById(android.R.id.list);

Adapter是ListView的数据容器。没有一个,您希望如何显示数据?

此外,您的空视图需要使用android:id="@android:id/empty"

requirements

涵盖了这一点