【问题标题】:SwipeRefreshLayout doesn't show any indicator of refreshingSwipeRefreshLayout 不显示任何刷新指示
【发布时间】:2014-07-02 22:20:13
【问题描述】:

我有一个SwipeRefreshLayout 在活动中显示的片段内部使用。滑动确实会执行刷新,但在我拉下后它会立即恢复并且没有不确定进度的指示(动画进度条)。它只是消失了,但 仍然 在完成后调用 onRefresh()。这是我的布局……

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <ListView
            android:id="@+id/left_drawer"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:dividerHeight="1px"
            android:background="@color/drawer_background_gray"
            android:clipToPadding="false"/>

</android.support.v4.widget.DrawerLayout>

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"/>

    </android.support.v4.widget.SwipeRefreshLayout>

    <TextView
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/empty_text_size"
            android:layout_gravity="center"
            android:gravity="center"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:textColor="@color/brand_green"/>

    <ProgressBar
            android:id="@android:id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateOnly="true"
            android:visibility="gone"
            android:layout_gravity="center"/>

</FrameLayout>

初始化Fragment布局的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, null);
        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        return view;
}

刷新时通常会有 3 到 6 秒的延迟,这足以在查看日志时引起注意。它应该是可见的,当我使用现在已弃用的 ActionBar-PullToRefresh 库时它工作正常。我尝试将 SwipeRefreshLayout 设为 Fragment 布局的根视图,并尝试删除空文本和进度视图,但仍然无法正常工作。

在我的实际代码中还有几个步骤我无法透露,因为它是为我的公司设计的应用程序。但这只是为了为许多做类似事情的不同片段提供类似的基础,它不应该影响任何功能。

【问题讨论】:

  • 请您编辑您的帖子并包含您的SwipeRefreshLayout 实现,好吗?进度条是否有足够的时间实际显示,或者您的数据刷新速度是否过快?
  • @adneal 刚刚添加了更多细节和代码
  • 您发布或所说的内容并没有任何明显的错误,至少据我所知。您可以尝试实现一些虚拟数据以查看问题是否仍然存在。您也可以尝试调用SwipeRefreshLayout.setRefreshing(true),而不是下拉查看进度条是否会以这种方式显示。无论如何,这有点奇怪。如果您能想到其他任何事情,您绝对应该更新您的帖子。
  • @adneal 好吧,所有数据都从我公司的服务器中提取出来,但是我会尝试手动调用 setRefreshing(bool) 看看是否有效
  • @adneal 即使手动使用 setRefreshing(true) 也不起作用。这真的很奇怪。

标签: android xml view fragment swiperefreshlayout


【解决方案1】:

我使用了以下这种方法,经过多次尝试,它在没有变通办法甚至片段的情况下对我有用:

<android.support.v4.widget.SwipeRefreshLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      ** content **

    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

AppCompat 库:com.android.support:appcompat-v7:21.0.3.

【讨论】:

    【解决方案2】:

    我发现问题是我的应用在 Android 4.4 上使用半透明 UI,填充设置为列表顶部和列表底部,以便它位于操作栏下方和导航栏上方。由于 ListView 成为 SwipeRefreshLayout 的子视图,因此填充应设置为 SwipeRefreshLayout 而不是 ListView,就像我在切换到使用 SwipeRefreshLayout 之前所做的那样。

    【讨论】:

    • 你能告诉我你在哪里使用cliptopadding = false吗?我在工具栏下获得刷新材料圈,如果我向 swiperefreshlayout 添加填充,我的 recyclerView 会被剪切(即使使用 cliptopadding=false)
    【解决方案3】:
    //to enable 
    swipeRefreshLayout.post(new Runnable() {
    @Override public void run() {
        swipeRefreshLayout.setRefreshing(true);
    }});
    
    
    //to disable 
    swipeRefreshLayout.post(new Runnable() {
    @Override public void run() {
        swipeRefreshLayout.setRefreshing(true);
    }});
    
    //IF ABOVE CODE DOESN'T WORK
    //you might have made SwipeRefreshLayout as a root layout and it can be the source of bug
    //so put SwipeRefreshLayout inside any ViewGroup like RelativeLayout, LinearLayout
    

    【讨论】:

    • 这篇文章已经过时了。 :P
    【解决方案4】:

    将 swipeLayout.setColorScheme() 更改为 .setColorSchemeResources(# Your Colors Ids)

    【讨论】:

    • 那是无关的,这篇文章是旧的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 2018-02-20
    相关资源
    最近更新 更多