androidx.recyclerview.widget.StaggeredGridLayoutManager Java Examples

The following examples show how to use androidx.recyclerview.widget.StaggeredGridLayoutManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DynamicRecyclerViewFrame.java    From dynamic-support with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    if (mRecyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
        ((StaggeredGridLayoutManager) mRecyclerViewLayoutManager).setGapStrategy(
                ((StaggeredGridLayoutManager) mRecyclerViewLayoutManager)
                        .getGapStrategy() | GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
        ((StaggeredGridLayoutManager) mRecyclerViewLayoutManager)
                .invalidateSpanAssignments();

        if (((StaggeredGridLayoutManager)
                mRecyclerViewLayoutManager).getSpanCount() > 1) {
            ((StaggeredGridLayoutManager) mRecyclerViewLayoutManager)
                    .scrollToPositionWithOffset(0, 0);
        }
    }
}
 
Example #2
Source File: FlexibleLayoutManager.java    From FlexibleAdapter with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to find the adapter position of the <b>first completely</b> visible view
 * [for each span], no matter which Layout is.
 *
 * @return the adapter position of the <b>first fully</b> visible item or {@code RecyclerView.NO_POSITION}
 * if there aren't any visible items.
 * @see #findFirstVisibleItemPosition()
 * @since 5.0.0-b8
 */
@Override
public int findFirstCompletelyVisibleItemPosition() {
    RecyclerView.LayoutManager layoutManager = getLayoutManager();
    if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGLM = (StaggeredGridLayoutManager) layoutManager;
        int position = staggeredGLM.findFirstCompletelyVisibleItemPositions(null)[0];
        for (int i = 1; i < getSpanCount(); i++) {
            int nextPosition = staggeredGLM.findFirstCompletelyVisibleItemPositions(null)[i];
            if (nextPosition < position) {
                position = nextPosition;
            }
        }
        return position;
    } else {
        return ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
    }
}
 
Example #3
Source File: ScrollCompat.java    From SmoothRefreshLayout with MIT License 6 votes vote down vote up
public static boolean isScrollingView(View view) {
    if (view instanceof AbsListView || view instanceof ScrollView || view instanceof WebView) {
        return true;
    }
    if (ViewCatcherUtil.isRecyclerView(view)) {
        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager != null) {
            if (manager instanceof LinearLayoutManager) {
                LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
                return linearManager.getOrientation() == RecyclerView.VERTICAL;
            } else if (manager instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager gridLayoutManager =
                        (StaggeredGridLayoutManager) manager;
                return gridLayoutManager.getOrientation() == RecyclerView.VERTICAL;
            }
        }
        return true;
    } else {
        return view instanceof ScrollingView;
    }
}
 
Example #4
Source File: BackToTopUtils.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void scrollToTop(RecyclerView recyclerView) {
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    int firstVisibleItemPosition = 0;
    if (manager instanceof LinearLayoutManager) {
        firstVisibleItemPosition = getFirstVisibleItemPosition((LinearLayoutManager) manager);
    } else if (manager instanceof StaggeredGridLayoutManager) {
        firstVisibleItemPosition = getFirstVisibleItemPosition((StaggeredGridLayoutManager) manager);
    }
    if (firstVisibleItemPosition > 5) {
        recyclerView.scrollToPosition(5);
    }
    recyclerView.smoothScrollToPosition(0);

    MysplashActivity activity = MysplashApplication.getInstance().getTopActivity();
    if (activity != null) {
        ComponentFactory.getSettingsService().notifySetBackToTop(activity);
    }
}
 
Example #5
Source File: StandardHomeActivityUIController.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
private void setupGridView() {
    final RecyclerView grid = activity.findViewById(R.id.home_gridview_buttons);
    grid.setHasFixedSize(false);

    StaggeredGridLayoutManager gridView =
            new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    grid.setLayoutManager(gridView);
    grid.setItemAnimator(null);
    grid.setAdapter(adapter);

    grid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @SuppressLint("NewApi")
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                grid.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                grid.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }

            grid.requestLayout();
            adapter.notifyDataSetChanged();
            activity.rebuildOptionsMenu();
        }
    });
}
 
