在Android SwipeRefreshLayout中添加文本提示

3
如何在一个来自android.support.v4的swipeRefreshLayout中的listView上方添加提示,比如“下拉以刷新”?
下拉刷新已经能够使用,但我想要添加一个文本,当用户稍微向下滑动listView时出现。

我想要和这个一样的东西... - Zala Janaksinh
这个问题有任何更新吗? - persianLife
1个回答

8

2014年10月21日更新

如果你将support-v4更新到最新版本(至少21.0.0),则可以使用内置的加载指示器!


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

这个想法是添加一个自定义ViewGroup,当SwipeRefreshLayout子项被向下拉时,它会增加其高度。在此ViewGroup中,您将放置需要用于提示的所有内容(TextView、ImageView等)。

我选择扩展RelativeLayout,因为它更容易定位“提示”元素。

1)创建以下自定义小部件:

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) 在您的Fragment或Activity布局中使用此自定义小部件。

<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)然后,在您的Activity onCreate()或Fragment onCreateView()方法中添加以下代码:

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

完成!


此外,如果文本能够像Kitkat中的股票电子邮件应用程序一样显示在操作栏中而不是其下方,那就太好了。 - Shantanu Paul
真是太聪明了!我唯一改变的是 SwipeRefreshHintLayout 扩展了 LinearLayout,因为相对布局对我没用。谢谢。 - Rotem
@araks,我在onRefresh()中设置文本,但只有当我将SwipeRefreshLayout拉到阈值距离并调用onRefresh()时才会设置文本。我想在SwipeRefreshLayout开始拖动时设置。 - Umesh
@Umesh,你可以在SwipeRefreshHintLayout类中的onPreDraw方法中完成它: 如果(!oldBounds.equals(newBounds)){ // 在这里设置文本 ... } 你也可以使用一个布尔变量作为标志,在onDraw方法中只设置一次文本,以避免多次设置文本! - araks
为了与内置指示器一起使用,请将 getLayoutParams().height = newBounds.height(); 更改为 if (newBounds.height() < 0) { getLayoutParams().height = 0; } else { getLayoutParams().height = newBounds.height(); } - Thijs
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接