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

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

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

 

                                    

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

 

一、底部布局编写

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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" 
 6     android:gravity="center_horizontal"
 7    >
 8     <LinearLayout 
 9         android:id="@+id/load_layout"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:gravity="center">
13     <ProgressBar 
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         style="?android:attr/progressBarStyleSmall"/><!-- 设置 样式成小的圆形进度条-->
17     <TextView 
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="正在加载..."
21         android:textSize="20sp"/>
22     </LinearLayout>
23 
24 </LinearLayout>

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

二、自定义ListView

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

 1 package com.fuly.load;
 2 
 3 import android.content.Context;
 4 import android.util.AttributeSet;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.widget.ListView;
 8 
 9 public class MyListView extends ListView{
10 
11     //注意,三个构造方法都要重写
12     public MyListView(Context context) {
13         super(context);
14         initView(context);
15         
16     }
17     public MyListView(Context context, AttributeSet attrs) {
18         super(context, attrs);
19         initView(context);
20     }
21     public MyListView(Context context, AttributeSet attrs, int defStyle) {
22         super(context, attrs, defStyle);
23         initView(context);
24     }
25     
26     
27     //初始化view
28     private void initView(Context context){
29         
30         View footer = LayoutInflater.from(context).inflate(R.layout.footer, null);
31         //注意,这句代码的意思是给自定义的ListView加上底布局
32         this.addFooterView(footer);
33         
34     }
35 
36 }

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

三、使用带底布局的ListView

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

    首先修改activity_main.xml.如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5      android:background="#ccffff">
 6      
 7     <com.fuly.load.MyListView
 8         android:id= "@+id/list_view"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:dividerHeight="5dp"
12         android:divider="#00cc00"></com.fuly.load.MyListView>
13 </LinearLayout>

 

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

 1 package com.fuly.load;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.os.Bundle;
 7 import android.app.Activity;
 8 
 9 
10 public class MainActivity extends Activity {
11     
12     private MyListView lv;
13     private List<MyData>  mDatas = new ArrayList<MyData>();
14     private MyAdapter mAdapter;
15 
16 
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         
21         initData();//该方法初始化数据
22         lv = (MyListView) findViewById(R.id.list_view);
23         mAdapter = new MyAdapter(this, mDatas);
24         lv.setAdapter(mAdapter);
25         
26         
27     }
28 
29 
30     /**
31      * 该方法初始化数据,即提供初始的素材
32      */
33     private void initData() {
34         for(int i = 0;i<12;i++){
35             MyData md = new MyData("你好,我是提前设定的");
36             mDatas.add(md);
37         }
38         
39     }
40 }

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

posted on 2015-10-10 15:12  fuly  阅读(2151)  评论(0编辑  收藏  举报