向下滑动动画android_Android SwipeRefreshLayout – Android向下拉动/向下滑动即可刷新

向下滑动动画android

In this tutorial we’ll discuss and implement Android Swipe Down to Refresh or Android Pull to Refresh the screen. This Android Material Design UI pattern is very commonly seen in many applications like Gmail, Facebook, Twitter and implemented using Android SwipeRefreshLayout.

在本教程中,我们将讨论并实现Android向下滑动 以刷新屏幕或Android Pull刷新屏幕。 这种Android Material Design用户界面模式在Gmail,Facebook,Twitter等许多应用程序中非常常见,并使用Android SwipeRefreshLayout实现。

Android SwipeRefreshLayout (Android SwipeRefreshLayout)

Android SwipeRefreshLayout, android pull to refresh, android swipe down to refresh screen

Android SwipeRefreshLayout is a ViewGroup that can hold only one scrollable child. It can be either a ScrollView, ListView or RecyclerView. The basic need for a SwipeRefreshLayout is to allow the users to refresh the screen manually. This is pretty common in the Facebook Newsfeed screen. Before this layout was available in the support library, we had to resort to creating and detecting custom swipe down gestures to refresh let’s say a ListView.


Android SwipeRefreshLayout是一个ViewGroup,只能容纳一个可滚动的子级。 它可以是ScrollView,ListView或RecyclerView。 SwipeRefreshLayout的基本需求是允许用户手动刷新屏幕。 这在Facebook Newsfeed屏幕中很常见。 在支持库中无法使用此布局之前,我们不得不诉诸于创建和检测自定义向下滑动手势以刷新(例如ListView)。

This class consists of one important listener that is OnRefreshListener. On swiping down this listener is triggered and the OnRefresh() method is called. We can override this method according to our needs.

此类包含一个重要的侦听器,即OnRefreshListener 。 向下滑动时,将触发此侦听器,并OnRefresh()方法。 我们可以根据需要重写此方法。

In this tutorial we’ll develop an application that consists of a ListView that on swipe down, refreshes the screen and shuffles the list rows.

在本教程中,我们将开发一个由ListView组成的应用程序,该View向下滑动,刷新屏幕并随机排列列表行。

Android SwipeRefreshLayout项目结构 (Android SwipeRefreshLayout Project Structure)

Android SwipeRefreshLayout代码 (Android SwipeRefreshLayout Code)

The activity_main.xml is given below.

下面给出activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.journaldev.swipetorefresh.MainActivity">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeToRefresh"
            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"
                >
            </ListView>

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

</RelativeLayout>

We add a ListView inside a SwipeRefreshLayout in the layout as shown above.

我们在布局中的SwipeRefreshLayout内部添加了ListView,如上所示。

The MainActivity.java class is given below.

MainActivity.java类在下面给出。

package com.journaldev.swipetorefresh;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    ArrayList arrayList = new ArrayList();
    SwipeRefreshLayout mSwipeRefreshLayout;
    ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh);
        mListView = (ListView) findViewById(R.id.listView);

        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);

        arrayList.add("First Element");
        arrayList.add("Second Element");
        arrayList.add("Third Element");
        arrayList.add("Fourth Element");
        arrayList.add("Fifth Element");

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
        mListView.setAdapter(adapter);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shuffle();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    public void shuffle(){
        Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
        mListView.setAdapter(adapter);
    }
}
  • In the above code we’ve created an ArrayList of strings and add it to the ArrayAdapter object that’s later set onto the ListView.

    在上面的代码中,我们创建了一个字符串ArrayList并将其添加到ArrayAdapter对象中,该对象随后设置到ListView上。
  • We’ve added a shuffle method that shuffles the whole ArrayList every time the onRefresh() is called.

    我们添加了一个shuffle方法,该方法在每次调用onRefresh()时都会对整个ArrayList进行重组
  • We’ve used the Collections framework method to randomly shuffle the ArrayList by setting a random seed as the current time in milli seconds.

    我们已经使用Collections框架方法通过将随机种子设置为当前时间(以毫秒为单位)来随机地对ArrayList进行随机排序。
  • setRefreshing(false) is an important line of code. It notifies the SwipeRefreshLayout instance that the refreshing is completed and it should stop the refreshing loader animation.

    setRefreshing(false)是重要的代码行。 它通知SwipeRefreshLayout实例刷新已完成,并且应停止刷新加载器动画。
  • The default refreshing animation color is set to black. We can change it using the method setColorSchemeResources()

    默认的刷新动画颜色设置为黑色。 我们可以使用setColorSchemeResources()方法更改它

The output of the application in action is given below.

android pull to refresh example

实际应用程序的输出如下。

This brings an end to this tutorial. You can download the Android SwipeRefreshLayout project from the link below and play around with different ways you can refresh the android screen on pull down.

本教程到此结束。 您可以从下面的链接下载Android SwipeRefreshLayout项目,并以不同的方式播放,以在下拉菜单中刷新android屏幕。

翻译自: https://www.journaldev.com/10708/android-swiperefreshlayout-pull-swipe-refresh

向下滑动动画android

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值