在nestedscrollview内的recyclerview addOnScrollListener(endless scrolllistener)

时间:2017-08-06 15:47:57

标签: java android layout listener

我在nestedscrollview中使用recyclelerview,如下所示:

<script>
            var map;
            var uluru;
            var start; <-- Declaration
            var geocoder;

            function codeAddress() {
                var address = "Essen";
                geocoder = new google.maps.Geocoder();
                geocoder.geocode({
                    'address': address
                }, function(results, status) {
                    if (status == 'OK') {

                        start = results[0].geometry.location; <-- Setting value
                        
                        var marker = new google.maps.Marker({

                            position: start,
                            map: map
                        }); <-- Position is set, start has a value
                    } else {
                        alert('Geocode was not successful for the following reason: ' + status);
                    }
                });

                calcRoute();
            }

            function calcRoute() {
                var directionsService = new google.maps.DirectionsService;
                var directionsDisplay = new google.maps.DirectionsRenderer({map: map});
                directionsService.route({
                    origin: start, <-- start is undefined
                    destination: uluru,
                    travelMode: 'DRIVING'
                }, function(response, status) {
                    if (status === 'OK') {
                        directionsDisplay.setDirections(response);
                    } else {
                        window.alert('Directions request failed due to ' + status);
                    }
                });
            }

        </script>

问题在于,当我想将侦听器(<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView android:id="@+id/mainScrollView" android:layout_marginBottom="?attr/actionBarSize" 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"> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:overScrollMode="never" android:layout_height="250dp" android:focusableInTouchMode="true"/> <android.support.v7.widget.RecyclerView android:id="@+id/VerticalRV" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> )设置为此回收站视图时,它无论如何都无法正常工作。我也调试了这段代码,但它甚至没有捕获到这个事件。以下是java代码:

onScrollListener

如何让这个监听器工作? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我知道这可能会晚一些,但这也许会对某人有所帮助。 当数据插入RecyclerView后,我的recyclerview不断调用RecyclerView的onScrolled方法的问题也让我感到震惊。 经过一些调试后,我发现我将NestedScrollView与RecyclerView一起使用。因此,我搜索了与此相关的内容,并找到了一个很棒的 medium.com 教程here。效果很好。

从此处的链接添加一些代码。

mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if(v.getChildAt(v.getChildCount() - 1) != null) {
            if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
                    scrollY > oldScrollY) {

                int visibleItemCount = mLayoutManager.getChildCount();
                int totalItemCount = mLayoutManager.getItemCount();
                int pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if (!isLoadingData()) {

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {

                         //Load Your Data
                    }
                }
            }
        }
    }
});