【问题标题】:Scroll from bottom to top does not work using SwipeRefreshLayout使用 SwipeRefreshLayout 从下到上滚动不起作用
【发布时间】:2018-01-03 02:39:01
【问题描述】:

我已经实现了SwipeRefreshLayout,它基本上工作正常。 https://developer.android.com/training/swipe/add-swipe-interface.html

当您转到列表底部并尝试滚动到顶部 SwipeRefreshLayout 刷新时,我遇到了奇怪的问题。

这不是我需要的。我只需要滚动到 List 的顶部,而不是刷新 ListView。

我的意思是,根据我们所处的位置,刷新时间应该是某种条件。对?因为当我们在 ListView 的顶部时,预期的行为是允许刷新。

请帮忙。

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <SearchView
        android:id="@+id/editSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
        android:queryHint="Input the number"
        />
<ListView
    android:paddingTop="5dp"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"   />

</LinearLayout>

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

还有代码

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_fragment_two, container, false);

        swiperefresh = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
        swiperefresh.setColorScheme(R.color.colorPrimary, R.color.colorAccent, R.color.colorPrimaryDark, R.color.colorPrimary);
        swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                swiperefresh.setRefreshing(true);
                Log.d("Swipe", "Refreshing Number");                 
                // Get data from WebAPI or database cache
            }
        });

        return view;
    }

【问题讨论】:

    标签: java android swiperefreshlayout


    【解决方案1】:

    嗯...最后我在这里找到了解决方案Scroll up does not work with SwipeRefreshLayout in Listview

    所以我的布局不会改变,但代码是的。

    我们必须做以下事情

    1. 像 public 一样实现 AbsListView.OnScrollListener class FragmentTwo extends ListFragment implements SearchView.OnQueryTextListener, AbsListView.OnScrollListener

    2. 添加实现接口AbsListView.OnScrollListener的两个方法 喜欢这里

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
    
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    
                swiperefresh.setEnabled(firstVisibleItem == 0);
    }
    

    这里的关键是
    swiperefresh.setEnabled(firstVisibleItem == 0);

    implements OnScrollListener inside listFragment

    【讨论】:

      【解决方案2】:

      这个库允许您在上拉和下拉之间进行选择:

      https://github.com/chrisbanes/Android-PullToRefresh

      上拉刷新 默认情况下,这个库设置为下拉刷新,但如果你想允许上拉刷新,那么你可以这样做。您甚至可以使用“两者”设置将视图设置为启用上拉和下拉。有关如何设置的更多信息,请参阅自定义页面。

      我认为 SwipeRefreshLayout 不允许这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多