解决Android SwipeRefreshLayout & RecyclerView使用的Bug

1、引言

用Android提供的support包里的SwipeRefreshLayout和RecyclerView同时使用的时候会出现RecyclerView的item被裁剪的情况,如下图所示:

解决Android SwipeRefreshLayout & RecyclerView使用的Bug_第1张图片


2、解决此Bug的方法

参考资料如下:

http://stackoverflow.com/questions/25178329/recyclerview-and-swiperefreshlayout/25227797#25227797

https://gist.github.com/VladSumtsov/ad4e13511a9b73ff3b13


上面显示的是两种解决办法,我推荐第二种比较简单,我的代码片段如下:

mLayoutManager = new LinearLayoutManager(getActivity());
			//mPicListView是RecyclerView对象
			mPicListView.setLayoutManager(mLayoutManager);
			mPicListView.setOnScrollListener(new OnScrollListener() {
				@Override
				public void onScrollStateChanged(RecyclerView recyclerView,
						int newState) {
					super.onScrollStateChanged(recyclerView, newState);
					//加载更多
					if (!isLoading
							&& hasMore
							&& mAdapter.getItemCount() == (mLayoutManager
									.findLastVisibleItemPosition() + 1)
							&& newState == RecyclerView.SCROLL_STATE_IDLE) {
						isLoading = true;
						requestServerArticleMore();
					}
				}

				@Override
				public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
					super.onScrolled(recyclerView, dx, dy);
					//解决RecyclerView和SwipeRefreshLayout共用存在的bug
					mRefreshLayout.setEnabled(mLayoutManager
							.findFirstCompletelyVisibleItemPosition() == 0);
				}
			});

希望对你有帮助。

你可能感兴趣的:(android,RecyclerView)