如何使用SwipeRefreshLayout? [英] How to use the SwipeRefreshLayout?

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

问题描述

谷歌最近公布 的更新,以支持库 ,现在有一个新的<一个href="http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html">SwipeRefreshLayout"视图。

Google has recently published an update to its support library, which now has a new "SwipeRefreshLayout" view.

的视图允许包裹另一个视图,同时支持以执行刷新操作刷下来。

The view allows to wrap another view, while supporting swiping down in order to perform a refresh operation.

截图:

谷歌没有提供的样品(至少不是一个,我可以找到,还),所以我用它自己尝试。

Google hasn't provided a sample (at least not one that I can find, yet), so I've tried using it myself.

起初,我得到了一个崩溃(NPE),每当我刷卡,但后来我发现,那是因为我没有为它提供了一个OnRefreshListener。

At first I got a crash (NPE) whenever I swiped, but then I've found out that's because I didn't provide a "OnRefreshListener" for it.

但我还是不明白如何使用它,更不用说对其进行自定义

But I still don't get how to use it, let alone customize it

下面是布局文件的XML:

Here's the XML of the layout file:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.swiperefreshlayouttest.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />
        </LinearLayout>
    </ScrollView>

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

code,虽然它并没有做什么特别可言:

Code, though it doesn't do anything special at all:

public class MainActivity extends ActionBarActivity
  {
  @Override
  protected void onCreate(final Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final SwipeRefreshLayout swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.container);
    swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener()
      {
        @Override
        public void onRefresh()
          {
          // do nothing
          }
      });
    }
  }

问题

什么是使用这种观点的正确方法?

The question

What is the correct way to use this view?

如何自定义的呢?目前,它只是一个黑线...

How do I customize it? Currently it's just a black line...

推荐答案

不知道你正在扩展的 ActionBarActivity 类,但是我把它工作得很好用FragmentActivity

Dunno what that ActionBarActivity class you're extending is, but I got it working just fine using a FragmentActivity

public class ActivityMain extends FragmentActivity implements OnRefreshListener {

    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        super.onCreate(savedInstanceState);
    }

    @Override
    public void onRefresh() {
        Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }, 2000);
    }
}

值得指出的是我复制粘贴到你的XML布局,正是因为它是

Worth Pointing out I copy pasted your xml layout exactly as it is

在自定义方面,有真的没有什么可以做,除了通过调用setColorScheme(INT colorResId,诠释colorResId,诠释colorResId,INT colorResId)更改彩色条的颜色;

In terms of customization, there's really not much you can do other than change the color of the colored bar by calling setColorScheme(int colorResId, int colorResId, int colorResId, int colorResId);

例如。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="blue">#0099CC</color>
    <color name="purple">#9933CC</color>
    <color name="green">#669900</color>
    <color name="orange">#FF8800</color>

</resources>

mSwipeRefreshLayout.setColorScheme(R.color.blue,R.color.purple,R.color.green,R.color.orange);

<打击>这是一种令人失望的增加真的。在刷新的敏感度是相当高的,并且没有设置进行更改

修改

我写这个上课的时候​​刚刚被添加到SDK。因此,有些东西已经从当我写这个答案变了。

I wrote this when the class had just been added to the sdk. As such, some things have changed from when I wrote this answer.

setColorScheme 现在去precated, setColorSchemeResources(INT ... colorResIds)应该使用。 (只要你喜欢,你可以把尽可能多的颜色标识在那里)。

setColorScheme is now deprecated, setColorSchemeResources(int... colorResIds) should be used instead. (you can put as many color ids in there as you like).

setDistanceToTriggerSync(INT距离),也可用于设定远了用户的需要,以触发刷新刷卡。

setDistanceToTriggerSync(int distance) can also be used to set how far down a user needs to swipe in order to trigger a refresh.

我建议您检查出官方文件以看看还有什么类所提供的。

I recommend checking out the official documentation to see what else the class has to offer.

这篇关于如何使用SwipeRefreshLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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