【问题标题】:Android - OnScrollListener for Staggered Grid ViewAndroid - 用于交错网格视图的 OnScrollListener
【发布时间】:2013-05-31 07:25:01
【问题描述】:

请原谅我的英语,我是法国人...

所以,我有一个关于我的 Android 应用程序的问题。 我必须将网格视图集成为 Pinterest 样式。 我找到了这个库:交错网格视图 (https://github.com/maurycyw/StaggeredGridView)

它工作正常但是......没有 OnScrollListener ! 我必须知道用户何时看到最后一个项目,以便加载更多。 但是没有 OnScrollListener 是不可能的!

你对我的问题有什么想法吗?

非常感谢。

【问题讨论】:

    标签: android gridview scroll


    【解决方案1】:

    如果对您不起作用,请尝试此操作,然后将您的回收器视图包装在滚动视图中,然后检查滚动视图是否已到达底部:

    在您的 xml 文件中:

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbars="vertical"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
        </RelativeLayout>
    
    </ScrollView>
    

    在你的类文件中:

    private ScrollView mScrollView;
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initScrollView();
        ...
    }
    
    private void initScrollView() {
        mScrollView = (ScrollView) mView.findViewById(R.id.scroll_view);
        mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if (mScrollView != null) {
                    if (mScrollView.getChildAt(0).getBottom() <= (mScrollView.getHeight() + mScrollView.getScrollY())) {
                        // Scroll view is at bottom
                        // If not loading then start load more
                    } else {
                        // Scroll view is not at bottom
                    }
                }
            }
        });
    }
    

    请记住将回收器视图的嵌套滚动设置为 false,以便在您的类文件中平滑滚动:

    mRecyclerView.setNestedScrollingEnabled(false);
    

    如果您想在下一页到达底部之前开始加载下一页,则只需从 mScrollView.getChildAt(0).getBottom() 中减去一个整数

    例如这里减去1000:

    if (mScrollView.getChildAt(0).getBottom() - 1000 <= (mScrollView.getHeight() + mScrollView.getScrollY())) {
        // Scroll view is at bottom
        // If not loading then start load more
    } else {
        // Scroll view is not at bottom
    }
    

    在滚动到达底部之前使用更大的整数开始加载。

    【讨论】:

      【解决方案2】:

      更新:
      经过测试,
      StaggeredGridView Adapter 调用 notifyDataSetChanged() 以将滚动位置重置为 0(视图的开始)。
      而是使用PinterestLikeAdapterView


      我已将loadMoreListener 添加到StaggeredGridView
      步骤:
      1-添加一个loadmore接口和booelan isLoading(在类的顶部)

      public interface OnLoadMoreListener {
          public boolean onLoadMore();
      }
      
      private boolean mIsLoading;
      private OnLoadMoreListener mLoadMoreListener;
      

      2- 在deltaY &gt; 0的else中找到trackMotionScroll函数
      并在up = false; 之后添加

      if (overhang <= 80) { // 80 is how nearest to the end. ZERO mean the end of list
          // scrolling down and almost reach the end of list
          if (false == mIsLoading && null != mLoadMoreListener) {
              if (false == mLoadMoreListener.onLoadMore()) {
                  mIsLoading = true;
                  Log.d(TAG, "loadMore");
              }
          }
      }
      

      3-添加setListener()loadComplated()函数

      public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
          this.mLoadMoreListener = onLoadMoreListener;
      }
      
      public void loadMoreCompleated() {
          mIsLoading = false;
      }
      

      如何使用
      1-在你的Activity/Fragment 中定义一个loadMoreListener

      private StaggeredGridView.OnLoadMoreListener loadMoreListener = new StaggeredGridView.OnLoadMoreListener() {
      
          @Override
          public boolean onLoadMore() {
              loading.setVisibility(View.VISIBLE);
              // load more data from internet (not in the UI thread)
      
              return true; // true if you have more data to load, false you dont have more data to load 
          }
      };
      

      2- 当您完成数据加载并将其添加到适配器调用时

      private void doneLoading() {
          gridView.loadMoreCompleated();
          loading.setVisibility(View.GONE);
      }
      

      【讨论】:

        【解决方案3】:

        必须知道用户何时看到最后一项,才能加载更多内容。 但是没有 OnScrollListener 是不可能的!

        你为什么要使用 onScrollListener 呢?相反,编写一个扩展 BaseAdapter 的类(也许是 MyStaggeredGridAdapter?)并将其应用于 StaggeredGridView。然后可以使用适配器的 getView() 方法,每当需要渲染新视图时都会调用该方法。

        【讨论】:

          【解决方案4】:

          要知道最后一个项目何时显示在屏幕上,我使用适配器的 getView() 方法。只需将您的最后一个索引与“位置”参数进行比较。像这样的

              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
                  ...
          
                  if (lastLoaded == position){
                      loadMore();
                  }
          
                  ...
              }
          

          然后您可能希望看到这个:How to add data into bottom of Android StaggeredGridView and not go back top? 以避免从头开始加载列表并从显示的最后一项继续。

          我希望这会有所帮助,不知道这是否是最好的解决方案,但对我有用;)

          【讨论】:

          • 你好@Hugo ...你的代码中的lastLoaded是什么...请告诉我..我也遇到了同样的问题
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-09
          相关资源
          最近更新 更多