Android View点击频率控制

提交数据操作

代码


/**
 * Created by wangjie on 2015/4/12
 */

public abstract class OnClickWrapperListener implements View.OnClickListener {

    private long MIN_DELAY_TIME = 500L; //default delay time

    private long lastClickTime = 0L;

    public OnClickWrapperListener() {}

    public OnClickWrapperListener(long delayTime) {
        this.MIN_DELAY_TIME = delayTime;
    }

    @Override
    public void onClick(View v) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime > MIN_DELAY_TIME) {
            lastClickTime = currentTime;
            onWrapperClick(v);
        }
    }

    public abstract void onWrapperClick(View v);
}

使用

        // delay 6000 millis
        targetView.setOnClickListener(new OnClickWrapperListener(6000L) {
            @Override
            public void onWrapperClick(View v) {
                Log.e(TAG, "- onWrapperClick 6000L-");
            }
        });

        // default 500 millis
        targetView.setOnClickListener(new OnClickWrapperListener() {
            @Override
            public void onWrapperClick(View v) {
                Log.e(TAG, "- onWrapperClick -");
            }
        });

测试

04-12 17:30:05.569 13828-13828/com.wangj.sample E/Sample: - onWrapperClick 6000L-
04-12 17:30:11.659 13828-13828/com.wangj.sample E/Sample: - onWrapperClick 6000L-
04-12 17:30:17.749 13828-13828/com.wangj.sample E/Sample: - onWrapperClick 6000L-
04-12 17:30:29.429 13828-13828/com.wangj.sample E/Sample: - onWrapperClick 6000L-

标签: none

添加新评论