【问题标题】:How to get SwipeRefreshLayout to set text in the ActionBar如何让 SwipeRefreshLayout 在 ActionBar 中设置文本
【发布时间】:2014-08-15 14:55:28
【问题描述】:

当查看 PlayNewsstand、QuidCo、OneDrive 和 YahooMail 等应用程序时,当出现向下滑动以刷新手势时,操作栏的内容会更改以指示向下滑动正在导致刷新。然而,GMail 不会更新操作栏的内容,只是依靠 progressBar 来指示刷新。

使用新的 SwipeRefreshLayout 如何更新 ActionBar 内容以像 PlayNewsstand 之类的应用一样运行。

我已经看到很多对 Chris Banes 的 ActionBar-PulLToRefresh 的引用,它支持更新 ActionBar,但这是在 SwipeRefreshLayout 之前编写的,因此不是如何获取wipeRefreshLayout 以更新 ActionBar 文本的示例

【问题讨论】:

    标签: android android-actionbar swiperefreshlayout


    【解决方案1】:

    你可以这样做

    swipeLayout.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    Log.i("INFO", "MOVE");
                    getActivity().getActionBar().setTitle("Swipe to refresh");
                    getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
                    getActivity().getActionBar().setCustomView(R.layout.custom_title);
                    getActivity().getActionBar().setDisplayUseLogoEnabled(false);
                }
                return false;
            }
        });
    

    custom_title.xml - 使文本居中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="my Title"
        android:id="@+id/mytext"
        android:textSize="18sp" />
    </LinearLayout>
    

    在你的 onRefresh 结束或不再听触摸时将标题和徽标设置回来。

    【讨论】:

    • 太棒了,谢谢。如果我希望操作栏只包含我的文本,我必须隐藏操作栏项目,但这给了我一个很好的起点,谢谢。
    • @SarahMaslin 如果它解决了您的问题,请将其标记为正确答案! :)
    • @user2855036,有没有办法在更改之前以一种简单的方式保存操作栏属性,然后在刷新完成后,我可以将操作栏设置回旧的。
    • @HanHe 只是将属性保存在局部变量中
    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    相关资源
    最近更新 更多