安卓PullTo刷新上滚动型(Android PullTo Refresh on ScrollVie

2019-07-30 06:53发布

我知道,像pullToRefresh功能可在iPhone和Android的,我们必须manualy管理。

我的有pullToRefresh得到了一些例子,但它的工作原理上仅ListView中。

在我来说,我要实现的滚动型。 喜欢的东西PullToRefresh在DrawFree应用可在谷歌播放。

请参见下图:

因此,如何实现呢?

任何实例来实现它会非常感激。

提前致谢。

Answer 1:

这是实现拉ListView中,GridView控件,WebView中,可扩展的ListView刷新的很好的例子。

你可以用这个例子,并根据大家的意见进行修改。

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



Answer 2:

[编辑:我的解决办法是Gmail的状,我很抱歉,如果这不是你想要什么,反正我张贴,可能对别人是有用的代码]

我刚刚做了,我已经实现了它在ListView下面的例子乔戴利(非常好,简单的)编写的。 然后我重新审视它与滚动型使用。

这是我做了什么:

我设置onTouchListener的滚动型;

我控制,如果是滚动视图顶部(scrollView.getScrollY()== 0)然后,我用“lastY = startY”要知道如果我滚动向下或向上(两个变量都是活动领域)。

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

在你asynkTask的onPostExecute方法,或在处理程序,如果你使用hanlders,你叫“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;
}

这是生成布局的代码,与进度和一切:

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());
}

请随时问我的每一个疑问! 请享用



文章来源: Android PullTo Refresh on ScrollView