无法使用"网格布局管理器"处理用户分页



我找了一整天,但找不到答案。我有RecyclerView,我必须使用GridLayoutManager作为布局管理器。但是当我想在这个RecyclerView上使用分页时,这个分页总是。visibleItemCount总是等于totalItemCount,但是当我使用LinearLayout时,一切正常。但我需要GridlayoutManager.那么谁能帮我?

public class CategoryChildrenFragment extends BasicFragment implements CategoryChildrenFragmentView, CategoryChildrenAdapter.CategoryChildrenListener, CategoryProductsAdapter.CategoryProductListener {
@BindView(R.id.recyclerView)
RecyclerView mRecyclerView;

@BindView(R.id.recyclerViewProducts)
RecyclerView mRecyclerViewProducts;

@BindView(R.id.text_view_category_name)
TextView mTextViewCategoryName;

@Inject
Navigator mNavigator;

@InjectPresenter
CategoryChildrenFragmentPresenter mPresenter;

private  List<CategoryObject> mCategories;
private  List<CategoryProduct> products;
private  CategoryObject mCategory;
GridLayoutManager layoutManager;
private int categoryId = 0;
private int page = 0;
private int mDisplayWidth = 1020;
private String searchText = "";
private CategoryProductsAdapter mAdapterProducts;
private CategoriesResponse mResponse;
private int totalItemCount = 0;
private boolean isLastPage = false;
private boolean isLoading = false;

@SuppressLint("ValidFragment")
public CategoryChildrenFragment(CategoryObject categoryObject,List<CategoryObject> categories) {
this.mCategory = categoryObject;
this.mCategories = categories;
}


private void initView(View view) {
ButterKnife.bind(this,view);

Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
mDisplayWidth = size.x;
List<CategoryObject> selectedList = new LinkedList<>();

if (null != mCategory) {
mTextViewCategoryName.setText(mCategory.getName());
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

if (mCategory.getId() != 0) {
for (int i = 0; i <mCategories.size(); i++) {
if (mCategory.getId() == mCategories.get(i).getParentId()) {
selectedList.add(mCategories.get(i));
}
}
}
else {
products = new LinkedList<>();
categoryId = 0;
layoutManager = new GridLayoutManager(getContext(), 2);
mRecyclerViewProducts.setLayoutManager(layoutManager);
mRecyclerViewProducts.setAdapter(mAdapterProducts = new CategoryProductsAdapter(products,this, (int) (mDisplayWidth/2.27),getContext()));
mRecyclerViewProducts.addOnScrollListener(recyclerViewOnScrollListener);
getLoadingDialog().showDialog(getFragmentManager());
mPresenter.GET_ALL_BY_PARENT_CATEGORY(getAccessToken(),searchText,categoryId,page, 20);
}
mRecyclerView.setAdapter(new CategoryChildrenAdapter(selectedList,this));
}
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =  inflater.inflate(R.layout.fragment_category_childs, container, false);
initView(view);
return view;
}


@Override
public void onChildrenClicked(CategoryObject categoryObject) {
mRecyclerView.setVisibility(View.GONE);
products = new LinkedList<>();
layoutManager = new GridLayoutManager(getContext(), 2);
categoryId = categoryObject.getId();
mTextViewCategoryName.setText(categoryObject.getName());
mRecyclerViewProducts.setLayoutManager(layoutManager);
mRecyclerViewProducts.setAdapter(mAdapterProducts = new CategoryProductsAdapter(products,this, (int) (mDisplayWidth/2.27),getContext()));
mRecyclerViewProducts.addOnScrollListener(recyclerViewOnScrollListener);
getLoadingDialog().showDialog(getFragmentManager());
mPresenter.GET_ALL_BY_PARENT_CATEGORY(getAccessToken(),searchText,categoryId,page,20);
}


@Override
public void initProducts(CategoriesResponse response) {
this.mResponse = response;
this.products.addAll(response.getList());
mAdapterProducts.notifyDataSetChanged();
isLoading = false;
}

@Override
public void stopProgressBar() {
getLoadingDialog().hideDialog();
}

@Override
public void onProductClicked(CategoryProduct categoryProduct) {
Product product = new Product();
product.setProductId(categoryProduct.getId());
product.setPhotos(categoryProduct.getPhotos());
mNavigator.toProductActivity(getContext(),product);
}

@Override
public void onAddToCard(CategoryProduct product) {
mNavigator.toAddTOCardDialogFragment(getFragmentManager(),product);
}


private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}


@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (null != mResponse && 0 != mResponse.getAllCount() && totalItemCount == mResponse.getAllCount()) {
isLastPage = true;
}

if (!isLoading && !isLastPage) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition > 0 &&  totalItemCount <  mResponse.getAllCount()) {
page = page + 1;
isLoading = true;
mPresenter.GET_ALL_BY_PARENT_CATEGORY(getAccessToken(),searchText,categoryId,page,20);
}
}
}
};

}

使用 findLastOnlyItemPosition(( 和 findLastCompleteVisibleItemPosition(( 函数进行 GridLayoutManager:

private val recyclerViewOnScrollListener = object : RecyclerView.OnScrollListener(){
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val layoutManager = recyclerView.layoutManager as GridLayoutManager
val visibleItemCount = layoutManager.findLastVisibleItemPosition()
}
}

最后,伙计们我找到了解决方案。我的回收器视图在NestedScrollview中,所以我无法为scrollListener找到正确的内容.我将NestedScroll更改为LinearLayout,一切都很酷。所以结论不要把回收器视图放在滚动视图中,如果你想听回收器视图的滚动

最新更新