安卓棒棒糖 - 下拉刷新

26

我正在尝试在Android中实现下拉刷新功能。我知道有SwipeRefreshLayout,但是在像Drive这样的全新设计的Google应用程序(见附件)中,我注意到在拉动时出现了一个新的刷新图标。我尝试在网上查找,但徒劳无功。

编辑:有些人指出这是如何实现下拉刷新的重复问题。这不是同一个问题。如果您仔细阅读问题,就会发现这一点。

输入图片说明


5
已发布。SwipeRefreshLayout 开箱即用地拥有这个漂亮的按钮。 - Pavel Dudka
可能是重复的问题:如何实现Android下拉刷新 - Marian Paździoch
@MarianPaździoch 如果你仔细看的话,它并不是。 - gsb
5个回答

60

这是 SwipeRefreshLayout 。支持库的版本21中包含了它,替代了旧样式。


19
  1. 下载最新的棒棒糖(Lollipop)SDK和Extras/Android支持库
  2. 将项目的构建目标设置为Android 5.0(否则支持包可能会出现与资源相关的错误)
  3. 将libs/android-support-v4.jar更新到第21个版本
  4. 使用android.support.v4.widget.SwipeRefreshLayout以及android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener

详细指南可以在此处找到:http://antonioleiva.com/swiperefreshlayout/

另外,对于ListView,我建议阅读一下评论中的canChildScrollUp();)


7

我最喜欢的是这个指南,它非常易懂:https://www.bignerdranch.com/blog/implementing-swipe-to-refresh/

  1. Add the following to gradle:

    compile 'com.android.support:support-v4:22.2.0'

  2. Add the swipe to refresh to your layout - put in listview or recyclerview in the middle of the swiperefreshlayout:

        <ListView
            android:id="@+id/activity_main_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </ListView>
    
    </android.support.v4.widget.SwipeRefreshLayout>
    
  3. Add in your code for the mainactivity:

    public class MainActivity extends Activity {
    
    ListView mListView;
    SwipeRefreshLayout mSwipeRefreshLayout;
    Adapter mAdapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.acivity_main);
      SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
      mListView = findViewById(R.id.activity_main_list_view);
      mListView.setAdapter(new ArrayAdapter<String>(){
      String[] fakeTweets = getResources().getStringArray(R.array.fake_tweets);
      mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fakeTweets)
      listView.setAdapter(mAdapter);
    });
    }
    

    }

  4. Don't forget to call mSwipeRefreshLayout.setRefreshing(false); once your refreshing ends.


将链接中的一些代码添加到程序中,以避免在将来无法访问该链接时出现错误。 - N J

4

链接已经失效。通常仅提供链接来解决问题并不是一个好主意。 - Jean-Francois T.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接