如果 ListView 适配器中没有数据,我想显示一个刷新按钮和一个 TextView。我还希望能够向将重新加载列表的按钮添加点击监听器。以下是我定义当前 Activity 的方式:
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));
.....
.....
}
这是我的 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"/>
我应该在哪里为我的刷新按钮设置点击监听器?
最佳答案
像下面这样更改您的 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>
将那几行放在java中
View empty = findViewById(R.id.emptyView);
btn_retry=(Button)empty.findViewById(R.id.btn_retry);
listView.setEmptyView(empty);
现在您可以在同一个 Activity 中编写 onClickListener(),因为它属于同一个 xml 布局。
btn_retry.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Do the refresh thing or verify with Toast.
}
});
在这里,如果 ListView 是空的,它将显示按钮刷新。否则它将显示已填充的列表。
关于android - ListView android中的自定义空 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16142274/