private void initGridLayout() { mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button); try { int screenOrientation = getResources().getConfiguration().orientation; if (!TextUtils.isEmpty(BrewSharedPrefs.getUserToken())) { userToken = BrewSharedPrefs.getUserToken(); } grid = (RecyclerView) findViewById(R.id.grid); grid.setItemAnimator(new DefaultItemAnimator()); grid.setHasFixedSize(false); int gridColumns = 3; if (screenOrientation == Configuration.ORIENTATION_LANDSCAPE) { gridColumns = 4; } final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(gridColumns, LinearLayoutManager.VERTICAL); layoutManager.scrollToPosition(0); grid.setLayoutManager(layoutManager); List<Image> placeHolder = new Vector<>(); adapter = new GridImagesRecycler(placeHolder, this); grid.setAdapter(adapter); load(); } catch (Exception e) { if (BuildConfig.DEBUG) { Log.e(Constants.LOG, e.getMessage()); } } }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // first solution (simple) // if (!recyclerView.canScrollVertically(1)) { // onScrolledToEnd(); // } // end of first solution // second solution visibleItemCount = recyclerView.getChildCount(); totalItemCount = sglm.getItemCount(); firstVisibleItem = sglm.findFirstVisibleItemPositions(null); if (loading) { if (totalItemCount > previousTotal) { loading = false; previousTotal = totalItemCount; } } if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem[0] + visibleThreshold)) { onLoadMore(); loading = true; } // End second solution }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); visibleItemCount = recyclerView.getChildCount(); totalItemCount = mGridLayoutManager.getItemCount(); int[] items = new int[3]; mGridLayoutManager.findFirstVisibleItemPositions(items); firstVisibleItem = items[0]; if (loading) { if (totalItemCount > previousTotal) { loading = false; previousTotal = totalItemCount; } } if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { // End has been reached // Do something current_page++; onLoadMore(current_page); loading = true; } }
public int getFirstVisiblePosition() { LayoutManager layoutManager = getLayoutManager(); if (null == layoutManager) return 0; int ret = -1; switch (mLayoutManagerType) { case LAYOUT_MANAGER_TYPE_LINEAR: ret = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition() - getHeaderViewsCount(); break; case LAYOUT_MANAGER_TYPE_GRID: ret = ((GridLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition() - getHeaderViewsCount(); break; case LAYOUT_MANAGER_TYPE_STAGGERED_GRID: StaggeredGridLayoutManager tempStaggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] firstVisibleItemPositions = new int[tempStaggeredGridLayoutManager.getSpanCount()]; tempStaggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions( firstVisibleItemPositions); ret = firstVisibleItemPositions[0] - getHeaderViewsCount(); break; } return ret < 0 ? 0 : ret; }
public boolean shouldHandleRecyclerViewLoadingMore(RecyclerView recyclerView) { if (mIsLoadingMore || mCurrentRefreshStatus == RefreshStatus.REFRESHING || mLoadMoreFooterView == null || mDelegate == null || recyclerView.getAdapter() == null || recyclerView.getAdapter().getItemCount() == 0) { return false; } RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); if (manager == null || manager.getItemCount() == 0) { return false; } if (manager instanceof LinearLayoutManager) { LinearLayoutManager layoutManager = (LinearLayoutManager) manager; if (layoutManager.findLastCompletelyVisibleItemPosition() == recyclerView.getAdapter().getItemCount() - 1) { return true; } } else if (manager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager; int[] out = layoutManager.findLastCompletelyVisibleItemPositions(null); int lastPosition = layoutManager.getItemCount() - 1; for (int position : out) { if (position == lastPosition) { return true; } } } return false; }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItemPosition = findMax(lastPositions); }
private void changeListVisualization(MenuItem menuItem) { if (isListView) { staggeredGridLayoutManager.setSpanCount(2); menuItem.setIcon(R.drawable.ic_action_list); menuItem.setTitle(R.string.menu_show_as_list); isListView = false; } else { staggeredGridLayoutManager.setSpanCount(1); menuItem.setIcon(R.drawable.ic_action_grid); menuItem.setTitle(R.string.menu_show_as_grid); isListView = true; } }
/** 获取最后一条展示的位置 */ private int getLastVisiblePosition() { int position; if (getLayoutManager() instanceof LinearLayoutManager) { position = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition(); } else if (getLayoutManager() instanceof GridLayoutManager) { position = ((GridLayoutManager) getLayoutManager()).findLastVisibleItemPosition(); } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager(); int[] lastPositions = layoutManager.findLastVisibleItemPositions(new int[layoutManager.getSpanCount()]); position = getMaxPosition(lastPositions); } else { position = getLayoutManager().getItemCount() - 1; } return position; }
public int getLastVisiblePosition() { LayoutManager layoutManager = getLayoutManager(); if (null == layoutManager) return -1; int curItemCount = (null != mReqAdapter ? mReqAdapter.getItemCount() - 1 : 0); int ret = -1; switch (mLayoutManagerType) { case LAYOUT_MANAGER_TYPE_LINEAR: ret = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() - getHeaderViewsCount(); if (ret > curItemCount) { ret -= getFooterViewsCount(); } break; case LAYOUT_MANAGER_TYPE_GRID: ret = ((GridLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() - getHeaderViewsCount(); if (ret > curItemCount) { ret -= getFooterViewsCount(); } break; case LAYOUT_MANAGER_TYPE_STAGGERED_GRID: StaggeredGridLayoutManager tempStaggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] lastVisibleItemPositions = new int[tempStaggeredGridLayoutManager.getSpanCount()]; tempStaggeredGridLayoutManager.findLastCompletelyVisibleItemPositions( lastVisibleItemPositions); if (lastVisibleItemPositions.length > 0) { int maxPos = lastVisibleItemPositions[0]; for (int curPos : lastVisibleItemPositions) { if (curPos > maxPos) maxPos = curPos; } ret = maxPos - getHeaderViewsCount(); if (ret > curItemCount) { ret -= getFooterViewsCount(); } } break; } return ret < 0 ? (null != mReqAdapter ? mReqAdapter.getItemCount() - 1 : 0) : ret; }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); int lastVisibleItem = 0; int firstVisibleItem = 0; RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); int totalItemCount = layoutManager.getItemCount(); if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) layoutManager); lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition(); firstVisibleItem = linearLayoutManager.findFirstCompletelyVisibleItemPosition(); } else if (layoutManager instanceof GridLayoutManager) { GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager); // 通过LayoutManager找到当前显示的最后的item的position lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition(); firstVisibleItem = gridLayoutManager.findFirstCompletelyVisibleItemPosition(); } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = ((StaggeredGridLayoutManager) layoutManager); // 因为StaggeredGridLayoutManager的特殊性可能导致最后显示的item存在多个,所以这里取到的是一个数组 // 得到这个数组后再取到数组中position值最大的那个就是最后显示的position值了 int[] lastPositions = new int[((StaggeredGridLayoutManager) layoutManager).getSpanCount()]; staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItem = findMax(lastPositions); firstVisibleItem = staggeredGridLayoutManager.findFirstVisibleItemPositions(lastPositions)[0]; } if (firstVisibleItem == 0) { if (!mPullLoadMoreRecyclerView.isLoadMore()) { mPullLoadMoreRecyclerView.setPullRefreshEnable(true); } } else { mPullLoadMoreRecyclerView.setPullRefreshEnable(false); } /** 无论水平还是垂直 */ if (!mPullLoadMoreRecyclerView.isRefresh() && mPullLoadMoreRecyclerView.isHasMore() && (lastVisibleItem >= totalItemCount - 1) && !mPullLoadMoreRecyclerView.isLoadMore() && (dx > 0 || dy > 0)) { mPullLoadMoreRecyclerView.setIsLoadMore(true); mPullLoadMoreRecyclerView.loadMore(); } }
private void initGameRecyclerView() { layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); gameRecyclerView.setLayoutManager(layoutManager); gameAdapter.setOnItemClickListener(this); gameRecyclerView.setAdapter(gameAdapter); gameRecyclerView.setHasFixedSize(true); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // For larger devices (like tablets), it creates more columns. int columnsNumber = getActivity().getResources().getInteger(R.integer.restaurant_columns); if (mStaggeredGridLayoutManager == null) { mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(columnsNumber, StaggeredGridLayoutManager.VERTICAL); } else { mStaggeredGridLayoutManager.setSpanCount(columnsNumber); } }
@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); // int lastVisibleItemPosition = -1; if (layoutManagerType == null) { if (layoutManager instanceof LinearLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR; } else if (layoutManager instanceof GridLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.GRID; } else if (layoutManager instanceof StaggeredGridLayoutManager) { layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID; } else { throw new RuntimeException( "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); } } switch (layoutManagerType) { case LINEAR: lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case GRID: lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); break; case STAGGERED_GRID: StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; if (lastPositions == null) { lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; } staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); lastVisibleItemPosition = findMax(lastPositions); break; } }
private int calculateFirstVisiblePosition(LayoutManager layoutManager) { if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; return linearLayoutManager.findFirstVisibleItemPosition(); } if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] firstVisibleItemPositions = staggeredGridLayoutManager.findFirstVisibleItemPositions(null); int min = 10; for (int i = 0; i < firstVisibleItemPositions.length; i++) { min = Math.min(min, firstVisibleItemPositions[i]); Log.e(TAG, "firstVisibleItemPositions" + i + firstVisibleItemPositions[i]); } return min; } return -1; }
private int calculateLastVisiblePosition(LayoutManager layoutManager) { if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; return linearLayoutManager.findLastVisibleItemPosition(); } if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int[] lastVisibleItemPositions = staggeredGridLayoutManager.findLastVisibleItemPositions(null); int max = -1; for (int i = 0; i < lastVisibleItemPositions.length; i++) { max = Math.max(max, lastVisibleItemPositions[i]); Log.e(TAG, "lastVisibleItemPositions" + i + lastVisibleItemPositions[i]); } return max; } return -1; }
/** * 检查是否需要执行Loadmore * * @param recycler RecyclerView * @param listener 加载更多监听 */ private static void checkLoadmore(RecyclerView recycler, OnLoadmoreListener listener) { RecyclerView.LayoutManager layoutManager = recycler.getLayoutManager(); if (layoutManager instanceof LinearLayoutManager) { int lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); int totalItemCount = recycler.getAdapter().getItemCount(); if (lastVisibleItem >= totalItemCount - 1) { listener.onLoadmore(); } } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) layoutManager; int last[] = new int[gridLayoutManager.getSpanCount()]; gridLayoutManager.findLastVisibleItemPositions(last); int totalItemCount = recycler.getAdapter().getItemCount(); for (int i = 0; i < last.length; i++) { int lastVisibleItem = last[i]; if (lastVisibleItem >= totalItemCount - 1) { listener.onLoadmore(); break; } } } }
@Override public void onMeasure( RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { for (int i = 0; i < getItemCount(); i++) { if (mSpanSizeLookup.getSpanSize(i) > 1) { try { View view = recycler.getViewForPosition(i); if (view != null) { LayoutParams lp = (LayoutParams) view.getLayoutParams(); lp.setFullSpan(true); } } catch (Exception e) { e.printStackTrace(); } } } super.onMeasure(recycler, state, widthSpec, heightSpec); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_consulta_tabela_preco); atoolbar = (Toolbar) (findViewById(R.id.tb_main)); atoolbar.setTitle("Consulta Tabela de Preço"); setSupportActionBar(atoolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); RecyviewTabelapreco = (RecyclerView) findViewById(R.id.RecyviewTabelaItempreco); produtodao = new ProdutoDao(this); if (getIntent().getExtras() != null) selecionandoTabela = getIntent().getExtras().getBoolean("selecionar_TabelaItemPreco", false); final StaggeredGridLayoutManager llm = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL); llm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE); RecyviewTabelapreco.setLayoutManager(llm); adapterTabelaitempreco = new AdapterConsultaTabelapreco(this, new ArrayList<Produtos>()) { @Override protected void onItemClickListener(int adapterPosition, int layoutPosition) { // evento de click simples Produtos produto = adapterTabelaitempreco.getItems().get(adapterPosition); if (selecionandoTabela) { Intent data = new Intent(); data.putExtra("Produto_id", produto.getIdproduto()); setResult(RESULT_OK, data); finish(); } else { idProduto = produto.getIdproduto(); nomeProduto = produto.getDescricao(); consultaTabelaPreco(produto); } } @Override protected boolean onLongItemClickListener(int adapterPosition, int layoutPosition) { // evento e click longo Produtos produto = adapterTabelaitempreco.getItems().get(adapterPosition); idProduto = produto.getIdproduto(); nomeProduto = produto.getDescricao(); consultaTabelaPreco(produto); return true; } }; RecyviewTabelapreco.setAdapter(adapterTabelaitempreco); consultaTabelaitem(); getDadosSearch(this.getIntent()); }