【问题标题】:Hard task - Detect when scrolling from RecyclerViewAdapter no Fragment or activity困难任务 - 从 RecyclerView Adapter 滚动到 Fragment 或活动时检测
【发布时间】:2018-03-25 01:51:31
【问题描述】:

您好,我正在开发一个 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,但它没有像我预期的那样工作,我不知道如何得到这个,希望有人可以帮助我

【问题讨论】:

  • addOnScrollListener 是一个函数。您可以从任何引用回收站视图的地方调用它。
  • 您好,我的适配器是另一个类,我如何从该适配器类实现 OnscrollListener?
  • 和你在其他地方做的一样——你声明一个类来实现它,并实现接口的所有功能。
  • 我使用了 public Shop getItem(int position) { Shop shop = storesList.get(Integer.valueOf(position));退货商店; } 在适配器中按位置从 Fragment 获取项目

标签: java android android-recyclerview onscrolllistener


【解决方案1】:

我解决了它在适配器中添加一个方法

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);
            }
        });

希望这可以帮助其他人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2016-07-07
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多