无法使用 `GridLayoutManager` 处理用户分页
Can not handle user pagination with `GridLayoutManager`
我搜索了一整天,但找不到答案。我有 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);
}
}
}
};
}
对 GridLayoutManager 使用 findLastVisibleItemPosition() 和 findLastCompletelyVisibleItemPosition() 函数:
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()
}
}
最后,伙计们,我发现 solution.My recyclerview 在 NestedScrollview 中,所以我无法得到正确的东西 scrollListener.I 将 NestedScroll 更改为 LinearLayout 一切都是 cool.So 不要将 recyclerview 放在 scrollview 中如果你想听 recyclerview 的滚动
我搜索了一整天,但找不到答案。我有 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);
}
}
}
};
}
对 GridLayoutManager 使用 findLastVisibleItemPosition() 和 findLastCompletelyVisibleItemPosition() 函数:
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()
}
}
最后,伙计们,我发现 solution.My recyclerview 在 NestedScrollview 中,所以我无法得到正确的东西 scrollListener.I 将 NestedScroll 更改为 LinearLayout 一切都是 cool.So 不要将 recyclerview 放在 scrollview 中如果你想听 recyclerview 的滚动