android pull,listview - How to implement Android Pull-to-Refresh - Stack Overflow

I have very easy way to do this but now sure its the foolproof way

There is my code

PullDownListView.java

package com.myproject.widgets;

import android.content.Context;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.widget.AbsListView;

import android.widget.AbsListView.OnScrollListener;

import android.widget.ListView;

/**

* @author Pushpan

* @date Nov 27, 2012

**/

public class PullDownListView extends ListView implements OnScrollListener {

private ListViewTouchEventListener mTouchListener;

private boolean pulledDown;

public PullDownListView(Context context) {

super(context);

init();

}

public PullDownListView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public PullDownListView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}

private void init() {

setOnScrollListener(this);

}

private float lastY;

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

if (ev.getAction() == MotionEvent.ACTION_DOWN) {

lastY = ev.getRawY();

} else if (ev.getAction() == MotionEvent.ACTION_MOVE) {

float newY = ev.getRawY();

setPulledDown((newY - lastY) > 0);

postDelayed(new Runnable() {

@Override

public void run() {

if (isPulledDown()) {

if (mTouchListener != null) {

mTouchListener.onListViewPulledDown();

setPulledDown(false);

}

}

}

}, 400);

lastY = newY;

} else if (ev.getAction() == MotionEvent.ACTION_UP) {

lastY = 0;

}

return super.dispatchTouchEvent(ev);

}

@Override

public void onScroll(AbsListView view, int firstVisibleItem,

int visibleItemCount, int totalItemCount) {

setPulledDown(false);

}

@Override

public void onScrollStateChanged(AbsListView view, int scrollState) {

}

public interface ListViewTouchEventListener {

public void onListViewPulledDown();

}

public void setListViewTouchListener(

ListViewTouchEventListener touchListener) {

this.mTouchListener = touchListener;

}

public ListViewTouchEventListener getListViewTouchListener() {

return mTouchListener;

}

public boolean isPulledDown() {

return pulledDown;

}

public void setPulledDown(boolean pulledDown) {

this.pulledDown = pulledDown;

}

}

You just need to implement ListViewTouchEventListener on your activity where you want to use this ListView and set the listener

I have it implemented in PullDownListViewActivity

package com.myproject.activities;

import android.app.Activity;

import android.os.Bundle;

/**

* @author Pushpan

*

*/

public class PullDownListViewActivity extends Activity implements ListViewTouchEventListener {

private PullDownListView listView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

listView = new PullDownListView(this);

setContentView(listView);

listView.setListViewTouchListener(this);

//setItems in listview

}

public void onListViewPulledDown(){

Log.("PullDownListViewActivity", "ListView pulled down");

}

}

It works for me :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值