Example #6
Source File: DividerGridItemDecoration.java    From RvHelper with Apache License 2.0 6 votes vote down vote up
private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
                            int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        if ((pos + 1) % spanCount == 0) {
            // 如果是最后一列,则不需要绘制右边
            return true;
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            if ((pos + 1) % spanCount == 0) {
                // 如果是最后一列,则不需要绘制右边
                return true;
            }
        } else {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
                return true;
        }
    }
    return false;
}
 
Example #7
Source File: DividerGridItemDecoration.java    From a with GNU General Public License v3.0 6 votes vote down vote up
private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                          int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        childCount = childCount - childCount % spanCount;
        if (pos >= childCount)// 如果是最后一行,则不需要绘制底部
            return true;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {

        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        // StaggeredGridLayoutManager 且纵向滚动
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            childCount = childCount - childCount % spanCount;
            // 如果是最后一行,则不需要绘制底部
            if (pos >= childCount)
                return true;
        } else {
            // 如果是最后一行,则不需要绘制底部
            if ((pos + 1) % spanCount == 0) {
                return true;
            }
        }
    }
    return false;
}
 
Example #8
Source File: HorizontalScrollCompat.java    From SmoothRefreshLayout with MIT License 6 votes vote down vote up
public static boolean isScrollingView(View view) {
    if (ViewCatcherUtil.isViewPager(view)
            || view instanceof HorizontalScrollView
            || view instanceof WebView) {
        return true;
    } else if (ViewCatcherUtil.isRecyclerView(view)) {
        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager != null) {
            if (manager instanceof LinearLayoutManager) {
                LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
                return linearManager.getOrientation() == RecyclerView.HORIZONTAL;
            } else if (manager instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager gridLayoutManager =
                        (StaggeredGridLayoutManager) manager;
                return gridLayoutManager.getOrientation() == RecyclerView.HORIZONTAL;
            }
        }
    }
    return false;
}
 
Example #9
Source File: PostsActivity.java    From Ruisi with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.menu:
            if (getType() == PostListAdapter.TYPE_IMAGE) {
                StaggeredGridLayoutManager m = (StaggeredGridLayoutManager) mLayoutManager;
                int span = m.getSpanCount();
                if (span == 1) {
                    m.setSpanCount(2);
                } else {
                    m.setSpanCount(1);
                }
            } else {
                if (isLogin()) {
                    Intent i = new Intent(this, NewPostActivity.class);
                    i.putExtra(NewPostActivity.FID, fid);
                    i.putExtra(NewPostActivity.TITLE, title);
                    startActivityForResult(i, 0);
                }
            }
            break;
        default:
            break;
    }
}
 
Example #10
Source File: StaggeredInfiniteScrollBrickFragment.java    From brickkit-android with Apache License 2.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    dataManager.applyStaggeredGridLayout(2, StaggeredGridLayoutManager.VERTICAL);
    dataManager.getBrickRecyclerAdapter().setOnReachedItemAtPosition(
            new OnReachedItemAtPosition() {
                @Override
                public void bindingItemAtPosition(int position) {
                    if (position == dataManager.getRecyclerViewItems().size() - 1) {
                        page++;
                        addNewBricks();
                    }
                }
            }
    );

    return view;
}
 
Example #11
Source File: FragmentOverall.java    From FlexibleAdapter with Apache License 2.0 6 votes vote down vote up
@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    MenuItem gridMenuItem = menu.findItem(R.id.action_list_type);
    if (mRecyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
        gridMenuItem.setIcon(R.drawable.ic_view_agenda_white_24dp);
        gridMenuItem.setTitle(R.string.linear_layout);
    } else if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
        gridMenuItem.setIcon(R.drawable.ic_dashboard_white_24dp);
        gridMenuItem.setTitle(R.string.staggered_layout);
    } else {
        gridMenuItem.setIcon(R.drawable.ic_view_grid_white_24dp);
        gridMenuItem.setTitle(R.string.grid_layout);
    }
}
 
Example #12
Source File: HomeFragment.java    From candybar with Apache License 2.0 6 votes vote down vote up
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mManager = new StaggeredGridLayoutManager(
            getActivity().getResources().getInteger(R.integer.home_column_count),
            StaggeredGridLayoutManager.VERTICAL);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(mManager);

    if (CandyBarApplication.getConfiguration().getHomeGrid() == CandyBarApplication.GridStyle.FLAT) {
        int padding = getActivity().getResources().getDimensionPixelSize(R.dimen.card_margin);
        mRecyclerView.setPadding(padding, padding, 0, 0);
    }

    initHome();
}
 
