【问题标题】:Adding text hint to Android SwipeRefreshLayout向 Android SwipeRefreshLayout 添加文本提示
【发布时间】:2014-06-07 11:43:38
【问题描述】:

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

下拉刷新工作,但我想在用户将列表视图稍微下拉时添加一个文本。

【问题讨论】:

  • 我也想要这样的东西......
  • 这个问题有什么更新吗?

标签: android listview swiperefreshlayout


【解决方案1】:

编辑 2014 年 10 月 21 日

如果您将 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);

完成!

【讨论】:

  • 不过有一个小问题。布局不会等待用户在下拉后释放列表视图,然后再开始刷新自身。只有你能纠正它。
  • 另外,如果文本可以显示在操作栏中而不是在其下方,就像 Kitkat 中的股票电子邮件应用程序一样,那就太好了。
  • 您好 Paul,对于第一个问题:刷新的自动开始在库中是硬编码的。如果你想自定义它,你必须扩展 SwipeRefreshLayout 并覆盖 onTouchEvent()。对于第二个问题:你问的有点棘手。 ActionBar 没有允许此类动画的辅助方法。使用类似于我提出的解决方案,您可以在 ActionBar 上放置一个视图并按比例为其显示行为设置动画。见[链接]stackoverflow.com/questions/15279647/…
  • 纯粹的天才!我唯一改变的是 SwipeRefreshHintLayout 扩展了 LinearLayout 因为相对布局对我不起作用。谢谢
  • @araks 感谢您的提示。但现在我想在下拉 SwipeRefreshLayout 时将文本动态设置为 TextView。请指教。
猜你喜欢
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-25
  • 2015-05-06
  • 1970-01-01
  • 2021-01-29
相关资源
最近更新 更多