本文共 5149 字,大约阅读时间需要 17 分钟。
向下滑动动画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是一个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向下滑动,刷新屏幕并随机排列列表行。
The activity_main.xml
is given below.
下面给出activity_main.xml
。
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); }}
setColorSchemeResources()
默认的刷新动画颜色设置为黑色。 我们可以使用setColorSchemeResources()
方法更改它 The output of the application in action is given below.
实际应用程序的输出如下。
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屏幕。
翻译自:
向下滑动动画android
转载地址:http://ffqzd.baihongyu.com/