Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通知SnapHelper何时以及哪些RecyclerView项已捕捉到位?_Android_Android Recyclerview_Adapter - Fatal编程技术网

Android 如何通知SnapHelper何时以及哪些RecyclerView项已捕捉到位?

Android 如何通知SnapHelper何时以及哪些RecyclerView项已捕捉到位?,android,android-recyclerview,adapter,Android,Android Recyclerview,Adapter,我以标准方式使用SnapHelper设置我的recyclerView。那很好。然而,当一个项目被“抓拍”到位时,我需要得到通知,以便我可以使用该索引更新TextView。我已经尝试在recyclerView上设置setOnFlingListener,但是snapHelper已经设置了它的一个实例,因此引发了一个异常 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); layoutM

我以标准方式使用SnapHelper设置我的recyclerView。那很好。然而,当一个项目被“抓拍”到位时,我需要得到通知,以便我可以使用该索引更新TextView。我已经尝试在recyclerView上设置setOnFlingListener,但是snapHelper已经设置了它的一个实例,因此引发了一个异常

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);
    adapter = new PlantAdapter(this, Globals.getPlants());
    recyclerView.setAdapter(adapter);
    snapHelper = new snapHelper(this);
    snapHelper.attachToRecyclerView(recyclerView);

有没有其他方法可以准确地知道什么时候,哪个物品被抓拍了?

我已经猜出来了。我向recyclerview添加了一个onScrollListener,然后在滚动状态为scroll\u state\u IDLE时更改OnScrollState中的位置

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                View centerView = snapHelper.findSnapView(layoutManager);
                if (centerView != null) {
                    int pos = layoutManager.getPosition(centerView);
                    updateAdapterPosition(pos);
                }
                else {
                    updateAdapterPosition(markers.size() - 1);
                }
            }
        }
    });