自定义带加载中与加载失败的EmptyView的ListView

来源:互联网 发布:同方云计算战略发布会 编辑:程序博客网 时间:2024/06/07 06:07

自定义ListView

用FrameLayout自定义了一个ListView

代码如下:

public class EmptyListVisew extends FrameLayout {    private ListView listView;    private LinearLayout ll1;    private LinearLayout ll2;    public ListView getListView() {        return listView;    }    public EmptyListVisew(Context context) {        super(context);        addListView();    }    public EmptyListVisew(Context context, AttributeSet attrs) {        super(context, attrs);        addListView();    }    public EmptyListVisew(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        addListView();    }    private void addListView() {        listView = new ListView(getContext());        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,                LayoutParams.MATCH_PARENT);        listView.setLayoutParams(params);        addView(listView);    }    public void setProgressView(String str) {        setProgressView(null, str);    }    public void setProgressView(ProgressBar bar, String str) {        ll1 = new LinearLayout(getContext());        ll1.setOrientation(LinearLayout.VERTICAL);        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,                LayoutParams.MATCH_PARENT);        ll1.setGravity(Gravity.CENTER);        ll1.setLayoutParams(params);        if (bar == null) {            bar = new ProgressBar(getContext());        }        TextView textView = new TextView(getContext());        textView.setText(str);        textView.setGravity(Gravity.CENTER);        ll1.addView(bar);        ll1.addView(textView);        if (ll1 != null) {            removeView(ll1);        }        if (ll2 != null) {            removeView(ll2);        }        addView(ll1);        listView.setEmptyView(ll1);    }    public void setEmptyView(String str) {        setEmptyView(-1, str,null);    }    public void setEmptyView(int rid, String str,OnClickListener click) {        ll2 = new LinearLayout(getContext());        ll2.setOrientation(LinearLayout.VERTICAL);        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,                LayoutParams.MATCH_PARENT);        ll2.setGravity(Gravity.CENTER);        ll2.setLayoutParams(params);        if (click!=null) {            ll2.setOnClickListener(click);        }        if (rid != -1) {            ImageView imageView = new ImageView(getContext());            imageView.setImageResource(rid);            ll2.addView(imageView);        }        TextView textView = new TextView(getContext());        textView.setText(str);        textView.setGravity(Gravity.CENTER);        ll2.addView(textView);        if (ll1 != null) {            removeView(ll1);        }        if (ll2 != null) {            removeView(ll2);        }        addView(ll2);        listView.setEmptyView(ll2);    }}

使用方法:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);elv = new EmptyListVisew(this);lv = elv.getListView();elv.setProgressView("正在加载中...");setContentView(elv);post();}int i = 0;void post() {new Handler().postDelayed(new Runnable() {@Overridepublic void run() {if (i == 2) {lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, new String[]{"hello", "world"}));}elv.setEmptyView(R.mipmap.ic_launcher, "加载失败,点击刷新", new View.OnClickListener() {@Overridepublic void onClick(View arg0) {elv.setProgressView("继续加载...");i++;post();}});}}, 2000);}

效果如下:

效果


github:https://github.com/canyinghao/EmptyListVisew

0 0