ListView动态加载数据模板(使用xml布局)

继上一篇ListView动态加载数据模板(使用代码布局),笔者想利用xml布局文件实现一下,因为布局文件在xml文件中实现要规范一些,原理和之前那一篇是一样的,直接来代码

主布局文件other_listview.xml,注意ListView定义id的方式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<ListView
android:id="@android:id/list"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
/>
</LinearLayout>

ListView里面每个Item的布局文件other_listview_item.xml,里面的ImageView使用的图片自己可以随便替换一张

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:scaleType
="fitXY"
android:src
="@drawable/avira_antyvir"/>
<TextView
android:id="@+id/tv"
android:layout_width
="fill_parent"
android:layout_height
="20dp"
android:text
="@string/hello"
/>
</LinearLayout>

再来个布局文件other_listview_footer_more.xml,这个文件就是ListView最下面那个View,当点击Button的时候显示进度条

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<Button
android:id="@+id/button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="更多"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:gravity
="center"
android:id
="@+id/linearlayout">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height
="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:text
="正在获取..."/>
</LinearLayout>
</LinearLayout>

类文件OtherListView.java

package com.focus.loading;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class OtherListView extends ListActivity implements OnScrollListener {

ListView list;
int scrollState;
int count = 40;
int lastItem;
int visibleItemCount;

Button footerButton;
LinearLayout footerProgressBarLayout;
View view;
ListAdapter listAdapter
= new ListAdapter();

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

LayoutInflater inflater
= LayoutInflater.from(this);
view
= inflater.inflate(R.layout.other_listview_footer_more, null);
footerButton
= (Button) view.findViewById(R.id.button);
footerProgressBarLayout
= (LinearLayout) view
.findViewById(R.id.linearlayout);
footerProgressBarLayout.setVisibility(View.GONE);
footerButton.setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {
if (lastItem == listAdapter.count
&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
footerButton.setVisibility(View.GONE);
footerProgressBarLayout.setVisibility(View.VISIBLE);
if (listAdapter.count <= count) {
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
listAdapter.count
+= 10;
listAdapter.notifyDataSetChanged();
// list.setSelection(lastItem);
footerButton.setVisibility(View.VISIBLE);
footerProgressBarLayout
.setVisibility(View.GONE);
}
},
2000);
}

}
}
});

list
= getListView();
list.addFooterView(view);
list.setAdapter(listAdapter);
list.setOnScrollListener(
this);
}

class ListAdapter extends BaseAdapter {

int count = 10;

@Override
public int getCount() {
return count;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater
= LayoutInflater.from(OtherListView.this);
View view
= inflater.inflate(R.layout.other_listview_item, null);
TextView tv
= (TextView) view.findViewById(R.id.tv);
tv.setText(
"Hello World " + position);
return view;
}

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.visibleItemCount = visibleItemCount;
lastItem
= firstVisibleItem + visibleItemCount - 1;
System.out.println(listAdapter.count);
if (listAdapter.count >= count) {
list.removeFooterView(view);
}
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;

}
}

由于跟之前那篇原理一样的,所以没写注释,不懂的先看前面那篇吧http://www.cnblogs.com/and_he/archive/2011/05/30/2063230.html

效果图

posted @ 2011-05-30 16:15  And.He  阅读(8157)  评论(4编辑  收藏  举报