添加文本提示到Android SwipeRefreshLayout [英] Adding text hint to Android SwipeRefreshLayout

查看:479
本文介绍了添加文本提示到Android SwipeRefreshLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ListView的顶部添加一个提示,如下拉刷新这是从android.support.v4包含在swipeRefreshLayout。

How do I add a hint on the top of a listView like "Pull down to refresh" which is contained in a swipeRefreshLayout from android.support.v4.

在下拉刷新的作品,但我想添加一个文本每当用户拉列表视图小幅回落。

The pull down to refresh works but I want to add a text whenever the user pulls the listview slightly down.

推荐答案

编辑2014年10月21日


EDIT 10/21/2014

如果您更新的支持-V4到最新版本(至少21.0.0),可以使用内置的负载指示!

If you update the support-v4 to the latest version (at least 21.0.0) you can use the built-in loading indicator!

我刚刚想出了一个简单而有效的解决方案。

I just came up with a simple, yet effective, solution.

这个想法是添加,生长高度时SwipeRefreshLayout孩子被推倒了一个自定义的ViewGroup。在这个ViewGroup中你会把你需要为你提示(TextViews,ImageViews,......)应有尽有。

The idea is to add a custom ViewGroup that grows its height when the SwipeRefreshLayout child gets pulled down. In this ViewGroup you will put everything you need for your hint (TextViews, ImageViews, ...).

我选择,因为它使更容易地定位你的暗示的元素来扩展RelativeLayout的。

I chose to extend a RelativeLayout because it makes easier to position your "hint" elements.

1)创建一个自定义部件如下:

1) Create a custom widget as follows:

public class SwipeRefreshHintLayout extends RelativeLayout {

    public SwipeRefreshHintLayout(Context context) {
        super(context);
    }

    public SwipeRefreshHintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SwipeRefreshHintLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setSwipeLayoutTarget(final SwipeRefreshLayout swipeRefreshLayout) {

        final View swipeTarget = swipeRefreshLayout.getChildAt(0);
        if (swipeTarget == null) {
            return;
        }

        swipeTarget.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            private Rect oldBounds = new Rect(), newBounds = new Rect();

            @Override
            public boolean onPreDraw() {
                newBounds.set(swipeTarget.getLeft(), swipeRefreshLayout.getTop(), swipeTarget.getRight(), swipeTarget.getTop());

                if (!oldBounds.equals(newBounds)){
                    getLayoutParams().height = newBounds.height();
                    requestLayout();
                    oldBounds.set(newBounds);
                }
                return true;
            }
        });

    }

}

2)在您的片段或活动布局使用自定义的部件。

2) In your Fragment or Activity layout use this custom widget.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.widget.SwipeRefreshHintLayout
        android:id="@+id/swipe_hint"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/label_swipe_to_refresh"/>

        <!-- Any other view positioned using RelativeLayout rules -->

    </com.example.widget.SwipeRefreshHintLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

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

</RelativeLayout>

3)然后,在你活动的onCreate(),或在您的片段onCreateView(),把这些行:

3) Then, in your Activity onCreate() or in your Fragment onCreateView(), put those lines:

mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
mSwipeRefreshHintLayout = (SwipeRefreshHintLayout) rootView.findViewById(R.id.swipe_hint);
mSwipeRefreshHintLayout.setSwipeLayoutTarget(mSwipeRefreshLayout);

完成!

这篇关于添加文本提示到Android SwipeRefreshLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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