简体   繁体   English

Android Swipe Refresh Layout 向上滚动刷新

[英]Android Swipe Refresh Layout scroll up to refresh

I've implemented a swipe refresh layout for my grid view and it works perfectly if I pull (scroll) downwards for the refresh to be called.我已经为我的网格视图实现了滑动刷新布局,如果我向下拉(滚动)以调用刷新,它可以完美地工作。

However, I'm trying to figure out how to refresh when the user scrolls up.但是,我试图弄清楚当用户向上滚动时如何刷新。 So when the user reaches the last of the items in the grid (my limit is 10) so when the user sees all ten and then pulls upwards or tries to continue to scroll how can I then refresh?因此,当用户到达网格中的最后一个项目(我的限制是 10)时,当用户看到所有十个然后向上拉或尝试继续滚动时,我该如何刷新?

Any help is appreciated.任何帮助表示赞赏。

See my answer in stackoverflowstackoverflow中查看我的答案

SwipeRefreshLayout from Android Support Library version 21 does not support pull from bottom. Android 支持库版本 21 中的 SwipeRefreshLayout 不支持从底部拉取。 I have made modification SwipeRefreshLayoutBottom with is based on original SwipeRefreshLayout code.我已经根据原始 SwipeRefreshLayout 代码进行了修改 SwipeRefreshLayoutBottom 。 It is fully based on original Google code with just inversion of coordinates and newly writed canChildScrollDown method.它完全基于原始的 Google 代码,只需反转坐标和新编写的 canChildScrollDown 方法。 All modification are marked as TODO.所有修改都标记为 TODO。

Bitbucker repository Bitbucker 存储库

so people either a: don't know the answer to this question or b: they're not willing to share the knowledge.所以人们要么a:不知道这个问题的答案,要么b:他们不愿意分享知识。

Anyway after a long time I came up with my own solution.无论如何,经过很长时间,我想出了自己的解决方案。 I added a scroll listener to my grid, and calculated when I scrolled to the bottom of the screen, at this point I recall the onRefresh() method from the swipe to refresh...我在我的网格中添加了一个滚动侦听器,并在我滚动到屏幕底部时进行了计算,此时我从滑动中调用了 onRefresh() 方法以刷新...

EDIT to add further info编辑以添加更多信息

So, after I did some research to calculate when the user reached the bottom of a grid view I followed questions like this and others to get that.所以,在我做了一些研究来计算用户何时到达网格视图的底部之后,我遵循了这样的问题和其他问题来得到那个。 Below is my code that I had to alter in order have it work with the native refreshing functionality.下面是我的代码,我必须修改它才能使其与本机刷新功能一起使用。 My alterations were to prevent constant refreshing.我的改变是为了防止不断刷新。 So included in my if statement is a check that it's not currently refreshing, so when I reached the bottom it refreshed, but I didn't have that check so it constantly refreshed.所以在我的 if 语句中包含一个检查它当前没有刷新,所以当我到达底部时它刷新了,但我没有那个检查,所以它不断刷新。 I then had to do a check against the total amount of items in my grid, as when the user reached the last item (very last item) the app also kept refreshing and there was nothing to break the cycle.然后我必须检查网格中的项目总数,因为当用户到达最后一个项目(最后一个项目)时,应用程序也不断刷新,没有什么可以打破循环。 Hopefully this is of some help...希望这会有所帮助...

        worldBeersGrid.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {

        }
        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            int position = firstVisibleItem+visibleItemCount;
            int limit = totalItemCount;
            int totalItems = sharedpreferences.getInt("WORLD ITEM COUNT", 0);

            Log.i("TOTAL WORLD ITEMS", String.valueOf(totalItems));
            Log.i("LIMIT :::::::::::: ", String.valueOf(limit));
            Log.i("POSITION ::::::::::::", String.valueOf(position));
            Log.i("REFRESHING :::::::::::::::", String.valueOf(swipeRefreshLayout.isRefreshing()));

            if(position>=limit && totalItemCount>0 && !swipeRefreshLayout.isRefreshing() && position <totalItems){
                swipeRefreshLayout.setRefreshing(true);
                //In the below method I made my call to my ws...
                onRefresh();
            }
        }
    });

You try this way你试试这个方法

listView.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    boolean enable = false;
    if(listView != null && listView.getChildCount() > 0){
        // check if the first item of the list is visible
        boolean firstItemVisible = listView.getFirstVisiblePosition() == 0;
        // check if the top of the first item is visible
        boolean topOfFirstItemVisible = listView.getChildAt(0).getTop() == 0;
        // enabling or disabling the refresh layout
        enable = firstItemVisible && topOfFirstItemVisible;
    }
    swipeRefreshLayout.setEnabled(enable);
}});

Another answer if anybody is still interested.如果有人仍然感兴趣,另一个答案。 In order to refresh when scrolling down, I've implemented the onRefreshListerner() on the swipeRefreshLayout.为了在向下滚动时刷新,我在 swipeRefreshLayout 上实现了 onRefreshListerner()。

    swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipe_messages);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(true);

            ( new Handler()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    refresh();
                }
            }, 1000);
        }
    });

But in order to refresh when scrolling up and only when I've got to the end of the list, I've implemented OnTouchListener() on the list which includes the swipeRefreshLayout.setRefreshing(true) to start the effect:但是为了在向上滚动时刷新并且只有当我到达列表末尾时,我已经在列表上实现了 OnTouchListener() ,其中包括 swipeRefreshLayout.setRefreshing(true) 来启动效果:

    listView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() -1 &&
                        listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight())
                {

                    swipeRefreshLayout.setRefreshing(true);

                    ( new Handler()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            refresh();
                        }
                    }, 1000);

                }
            }
            return false;
        }
    });

Of course you have to disable the swipeRefreshLayout after updating your list:当然,您必须在更新列表后禁用 swipeRefreshLayout:

public void refresh (){
    //Do something neat here to refresh your list
    swipeRefreshLayout.setRefreshing(false);
}

I've used this answer to know when I've got to the end of the list:我用这个答案知道我什么时候到了列表的末尾:

https://stackoverflow.com/a/10235887/6144027 https://stackoverflow.com/a/10235887/6144027

I have another way:我有另一种方式:

binding.contentListScroll.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                if (!binding.contentListScroll.canScrollVertically(1))
                {
                    binding.swiperefresh.setRefreshing(true);
                    YourRefreshMethod();
                }
            }
            return false;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM