分别设置listview加载中、空数据、加载数据失败三种状态的显示

本文介绍了一个用于Android开发的实用组件EmptyLayout,该组件能够帮助开发者轻松实现ListView在加载中、空数据及加载失败等不同状态下的界面展示。通过示例代码展示了如何初始化组件并设置不同状态下的回调操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置listview加载中,空数据,加载数据失败三种状态的显示.

[img]http://dl2.iteye.com/upload/attachment/0093/5400/b0416ab6-8f03-3cb5-89fa-1cc99697c55c.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0093/5402/c2cd0426-fccf-3ef2-b3e6-5d7b44e4012a.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0093/5404/baf14353-8a49-3412-979a-7728b22a2bf7.png[/img]

基本用法:

import java.util.ArrayList;
import java.util.Arrays;

import com.kanak.emptylayout.EmptyLayout;


import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.app.ListActivity;

public class MainActivity extends ListActivity {

private EmptyLayout mEmptyLayout; // this is required to show different layouts (loading or empty or error)
private ArrayAdapter<String> mAdapter;
private View.OnClickListener mErrorClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Try again button clicked", Toast.LENGTH_LONG).show();
}
};

// the list items
static final String[] MOVIES = new String[] {
"Forrest Gump",
"Toy Story",
"Saving Private Ryan",
"Toy Story 2",
"The Green Mile",
"Cast Away",
"Road to Perdition",
"Catch Me If You Can",
"The Terminal",
"The Polar Express",
"The Da Vinci Code",
"Angels & Demons",
"Toy Story 3",
"Extremely Loud & Incredibly Close",
"Cloud Atlas",
"Captain Phillips",
"Toy Story 4",
"The Lost Symbol"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// initialize the empty view
mEmptyLayout = new EmptyLayout(this, getListView());
mEmptyLayout.setErrorButtonClickListener(mErrorClickListener);

// populate the list view
populateList();
}

// Triggered when "Empty" button is clicked
public void onShowEmpty(View view) {
// clear the list and show the empty layout
mAdapter.clear();
mEmptyLayout.showEmpty();
}

// Triggered when "Loading" button is clicked
public void onShowLoading(View view) {
// clear the list and show the loading layout
mAdapter.clear();
mEmptyLayout.showLoading();
}

// Triggered when "Error" button is clicked
public void onShowError(View view) {
// clear the list and show the error layout
mAdapter.clear();
mEmptyLayout.showError();
}

// Triggered when "List" button is clicked
public void onShowList(View view) {
// show the list
populateList();
}

private void populateList() {
ArrayList<String> list = new ArrayList<String>();
list.addAll(Arrays.asList(MOVIES));
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
setListAdapter(mAdapter);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值