ListView android中的自定义空视图

Lal*_*rma 4 android listview

如果ListView适配器中没有数据,我想显示刷新Button和TextView.我还希望能够向将重新加载列表的按钮添加单击侦听器.以下是我定义当前活动的方式:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.foods);

    arrList=new ArrayList<FoodsData>();

    listView=(ListView)findViewById(R.id.list_foods);

    this.listView.setEmptyView(findViewById(R.id.emptyView));

    .....
    .....
}
Run Code Online (Sandbox Code Playgroud)

这是我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
<LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:id="@+id/emptyView">
    <TextView
        android:layout_width="wrap_content"
        android:textColor="@android:color/white"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Click Refresh to load data"/>
   <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_retry"
        android:textColor="@android:color/white"
        android:background="@drawable/orange_button_selecter"
        android:layout_gravity="center"
        android:textSize="25sp"
        android:text="@string/retry"/>
</LinearLayout>
<ListView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:dividerHeight="2dp"
    android:id="@+id/list_foods"/>
Run Code Online (Sandbox Code Playgroud)

我在哪里设置刷新按钮的点击监听器?

use*_*629 10

像下面一样更改你的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">
    <FrameLayout 
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_centerInParent="true"
        android:id="@+id/emptyView">
        <TextView
            android:layout_width="wrap_content"
            android:textColor="@android:color/white"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="Click Refresh to load data"/>
       <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_retry"
            android:textColor="@android:color/white"
            android:background="@drawable/orange_button_selecter"
            android:layout_gravity="center"
            android:textSize="25sp"
            android:text="@string/retry"/>
    </FrameLayout>
<ListView 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:dividerHeight="2dp"
    android:id="@+id/list_foods"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

把这些行放在java中

    View empty = findViewById(R.id.emptyView);
    btn_retry=(Button)empty.findViewById(R.id.btn_retry);
    listView.setEmptyView(empty);
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用相同的xml布局在相同的Activity中编写onClickListener().

btn_retry.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //Do the refresh thing or verify with Toast.
            }
        });
Run Code Online (Sandbox Code Playgroud)

在这里,如果listview为空,它将显示按钮刷新.否则它将显示填充列表.