GridView基于pulltorefresh实现下拉刷新 上拉加载更多功能,android开发入门与实战网盘

holder.tvTitle = (TextView)convertView.findViewById(R.id.title_tv);

convertView.setTag(holder);

} else {

holder = (ChildHolderOne) convertView.getTag();

}

return convertView;

}

class ChildHolderOne {

TextView tvTitle;

}

}

PullToRefreshGridView

package com.example.refreshgridview;

import android.content.Context;

import android.util.AttributeSet;

import android.view.ContextMenu.ContextMenuInfo;

import android.view.View;

import android.widget.GridView;

public class PullToRefreshGridView extends PullToRefreshAdapterViewBase {

class InternalGridView extends GridView implements EmptyViewMethodAccessor {

public InternalGridView(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

public void setEmptyView(View emptyView) {

PullToRefreshGridView.this.setEmptyView(emptyView);

}

@Override

public void setEmptyViewInternal(View emptyView) {

super.setEmptyView(emptyView);

}

@Override

public ContextMenuInfo getContextMenuInfo() {

return super.getContextMenuInfo();

}

}

public PullToRefreshGridView(Context context) {

super(context);

}

public PullToRefreshGridView(Context context, int mode) {

super(context, mode);

}

public PullToRefreshGridView(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

protected final GridView createRefreshableView(Context context, AttributeSet attrs) {

GridView gv = new InternalGridView(context, attrs);

// Use Generated ID (from res/values/ids.xml)

gv.setId(R.id.gridview);

return gv;

}

@Override

public ContextMenuInfo getContextMenuInfo() {

return ((InternalGridView) getRefreshableView()).getContextMenuInfo();

}

}

PullToRefreshBase

package com.example.refreshgridview;

import android.content.Context;

import android.os.Handler;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewConfiguration;

import android.view.ViewGroup;

import android.view.animation.AccelerateDecelerateInterpolator;

import android.view.animation.Interpolator;

import android.widget.LinearLayout;

import android.widget.TextView;

/**

  • @author zlw

*/

public abstract class PullToRefreshBase extends LinearLayout {

final class SmoothScrollRunnable implements Runnable {

static final int ANIMATION_DURATION_MS = 190;

static final int ANIMATION_FPS = 1000 / 60;

private final Interpolator interpolator;

private final int scrollToY;

private final int scrollFromY;

private final Handler handler;

private boolean continueRunning = true;

private long startTime = -1;

private int currentY = -1;

public SmoothScrollRunnable(Handler handler, int fromY, int toY) {

this.handler = handler;

this.scrollFromY = fromY;

this.scrollToY = toY;

this.interpolator = new AccelerateDecelerateInterpolator();

}

@Override

public void run() {

/**

  • Only set startTime if this is the first time we’re starting, else

  • actually calculate the Y delta

*/

if (startTime == -1) {

startTime = System.currentTimeMillis();

} else {

/**

  • We do do all calculations in long to reduce software float

  • calculations. We use 1000 as it gives us good accuracy and

  • small rounding errors

*/

long normalizedTime = (1000 * (System.currentTimeMillis() - startTime))

/ ANIMATION_DURATION_MS;

normalizedTime = Math.max(Math.min(normalizedTime, 1000), 0);

final int deltaY = Math

.round((scrollFromY - scrollToY)

  • interpolator

.getInterpolation(normalizedTime / 1000f));

this.currentY = scrollFromY - deltaY;

setHeaderScroll(currentY);

}

// If we’re not at the target Y, keep going…

if (continueRunning && scrollToY != currentY) {

handler.postDelayed(this, ANIMATION_FPS);

}

}

public void stop() {

this.continueRunning = false;

this.handler.removeCallbacks(this);

}

};

// ===========================================================

// Constants

// =================

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值