如何实现 Android 下拉刷新 [英] How to implement Android Pull-to-Refresh

查看:24
本文介绍了如何实现 Android 下拉刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Twitter(官方应用)等Android应用中,遇到ListView可以下拉(释放时会弹回来)刷新内容.

In Android applications such as Twitter (official app), when you encounter a ListView, you can pull it down (and it will bounce back when released) to refresh the content.

我想知道在您看来,实现这一目标的最佳方法是什么?

I wonder what is the best way, in your opinion, to implement that?

我能想到的一些可能性:

Some possibilities I could think of:

  1. ListView 顶部的一个项目 - 但是我认为在 ListView 上滚动回带有动画的项目位置 1(基于 0)并不是一件容易的事.
  2. ListView 之外的另一个视图 - 但我需要注意在拉动 ListView 时将其向下移动,而且我不确定我们是否可以检测到 ListView 的拖动触摸是否仍然真正滚动项目列表视图.

有什么建议吗?

附言我想知道官方 Twitter 应用程序源代码何时发布.之前说要发布,但是6个月过去了,一直没听说过.

P.S. I wonder when the official Twitter app source code is released. It has been mentioned that it will be released, but 6 months has passed and we haven't heard about it since then.

推荐答案

Google终于发布了pull-to-refresh库的正式版!

Finally, Google released an official version of the pull-to-refresh library!

在支持库中称为SwipeRefreshLayout,文档为此处:

It is called SwipeRefreshLayout, inside the support library, and the documentation is here:

  1. 添加 SwipeRefreshLayout 作为视图的父级,它将被视为刷新布局的拉动.(我以ListView为例,它可以是任何View,如LinearLayoutScrollView等)>

  1. Add SwipeRefreshLayout as a parent of view which will be treated as a pull to refresh the layout. (I took ListView as an example, it can be any View like LinearLayout, ScrollView etc.)

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

  • 为你的班级添加一个监听器

  • Add a listener to your class

     protected void onCreate(Bundle savedInstanceState) {
         final SwipeRefreshLayout pullToRefresh = findViewById(R.id.pullToRefresh);
         pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
             @Override
             public void onRefresh() {
                 refreshData(); // your code
                 pullToRefresh.setRefreshing(false);
             }
         });
     }
    

  • 您也可以根据需要调用 pullToRefresh.setRefresh(true/false);.

    You can also call pullToRefresh.setRefreshing(true/false); as per your requirement.

    更新

    Android 支持库已被弃用并已被 AndroidX 取代.可以在此处.

    Android support libraries have been deprecated and have been replaced by AndroidX. The link to the new library can be found here.

    此外,您需要将以下依赖项添加到您的项目中:

    Also, you need to add the following dependency to your project:

    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
    

    您可以转到 Refactor>>Migrate to AndroidX,Android Studio 将为您处理依赖项.

    You can go to Refactor>>Migrate to AndroidX and Android Studio will handle the dependencies for you.

    这篇关于如何实现 Android 下拉刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