Example #13
Source File: DividerGridItemDecoration.java    From a with GNU General Public License v3.0 6 votes vote down vote up
private boolean isLastColumn(RecyclerView parent, int pos, int spanCount,
                             int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        if ((pos + 1) % spanCount == 0) {
            return true;
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {

        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            // 如果是最后一列,则不需要绘制右边
            if ((pos + 1) % spanCount == 0) {
                return true;
            }
        } else {
            childCount = childCount - childCount % spanCount;
            // 如果是最后一列,则不需要绘制右边
            if (pos >= childCount)
                return true;
        }
    }
    return false;
}
 
Example #14
Source File: GridDividerItemDecoration.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 判断是否是最后一列
 * @param parent
 * @param pos
 * @param spanCount
 * @param childCount
 * @return
 */
private boolean isLastColumn(RecyclerView parent, int pos, int spanCount, int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        if ((pos + 1) % spanCount == 0) {// 如果是最后一列,则不需要绘制右边
            return true;
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            if ((pos + 1) % spanCount == 0) {// 最后一列
                return true;
            }
        } else {

        }
    }
    return false;
}
 
Example #15
Source File: RcvMultiAdapter.java    From RecyclerViewAdapter with Apache License 2.0 6 votes vote down vote up
/**
 * 添加HeadView或FootView或LoadMore或EmptyView后
 * 兼容StaggeredGridLayoutManager的方法
 */
@Override
public void onViewAttachedToWindow(RcvHolder holder)
{
    super.onViewAttachedToWindow(holder);
    if (isInHeadViewPos(holder.getLayoutPosition())
            || isInFootViewPos(holder.getLayoutPosition())
            || isInLoadMorePos(holder.getLayoutPosition())
            || isInEmptyStatus())
    {
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams)
        {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(true);
        }
    }
}
 
Example #16
Source File: AnRecyclerView.java    From Animer with Apache License 2.0 6 votes vote down vote up
@Override
public void setLayoutManager(LayoutManager layout) {

    if(layout instanceof LinearLayoutManager){
        LAYOUT_MODE = LINEAR_LAYOUT;
    }

    if(layout instanceof GridLayoutManager){
        LAYOUT_MODE = GRID_LAYOUT;
    }

    if(layout instanceof StaggeredGridLayoutManager){
        LAYOUT_MODE = STAGGERED_LAYOUT;
    }

    mLayoutManager = layout;
    super.setLayoutManager(layout);
}
 
Example #17
Source File: DefaultItemDecoration.java    From SwipeRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
    @NonNull RecyclerView.State state) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        int orientation = getOrientation(layoutManager);
        int position = parent.getChildLayoutPosition(view);
        int spanCount = getSpanCount(layoutManager);
        int childCount = layoutManager.getItemCount();

        if (orientation == RecyclerView.VERTICAL) {
            offsetVertical(outRect, position, spanCount, childCount);
        } else {
            offsetHorizontal(outRect, position, spanCount, childCount);
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        outRect.set(mWidth, mHeight, mWidth, mHeight); // |-|-
    }
}
 
Example #18
Source File: StaggeredGridLayoutInfo.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * @param heightSpec the heightSpec used to measure the parent {@link RecyclerSpec}.
 * @return heightSpec of a child that is of span size 1
 */
@Override
public int getChildHeightSpec(int heightSpec, RenderInfo renderInfo) {
  switch (mStaggeredGridLayoutManager.getOrientation()) {
    case StaggeredGridLayoutManager.HORIZONTAL:
      Integer overrideHeight = (Integer) renderInfo.getCustomAttribute(OVERRIDE_SIZE);
      if (overrideHeight != null) {
        return SizeSpec.makeSizeSpec(overrideHeight, EXACTLY);
      }

      final int spanCount = mStaggeredGridLayoutManager.getSpanCount();
      final int spanSize =
          renderInfo.isFullSpan() ? mStaggeredGridLayoutManager.getSpanCount() : 1;

      return SizeSpec.makeSizeSpec(
          spanSize * (SizeSpec.getSize(heightSpec) / spanCount), EXACTLY);
    default:
      return SizeSpec.makeSizeSpec(0, UNSPECIFIED);
  }
}
 
