首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向Android SwipeRefreshLayout添加文本提示

向Android SwipeRefreshLayout添加文本提示
EN

Stack Overflow用户
提问于 2014-06-07 11:43:38
回答 1查看 3.5K关注 0票数 3

如何在listView顶部添加一个提示,如“下拉到刷新”,这包含在来自android.support.v4的swipeRefreshLayout中。

下拉显示可以刷新,但是每当用户稍微向下拉下列表视图时,我想添加一个文本。

EN

回答 1

Stack Overflow用户

发布于 2014-07-23 16:07:10

2014年10月21日编辑

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

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

这样做的目的是添加一个自定义ViewGroup,当SwipeRefreshLayout子节点被拉下时,它的高度会增长。在这个ViewGroup中,您将把提示所需的一切(TextViews,ImageViews,.)放入其中。

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

1)创建一个自定义小部件,如下所示:

代码语言:javascript
复制
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)在您的片段或活动布局中使用此自定义小部件。

代码语言:javascript
复制
<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()中放置以下行:

代码语言:javascript
复制
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
mSwipeRefreshHintLayout = (SwipeRefreshHintLayout) rootView.findViewById(R.id.swipe_hint);
mSwipeRefreshHintLayout.setSwipeLayoutTarget(mSwipeRefreshLayout);

完成了!

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24096793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档