2

我知道 iPhone 中提供了类似 pullToRefresh 的功能,而对于 Android,我们必须手动管理它。

我得到了一些具有 pullToRefresh 的示例,但它仅适用于 ListView。

就我而言,我想为 Scrollview 实现。Google Play 的 DrawFree 应用程序中提供了 PullToRefresh 之类的东西。

请看下图:

在此处输入图像描述

那么,如何实现呢?

4

2 回答 2

12

这是在 ListView、GridView、WebView、Expandable ListView 中实现拉取刷新的一个很好的例子。

您可以使用此示例并根据您的视图进行更改。

https://github.com/chrisbanes/Android-PullToRefresh

于 2012-07-09T08:58:55.680 回答
4

[编辑:我的解决方案类似于 Gmail,如果它不是您想要的,我很抱歉,无论如何我发布的代码可能对其他人有用]

我刚刚完成,我已经按照Joe Dailey 编写的示例在 ListView 中实现了它(非常好且简单)。然后我重新访问它以与 ScrollView 一起使用。

这就是我所做的:

我为 ScrollView 设置了一个 onTouchListener;

我控制滚动视图是否在顶部(scrollView.getScrollY()== 0)然后,我使用“lastY = startY”来了解我是向下滚动还是向上滚动(两个变量都是活动字段)。

“act.refresh()”是运行任务以从我的服务器获取数据的方法。

在你的 asynkTask 的 onPostExecute 方法中,或者如果你使用 handlers 在 Handler 中,你调用“finishRefresh()”方法;

这是 onTouchListener:

class RefreshTouchListener implements View.OnTouchListener {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ScrollView scroll = (ScrollView) v;
        if (scroll.getScrollY() == 0) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startY = event.getY();
                lastY = startY;
                break;
            case MotionEvent.ACTION_MOVE:
                if (!refreshing && event.getY() > lastY) {

                    lastY = event.getY();
                    if (event.getY() - startY <= dragLength) {
                        double percent = 1 - (event.getY() - startY) / dragLength;
                        double weight;
                        weight = 2 * Math.pow(percent, 0.8);
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) progress.getLayoutParams();
                        params.weight = (float) weight;
                        progress.setLayoutParams(params);
                        progress.setIndeterminate(false);
                        progress.setPadding(0, 0, 0, 0);
                        return true;
                    } else {
                        refreshing = true;

                        act.refresh();
                        startY = 100000f;
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) progress.getLayoutParams();
                        params.weight = 0;
                        progress.setIndeterminate(true);
                        progress.postInvalidate();
                        progress.setLayoutParams(params);
                    }
                }
            case MotionEvent.ACTION_UP:
                startY = 100000f;
                Log.i(TAG, "action up " + event.getY());
                if (!refreshing) {
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) progress.getLayoutParams();
                    params.weight = 2;
                    progress.setLayoutParams(params);
                }
            }
        }
        return false;
    }
}

这是 finishRefresh() 方法:

public void finishRefresh() {
    progress.setIndeterminate(false);
    progress.postInvalidate();
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) progress.getLayoutParams();
    params.weight = 2;
    progress.setLayoutParams(params);
    refreshing = false;
}

这是生成布局的代码,带有 progressBar 和其他所有内容:

private void createProgressBarLayout() {
    topMargin = -Math.round(6 * act.metrics.density);
    dragLength = Math.round(act.screen_size.y / 2.5f);

    LinearLayout top = new LinearLayout(this);
    top.setGravity(Gravity.TOP);
    top.setOrientation(LinearLayout.HORIZONTAL);

    content_rel_layout = (RelativeLayout) findViewById(R.id.rel_layout_name);
    content_rel_layout.addView(top);
    ViewGroup.LayoutParams topParams = top.getLayoutParams();
    topParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
    topParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    top.setLayoutParams(topParams);

    FrameLayout left = new FrameLayout(this);
    progress = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
    progress.setProgress(100);
    progress.setIndeterminate(false);
    // progress.setBackgroundResource(R.drawable.progress_bar);
    FrameLayout right = new FrameLayout(this);

    top.addView(left);
    top.addView(progress);
    top.addView(right);

    LinearLayout.LayoutParams leftParams = (LinearLayout.LayoutParams) left.getLayoutParams();
    leftParams.weight = 1;
    leftParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
    leftParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    leftParams.topMargin = topMargin;
    left.setLayoutParams(leftParams);

    LinearLayout.LayoutParams progressParams = (LinearLayout.LayoutParams) progress.getLayoutParams();
    progressParams.weight = 2;
    progressParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
    progressParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    progressParams.topMargin = topMargin;

    progress.setLayoutParams(progressParams);

    LinearLayout.LayoutParams rightParams = (LinearLayout.LayoutParams) right.getLayoutParams();
    rightParams.weight = 1;
    rightParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
    rightParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    rightParams.topMargin = topMargin;

    right.setLayoutParams(rightParams);

    ScrollView sv = (ScrollView) findViewById(R.id.prof_mon4_vert_scroll);
    sv.setOnTouchListener(new RefreshTouchListener());
}

如有任何疑问,请随时问我!享受

于 2013-11-02T21:38:23.100 回答