Example #19
Source File: StaggeredGridLayoutInfo.java    From litho with Apache License 2.0 6 votes vote down vote up
@Override
public int approximateRangeSize(
    int firstMeasuredItemWidth,
    int firstMeasuredItemHeight,
    int recyclerMeasuredWidth,
    int recyclerMeasuredHeight) {
  final int spanCount = mStaggeredGridLayoutManager.getSpanCount();
  switch (mStaggeredGridLayoutManager.getOrientation()) {
    case StaggeredGridLayoutManager.HORIZONTAL:
      final int colCount =
          (int) Math.ceil((double) recyclerMeasuredWidth / (double) firstMeasuredItemWidth);

      return colCount * spanCount;
    default:
      final int rowCount =
          (int) Math.ceil((double) recyclerMeasuredHeight / (double) firstMeasuredItemHeight);

      return rowCount * spanCount;
  }
}
 
Example #20
Source File: ExpandableItem.java    From FlexibleAdapter with Apache License 2.0 6 votes vote down vote up
@Override
public void scrollAnimators(@NonNull List<Animator> animators, int position, boolean isForward) {
    if (mAdapter.getRecyclerView().getLayoutManager() instanceof GridLayoutManager ||
            mAdapter.getRecyclerView().getLayoutManager() instanceof StaggeredGridLayoutManager) {
        if (position % 2 != 0)
            AnimatorHelper.slideInFromRightAnimator(animators, itemView, mAdapter.getRecyclerView(), 0.5f);
        else
            AnimatorHelper.slideInFromLeftAnimator(animators, itemView, mAdapter.getRecyclerView(), 0.5f);
    } else {
        // Linear layout
        if (mAdapter.isSelected(position))
            AnimatorHelper.slideInFromRightAnimator(animators, itemView, mAdapter.getRecyclerView(), 0.5f);
        else
            AnimatorHelper.slideInFromLeftAnimator(animators, itemView, mAdapter.getRecyclerView(), 0.5f);
    }
}
 
Example #21
Source File: GridDividerDecoration.java    From pandora with Apache License 2.0 6 votes vote down vote up
private boolean isLastRow(RecyclerView parent, int pos, int spanCount,
                          int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;
        return lines == pos / spanCount + 1;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)
                return true;
        } else {
            if ((pos + 1) % spanCount == 0) {
                return true;
            }
        }
    }
    return false;
}
 
Example #22
Source File: CenteringRecyclerView.java    From centering-recycler-view with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the first visible grid position.
 *
 * @return the first visible position or RecyclerView.NO_POSITION if any error occurs.
 * @see #getFirstCompletelyVisiblePosition()
 */
public int getFirstVisiblePosition() {
    LayoutManager lm = getLayoutManager();
    if (lm instanceof LinearLayoutManager) {
        LinearLayoutManager llm = (LinearLayoutManager) lm;

        return llm.findFirstVisibleItemPosition();
    } else {
        StaggeredGridLayoutManager sglm = (StaggeredGridLayoutManager) lm;
        if (sglm == null) {
            return NO_POSITION;
        }
        int[] firstVisibleItemPositions = sglm.findFirstVisibleItemPositions(null);
        Arrays.sort(firstVisibleItemPositions);

        return firstVisibleItemPositions[0];
    }
}
 
Example #23
Source File: AssemblyItemFactory.java    From assembly-adapter with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
public AssemblyItemFactory<DATA> fullSpan(@NonNull RecyclerView recyclerView) {
    setSpanSize(1);
    fullSpanInStaggeredGrid = false;

    //noinspection ConstantConditions
    if (recyclerView != null) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            setSpanSize(gridLayoutManager.getSpanCount());
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            fullSpanInStaggeredGrid = true;
        }
    }
    return this;
}
 
Example #24
Source File: StaggeredGridRecyclerViewActivity.java    From Android with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_staggered_grid_recycler_view);

    RecyclerView simpleRecyclerView = findViewById(R.id.recycler_view_staggered);

    // add data in the mPersonList
    addPersonData();


    // set layout manager
    StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
    simpleRecyclerView.setLayoutManager(gridLayoutManager);

    // set adapter
    RecyclerViewJavaAdapter adapter = new RecyclerViewJavaAdapter(personModelList,this, Glide.with(this));
    simpleRecyclerView.setAdapter(adapter);

}
 
