其他分享
首页 > 其他分享> > 添加文本提示到Android SwipeRefreshLayout

添加文本提示到Android SwipeRefreshLayout

作者:互联网

如何在listView的顶部添加提示,如“下拉到刷新”,它包含在android.support.v4的swipeRefreshLayout中.

下拉刷新有效但我想在用户稍微拉下listview时添加文本.

解决方法:

编辑10/21/2014

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

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

我们的想法是添加一个自定义ViewGroup,当SwipeRefreshLayout子项被拉下时,它会增加其高度.在这个ViewGroup中,您将提供所需的一切提示(TextViews,ImageViews,…).

我选择扩展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);

完成!

标签:android,listview,swiperefreshlayout
来源: https://codeday.me/bug/20190725/1530546.html