上一篇中,我们搭建好了一个Demo。没有阅读的可以点击下面的链接:

http://www.cnblogs.com/fuly550871915/p/4866929.html

在这一篇中,我们将实现ListView的底布局。我们首先看实现效果,如下;

即底部出现一个进度条提示正在加载。废话不多说,直接进入代码。

一、底部布局编写

首先把这个底部布局编写出来,名为footer.xml。代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<LinearLayout
android:id="@+id/load_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleSmall"/><!-- 设置 样式成小的圆形进度条-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载..."
android:textSize="20sp"/>
</LinearLayout> </LinearLayout>

这样子,底布局文件我们就准备好了。

二、自定义ListView

终于来到这一步了,我们现在要给ListView加上底布局,当然就需要自定义它了。新建类MyListView,继承自ListView。我就不多解释了,代码注释写的很清楚了。如下:

 package com.fuly.load;

 import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView; public class MyListView extends ListView{ //注意,三个构造方法都要重写
public MyListView(Context context) {
super(context);
initView(context); }
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
} //初始化view
private void initView(Context context){ View footer = LayoutInflater.from(context).inflate(R.layout.footer, null);
//注意,这句代码的意思是给自定义的ListView加上底布局
this.addFooterView(footer); } }

好了,底布局我们可算是加上来了。下面就是使用我们自定义的ListView的时候了。

三、使用带底布局的ListView

这一步是最简单的,就是把之前ListView统统换成我们的MyListView即可。

首先修改activity_main.xml.如下:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccffff"> <com.fuly.load.MyListView
android:id= "@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="5dp"
android:divider="#00cc00"></com.fuly.load.MyListView>
</LinearLayout>

然后再修改MainActivity里面的即可。改动的地方很少,我还是贴出完成代码吧。如下:

 package com.fuly.load;

 import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.app.Activity; public class MainActivity extends Activity { private MyListView lv;
private List<MyData> mDatas = new ArrayList<MyData>();
private MyAdapter mAdapter; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initData();//该方法初始化数据
lv = (MyListView) findViewById(R.id.list_view);
mAdapter = new MyAdapter(this, mDatas);
lv.setAdapter(mAdapter); } /**
* 该方法初始化数据,即提供初始的素材
*/
private void initData() {
for(int i = 0;i<12;i++){
MyData md = new MyData("你好,我是提前设定的");
mDatas.add(md);
} }
}

好了,至此带底布局的ListView我们实现了。赶快运行看看是不是上面的效果吧。下一篇中,我们将实现真正的加载数据。

最新文章

  1. 移动端js知识总结
  2. 控制 Android 程序使用的内存
  3. Oracle 11g XE release2安装与指导
  4. python os.path.dirname 是什么目录
  5. linux C(hello world) 解方程
  6. linux下shell编程示例-获取进程id
  7. MYSQL: Cannot delete or update a parent row: a foreign key constraint fails
  8. 网页中如何用 CSS 设置打印分页符
  9. 深入浅出分析MySQL索引设计背后的数据结构
  10. 【干货】一篇文章学会Gulp(Getting started with Gulp)
  11. nodejs-url网址解析的好帮手
  12. SQL COUNT() 函数
  13. java格式化
  14. BaseDao优化
  15. 547. Friend Circles 求间接朋友形成的朋友圈数量
  16. LeetCode 312. Burst Balloons(戳气球)
  17. 从 bootup.js 学习加载脚本等资源
  18. 一句话木马:PHP篇
  19. iOS开发-UIWebView加载本地和网络数据
  20. Linux定时任务crontab使用指南

热门文章

  1. SpringMVC入门(一)
  2. win10中xshell的ssh链接virtualBox中的centos7
  3. Array中对象的排序
  4. Selenium库简介
  5. Agile software Development
  6. Java - 多线程中的不变性问题
  7. python监控linux内存并写入mongodb
  8. poj Corn Fields 状态压缩dp。
  9. hdu 1011 Starship Troopers 经典的树形DP ****
  10. easyui numberbox 输入框禁止输入