>

하루 종일 검색했지만 답을 찾을 수 없습니다. 나는 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);
            }
        }
    }
};

}


  • 답변 # 1

    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()
        }
    }
    
    

  • 답변 # 2

    마침내 해결책을 찾았습니다. 내 recyclerview가 NestedScrollview 안에 있었으므로 scrollListener에 대한 올바른 것을 얻을 수 없습니다. NestedScroll을 LinearLayout으로 변경합니다.

  • 이전 emacs - orgmodeorg/manual/Installationhtml에 대한 조직 소스 코드는 어디에서 찾을 수 있습니까?
  • 다음 emacs - 에코 영역에 시계를 표시 할 수 있습니까?