在Twitter(官方应用)等Android应用中,当你遇到一个ListView时,你可以下拉它(它会在发布时反弹)来刷新内容。

我想知道,在你看来,最好的实现方式是什么?

我能想到的一些可能性:

一个在ListView顶部的项目-然而我不认为滚动回项目位置1(基于0)在ListView上的动画是一个简单的任务。 ListView之外的另一个视图-但我需要注意在它被拉时将ListView位置向下移动,我不确定我们是否可以检测到对ListView的拖动触摸是否仍然真正滚动ListView上的项。

你有什么推荐吗?

另外,我想知道官方的Twitter应用程序源代码什么时候发布。有消息说它会发布,但6个月过去了,我们一直没有听到它的消息。


当前回答

没有人提到新的类型的“拉刷新”,显示在操作栏的顶部,就像谷歌Now或Gmail应用程序。

有一个ActionBar-PullToRefresh库,它的工作原理完全相同。

其他回答

没有人提到新的类型的“拉刷新”,显示在操作栏的顶部,就像谷歌Now或Gmail应用程序。

有一个ActionBar-PullToRefresh库,它的工作原理完全相同。

我有很简单的方法来做到这一点,但现在肯定是万无一失的方法 这是我的代码 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;
    }
}

你只需要在你的活动上实现ListViewTouchEventListener,你想使用这个ListView并设置监听器

我在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");
    }
}

这对我很有用:)

注意,在Android和WP上实现时,有一些UX问题需要解决。

“为什么设计师/开发者不应该像iOS应用那样执行拉式刷新,一个很好的指标是谷歌及其团队在Android上从未使用过拉式刷新,而他们却在iOS上使用它。”

https://plus.google.com/109453683460749241197/posts/eqYxXR8L4eb

将此添加到布局中

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

现在只需在代码中添加以下这些行

 mSwipeRefreshLayout.setOnRefreshListener(() -> {

       // your refresh code here

    });

都做完了

要实现android的拉到刷新,试试这段代码,

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/pullToRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</android.support.v4.widget.SwipeRefreshLayout>

活动类:

ListView lv = (ListView) findViewById(R.id.lv);
SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) findViewById(R.id.pullToRefresh);


lv.setAdapter(mAdapter);

pullToRefresh.setOnRefreshListener(new OnRefreshListener() {

        @Override
        public void onRefresh() {
            // TODO Auto-generated method stub

            refreshContent();

        }
    });



private void refreshContent(){ 

     new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                pullToRefresh.setRefreshing(false);
            }
        }, 5000);

 }