android自定义空的view,ListView android中的自定义空视图

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

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.foods);

arrList=new ArrayList();

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

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

.....

.....

}

这是我的xml文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:orientation="vertical"

android:layout_height="match_parent">

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:orientation="vertical"

android:layout_centerInParent="true"

android:id="@+id/emptyView">

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"/>

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"/>

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:dividerHeight="2dp"

android:id="@+id/list_foods"/>

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

解决方法:

像下面一样更改你的xml:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:orientation="vertical"

android:layout_height="fill_parent">

android:layout_height="fill_parent"

android:layout_width="fill_parent"

android:layout_centerInParent="true"

android:id="@+id/emptyView">

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"/>

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"/>

android:layout_height="fill_parent"

android:layout_width="fill_parent"

android:dividerHeight="2dp"

android:id="@+id/list_foods"/>

把这些行放在java中

View empty = findViewById(R.id.emptyView);

btn_retry=(Button)empty.findViewById(R.id.btn_retry);

listView.setEmptyView(empty);

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

btn_retry.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//Do the refresh thing or verify with Toast.

}

});

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

标签:android,listview

来源: https://codeday.me/bug/20190723/1509860.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值