NestedScrollView setOnScrollChangeListener api 21

时间:2018-04-14 13:37:28

标签: android android-recyclerview android-nestedscrollview onscrolllistener

大家好我试图处理onScrolledrecyclerView内的NestedScrollView事件。{/ 1}}。

到目前为止,我找到了setOnScrollChangeListener,但是此方法是API 23,而我的目标是API 21,是否有任何想法在API 21中处理此问题?

3 个答案:

答案 0 :(得分:1)

过了一段时间我有了一个想法,我已经停止听NestedScrollView上的滚动并开始听onScroll recyclerView事件。

我设法使用此代码段

制作我的loadOnDemand nsv_posts_timeline.setOnScrollChangeListener( NestedScrollView.OnScrollChangeListener { _, scrollX, scrollY, _, oldScrollY -> if(scrollY > oldScrollY){ val totalItens = timelineAdapter.itemCount val currentView = rv_timeline.findChildViewUnder(scrollX.toFloat(), scrollY.toFloat()) val childPosition = rv_timeline.getChildAdapterPosition(currentView) if((totalItens/2) - (stepSize/2) <= childPosition && !isLoading){ isLoading = true //skip , take timelinePresenter.loadMore(totalItens, 5) } } })
isLoading

addEventListener('fetch', event => { let request = event.request var flag = false; if (request.headers.has('Cookie')) { var cookie = request.headers.get('Cookie'); pairs = cookie.split(";"); var patt = new RegExp("wordpress_logged_in.*|wp_woocommerce_session.*"); for(var i=0;i<pairs.length;i++){ if(patt.test(pairs[i])){ flag = true; break; } } console.log(flag); if (request.headers.has('Cookie') && flag) { let newHeaders = new Headers(request.headers) newHeaders.set('Cache-Control', 'no-cache') newHeaders.set('Pragma', 'no-cache') event.respondWith(fetch(request, {headers: newHeaders})) } // Use default behavior. return; } }) 是一个类属性,当我的回调从服务器

返回时,我将其更改为false

答案 1 :(得分:0)

你可以使用api 23下面的ViewTreeObserver#addOnScrollChangedListener()实现它:

def s = 'responseString:"{"session":"vvSbMInXHRJuZQ==","age":7200,"prid":"901Vjmx9qenYKw","userid":"user_1"}"'

def matcher = (s =~ /responseString:"(.*)"/)
assert matcher.matches()
def responseStr = matcher[0][1]

import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText(responseStr)
assert "user_1" ==  json.userid

答案 2 :(得分:0)

我认为您正在将setOnChangeScrollListener与View.OnScrollChangeListener一起使用。 请这样使用NestedScrollView.OnScrollChangeListener:

 scvProduct.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView nestedScrollView,int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            Log.e("ProductFragment","position button " + topPositionButton + " scrollY " + scrollY);
            if(scrollY > oldScrollY){
                Log.e("ProductFragment","down");
            }
            else{
                Log.e("ProductFragment","up");
            }
        }
    });

image compare NestedScrollview and View OnScrollChangeListener