无法使用“ GridLayoutManager`处理用户分页”>

问题描述 投票:0回答:2

我整天都在搜索,但找不到答案。我有RecyclerView,并且必须使用GridLayoutManager作为layoutManager。但是,当我想在此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);
            }
        }
    }
};

}

我整天都在搜索,但找不到答案。我有RecyclerView,并且必须使用GridLayoutManager作为layoutManager。但是,当我想在此Recycler上使用分页时,请始终查看此分页。 ...

android android-recyclerview gridlayoutmanager
2个回答
0
投票

使用GridLayoutManager的findLastVisibleItemPosition()和findLastCompletelyVisibleItemPosition()函数:


0
投票

最后找到了解决方案的人。我的recyclerview位于NestedScrollview内,所以我无法为scrollListener获得正确的东西。我将NestedScroll更改为LinearLayout,一切都很棒。因此,如果您想收听recyclerview的滚动条,请不要将recyclerview放在scrollview内] >

© www.soinside.com 2019 - 2024. All rights reserved.