滚动到底部加载更多及下拉刷新listview的使用

发布时间:2013-08-25 17:24:07   来源:文档文库   
字号:

滚动到底部加载更多及下拉刷新listview的使用

本文主要介绍可同时实现下拉刷新及滑动到底部加载更多的ListView的使用。

ListView优点包括:a. 可自定义下拉响应事件(如下拉刷新) b.可自定义滚动到底部响应的事件(如滑动到底部加载更多) c.可自定义丰富的样式 d.高效(若下拉样式关闭不会加载其布局,同listView效率一致) e. 丰富的设置。

1、引入公共库

引入TrineaAndroidCommon@GoogleCode作为你项目的library,或是自己抽取其中的DropDownListView部分使用

2、在layout中定义

将布局中的ListView标签换成cn.trinea.android.common.view.DropDownListView标签

并加上自定义属性的命名空间xmlns:listViewAttr="http://schemas.android.com/apk/res/cn.trinea.android.demo",其中cn.trinea.android.demo需要用自己的包名替换。如何自定义属性及其命名空间可见本文最后。xml代码如下:

Java代码

xmlns:listViewAttr="http://schemas.android.com/apk/res/cn.trinea.android.demo"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/list_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:drawSelectorOnTop="false"

android:paddingBottom="@dimen/dp_40"

listViewAttr:isDropDownStyle="true"

listViewAttr:isOnBottomStyle="true"

listViewAttr:isAutoLoadOnBottom="true" />

DropDownListView自定义了三个boolean属性

isDropDownStyle表示是否允许下拉样式,java代码中可自定义下拉listener,表示需要完成的任务

isOnBottomStyle表示是否允许底部样式,java代码中可自定义滚动到底部的listener,表示需要完成的任务

isAutoLoadOnBottom表示是否允许滚动到底部时自动执行对应listener,仅在isOnBottomStyletrue时有效

PS:如果isDropDownStyleisOnBottomStylefalse,并不会加载对应的布局,所以性能同ListView一样

3、在Java类中调用

通过setOnDropDownListener设置下拉的事件,不过需要在事件结束时手动调用onDropDownComplete恢复状态

通过setOnBottomListener设置滚动到底部的事件,不过需要在事件结束时手动调用onBottomComplete恢复状态,示例代码如下:

Java代码

/**

* DropDownListViewDemo

*

* @author Trinea 2013-6-1

*/

public class DropDownListViewDemo extends BaseActivity {

private LinkedList listItems = null;

private DropDownListView listView = null;

private Adapter adapter;

private String[] mStrings = { "Aaaaaa", "Bbbbbb", "Cccccc", "Dddddd", "Eeeeee",

"Ffffff", "Gggggg", "Hhhhhh", "Iiiiii", "Jjjjjj", "Kkkkkk", "Llllll", "Mmmmmm",

"Nnnnnn", };

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState, R.layout.drop_down_listview_demo);

listView = (DropDownListView)findViewById(R.id.list_view);

// set drop down listener

listView.setOnDropDownListener(new OnDropDownListener() {

@Override

public void onDropDown() {

new GetDataTask(true).execute();

}

});

// set on bottom listener

listView.setOnBottomListener(new OnClickListener() {

@Override

public void onClick(View v) {

new GetDataTask(false).execute();

}

});

listItems = new LinkedList();

listItems.addAll(s.asList(mStrings));

adapter = new Adapter(this, android.R.layout.simple_list_item_1, listItems);

listView.setAdapter(adapter);

}

private class GetDataTask extends AsyncTask {

private boolean isDropDown;

public GetDataTask(boolean isDropDown){

this.isDropDown = isDropDown;

}

@Override

protected String[] doInBackground(Void... params) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

;

}

return mStrings;

}

@Override

protected void onPostExecute(String[] result) {

if (isDropDown) {

listItems.addFirst("Added after drop down");

adapter.notifyDataSetChanged();

// should call onDropDownComplete function of DropDownListView at end of drop down complete.

SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");

listView.onDropDownComplete(getString(R.string.update_at)

+ dateFormat.format(new Date()));

} else {

listItems.add("Added after on bottom");

adapter.notifyDataSetChanged();

// should call onBottomComplete function of DropDownListView at end of on bottom complete.

listView.onBottomComplete();

}

super.onPostExecute(result);

}

}

}

4、高级接口设置

5、样式设置(自定义headerfooter信息)

来源:清源教育

本文来源:https://www.2haoxitong.net/k/doc/296d1b9bfd0a79563c1e72ca.html

《滚动到底部加载更多及下拉刷新listview的使用.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式