如何在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 SwipeRefreshLayout全部内容,希望文章能够帮你解决添加文本提示到Android SwipeRefreshLayout所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)