硬任务 - 从RecyclerViewAdapter滚动时检测到没有碎片或活动

时间:2017-10-13 01:38:34

标签: java android android-recyclerview onscrolllistener

你好我正在研究一个Android项目,我需要检测滚动Recycler View时,我知道你可以从Fragment或Activity中将OnscrollListener添加到RecyclerView,但我需要从适配器执行以便从那里传递数据片段,它是关于实时动画的东西,这是我现在在我的适配器

holder.linearLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

                Intent intent = new Intent("custom-message");
                intent.putExtra("address",shop.getAddress());
                LocalBroadcastManager.getInstance(v.getContext()).sendBroadcast(intent);

            }
        });

我使用了AddonLayoutChangeListener,但它没有像我预期的那样工作,我无法弄清楚如何得到这个,希望有人可以帮助我

1 个答案:

答案 0 :(得分:0)

我解决了在Adapter

中添加方法的问题
public Shop getItem(int position) {
        Shop shop = shopsList.get(Integer.valueOf(position));
        return shop;
    }

然后从Fragment添加以下

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (recyclerView.getScrollState() == recyclerView.SCROLL_STATE_IDLE)
                {
                    int postitionCentereitem = carouselaLyoutManager.getCenterItemPosition();
                    Shop item = (Shop) mAdapter.getItem(postitionCentereitem);
                    int Id = item.getId();
                    String Address = item.getAddress();
                    lblMessage.setText(Address);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

希望这可以帮助别人。