Example #25
Source File: ThemeSelectionDialogFragment.java    From SAI with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onContentViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onContentViewCreated(view, savedInstanceState);

    setTitle(R.string.installer_select_theme);
    getPositiveButton().setVisibility(View.GONE);
    getNegativeButton().setOnClickListener(v -> dismiss());

    RecyclerView recycler = (RecyclerView) view;
    recycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

    ThemeAdapter adapter = new ThemeAdapter(requireContext());
    adapter.setThemes(Theme.getInstance(requireContext()).getThemes());
    adapter.setOnThemeInteractionListener(this);
    mBillingManager.getDonationStatus().observe(this, adapter::setDonationStatus);
    recycler.setAdapter(adapter);

    revealBottomSheet();
}
 
Example #26
Source File: MainActivity.java    From nextcloud-notes with GNU General Public License v3.0 6 votes vote down vote up
private void initRecyclerView() {
    adapter = new ItemAdapter(this, gridView);
    listView.setAdapter(adapter);

    if (gridView) {
        int spanCount = getResources().getInteger(R.integer.grid_view_span_count);
        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);
        listView.setLayoutManager(gridLayoutManager);
        listView.addItemDecoration(new GridItemDecoration(adapter, spanCount,
                getResources().getDimensionPixelSize(R.dimen.spacer_3x),
                getResources().getDimensionPixelSize(R.dimen.spacer_5x),
                getResources().getDimensionPixelSize(R.dimen.spacer_3x),
                getResources().getDimensionPixelSize(R.dimen.spacer_1x),
                getResources().getDimensionPixelSize(R.dimen.spacer_activity_sides) + getResources().getDimensionPixelSize(R.dimen.spacer_1x)
        ));
    } else {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        listView.setLayoutManager(layoutManager);
        listView.addItemDecoration(new SectionItemDecoration(adapter,
                getResources().getDimensionPixelSize(R.dimen.spacer_activity_sides) + getResources().getDimensionPixelSize(R.dimen.spacer_1x) + getResources().getDimensionPixelSize(R.dimen.spacer_3x) + getResources().getDimensionPixelSize(R.dimen.spacer_2x),
                getResources().getDimensionPixelSize(R.dimen.spacer_5x),
                getResources().getDimensionPixelSize(R.dimen.spacer_1x),
                0
        ));
    }
}
 
Example #27
Source File: DividerGridItemDecoration.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
private boolean isLastColumn(RecyclerView parent, int pos, int spanCount,
                             int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        if ((pos + 1) % spanCount == 0) {
            return true;
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {

        int orientation = ((StaggeredGridLayoutManager) layoutManager)
                .getOrientation();
        if (orientation == StaggeredGridLayoutManager.VERTICAL) {
            // 如果是最后一列,则不需要绘制右边
            if ((pos + 1) % spanCount == 0) {
                return true;
            }
        } else {
            childCount = childCount - childCount % spanCount;
            // 如果是最后一列,则不需要绘制右边
            if (pos >= childCount)
                return true;
        }
    }
    return false;
}
 
Example #28
Source File: SpacesItemDecoration.java    From Pixiv-Shaft with MIT License 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

    outRect.bottom = space;

    int position = parent.getChildAdapterPosition(view);
    if (position == 0 || position == 1) {
        outRect.top = space;
    }

    StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
    if (params.getSpanIndex() % 2 != 0) {
        //右边
        outRect.left = space / 2;
        outRect.right = space;
    } else {
        //左边
        outRect.left = space;
        outRect.right = space / 2;
    }
}
 
Example #29
Source File: FastScroller.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
private boolean isLayoutReversed(final RecyclerView.LayoutManager layoutManager) {
    if (layoutManager instanceof LinearLayoutManager) {
        return ((LinearLayoutManager) layoutManager).getReverseLayout();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        return ((StaggeredGridLayoutManager) layoutManager).getReverseLayout();
    }
    return false;
}
 
Example #30
Source File: DoubanTopActivity.java    From CloudReader with Apache License 2.0 5 votes vote down vote up
private void initRecyclerView() {
    mDouBanTopAdapter = new DouBanTopAdapter(this);
    bindingView.xrvTop.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
    bindingView.xrvTop.setItemAnimator(null);
    bindingView.xrvTop.setAdapter(mDouBanTopAdapter);
    mDouBanTopAdapter.setListener((bean, view) -> OneMovieDetailActivity.start(DoubanTopActivity.this, bean, view));
    bindingView.xrvTop.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            viewModel.handleNextStart();
            loadDouBanTop250();
        }
    });
}