Java Code Examples for androidx.recyclerview.widget.GridLayoutManager#setSpanSizeLookup()

The following examples show how to use androidx.recyclerview.widget.GridLayoutManager#setSpanSizeLookup() . 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: MainActivity.java    From animation-samples with Apache License 2.0 6 votes vote down vote up
private void setupRecyclerView() {
    GridLayoutManager gridLayoutManager = (GridLayoutManager) grid.getLayoutManager();
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            /* emulating https://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B6Okdz75tqQsck9lUkgxNVZza1U/style_imagery_integration_scale1.png */
            switch (position % 6) {
                case 5:
                    return 3;
                case 3:
                    return 2;
                default:
                    return 1;
            }
        }
    });
    grid.addItemDecoration(new GridMarginDecoration(
            getResources().getDimensionPixelSize(R.dimen.grid_item_spacing)));
    grid.setHasFixedSize(true);

}
 
Example 2
Source File: RcvSectionMultiLabelAdapter.java    From RecyclerViewAdapter with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
{
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    if (manager instanceof GridLayoutManager)
    {
        final GridLayoutManager gridManager = ((GridLayoutManager) manager);
        gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
        {
            @Override
            public int getSpanSize(int position)
            {
                if (isInHeadViewPos(position)
                        || isInSectionLabelPos(position)
                        || isInFootViewPos(position)
                        || isInLoadMorePos(position)
                        || isInEmptyStatus())
                    return gridManager.getSpanCount();
                else
                    return 1;
            }
        });
        gridManager.setSpanCount(gridManager.getSpanCount());
    }
}
 
Example 3
Source File: FragmentEndlessScrolling.java    From FlexibleAdapter with Apache License 2.0 6 votes vote down vote up
@Override
protected GridLayoutManager createNewGridLayoutManager() {
    GridLayoutManager gridLayoutManager = new SmoothScrollGridLayoutManager(getActivity(), mColumnCount);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // NOTE: If you use simple integers to identify the ViewType,
            // here, you should use them and not Layout integers
            switch (mAdapter.getItemViewType(position)) {
                case R.layout.recycler_scrollable_expandable_item:
                case R.layout.recycler_scrollable_header_item:
                case R.layout.recycler_scrollable_footer_item:
                case R.layout.recycler_scrollable_layout_item:
                case R.layout.recycler_scrollable_uls_item:
                case R.layout.progress_item:
                    return mColumnCount;
                default:
                    return 1;
            }
        }
    });
    return gridLayoutManager;
}
 
Example 4
Source File: MainActivity.java    From android-instant-apps with Apache License 2.0 6 votes vote down vote up
private void setupRecyclerView() {
    GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
    grid.setLayoutManager(gridLayoutManager);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            /* emulating https://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B6Okdz75tqQsck9lUkgxNVZza1U/style_imagery_integration_scale1.png */
            switch (position % 6) {
                case 5:
                    return 3;
                case 3:
                    return 2;
                default:
                    return 1;
            }
        }
    });
    grid.addItemDecoration(new GridMarginDecoration(
            getResources().getDimensionPixelSize(
                    com.example.android.unsplash.R.dimen.grid_item_spacing)));
    grid.setHasFixedSize(true);

}
 
Example 5
Source File: FragmentExpandableMultiLevel.java    From FlexibleAdapter with Apache License 2.0 6 votes vote down vote up
@Override
protected GridLayoutManager createNewGridLayoutManager() {
    GridLayoutManager gridLayoutManager = new SmoothScrollGridLayoutManager(getActivity(), mColumnCount);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // NOTE: If you use simple integers to identify the ViewType,
            // here, you should use them and not Layout integers
            switch (mAdapter.getItemViewType(position)) {
                case R.layout.recycler_scrollable_layout_item:
                case R.layout.recycler_scrollable_uls_item:
                case R.layout.recycler_header_item:
                case R.layout.recycler_expandable_header_item:
                case R.layout.recycler_expandable_item:
                    return mColumnCount;
                default:
                    return 1;
            }
        }
    });
    return gridLayoutManager;
}
 
Example 6
Source File: BaseRecyclerAdapter.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
    this.display = recyclerView;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                BaseRecyclerAdapter wrapperAdapter = (BaseRecyclerAdapter) recyclerView.getAdapter();
                if (isFullSpanType(wrapperAdapter.getItemViewType(position))) {
                    return gridLayoutManager.getSpanCount();
                } else if (spanSizeLookup != null) {
                    return spanSizeLookup.getSpanSize(position - getItemUpCount());
                }
                return 1;
            }
        });
    }
}
 
Example 7
Source File: DynamicLayoutUtils.java    From dynamic-support with Apache License 2.0 6 votes vote down vote up
/**
 * Sets full span for the positions in case of a {@link GridLayoutManager}.
 * This method must be called after setting an adapter for the recycler view.
 *
 * @param recyclerView The recycler view to set the span size.
 * @param positions The positions supported by the recycler view.
 * @param spanCount The maximum span supported by the layout manager.
 *
 * @see GridLayoutManager#setSpanSizeLookup(GridLayoutManager.SpanSizeLookup)
 */
public static void setFullSpanForPosition(@Nullable final RecyclerView recyclerView,
        @NonNull final Integer[] positions, int spanCount) {
    if (recyclerView == null || recyclerView.getAdapter() == null) {
        return;
    }

    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
        final GridLayoutManager layoutManager =
                (GridLayoutManager) recyclerView.getLayoutManager();
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (Arrays.asList(positions).contains(position)) {
                    return Math.min(Math.abs(spanCount - position % spanCount), spanCount);
                } else {
                    return 1;
                }
            }
        });
    }
}
 
Example 8
Source File: SearchViewModel.java    From Jockey with Apache License 2.0 6 votes vote down vote up
@Bindable
public RecyclerView.LayoutManager getLayoutManager() {
    mColumnCount = ViewUtils.getNumberOfGridColumns(getContext(), R.dimen.grid_width);
    GridLayoutManager layoutManager = new GridLayoutManager(getContext(), mColumnCount);

    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (getAdapter().getItemViewType(position) == mAlbumSection.getTypeId()) {
                return 1;
            } else {
                return mColumnCount;
            }
        }
    });

    return layoutManager;
}
 
Example 9
Source File: BaseDataAdapter.java    From RvHelper with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    if (layoutManager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager);
        if (mGridSpanSizeLookup == null) {
            mSpanCount = gridLayoutManager.getSpanCount();
            mGridSpanSizeLookup = getGridSpanSizeLookup();
        }
        gridLayoutManager.setSpanSizeLookup(mGridSpanSizeLookup);
    }
}
 
Example 10
Source File: NavigationContentAdapter.java    From CloudReader with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    if (manager instanceof GridLayoutManager) {
        final GridLayoutManager gridManager = ((GridLayoutManager) manager);
        gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = getItemViewType(position);
                /**
                 * 根据GridLayoutManager的getSpanSize方法可以动态的设置item跨列数
                 * 需要设置:4个参数的GridLayoutManager
                 * new GridLayoutManager(getActivity(),6,GridLayoutManager.VERTICAL,false);
                 * 这里的6(自己设置的最好设置成偶数)就相当于分母,6默认显示一整行(1列),下面的3 和2 就相当于分子,返回3就是(1/2)所以此类型对应的是2列,返回2就是(1/3)所以此类型对应的是3列
                 * */
                switch (type) {
                    case StickyHeaderHandler.TYPE_STICKY_VIEW:
                        // title栏显示一列
                        return gridManager.getSpanCount();
                    case TYPE_CONTENT:
                        // 内容栏显示2列
                        return 3;
                    default:
                        //默认显示2列
                        return 3;
                }
            }
        });
    }
}
 
Example 11
Source File: SubsonicFragment.java    From Audinaut with GNU General Public License v3.0 5 votes vote down vote up
private GridLayoutManager getGridLayoutManager(RecyclerView recyclerView) {
    final int columns = getRecyclerColumnCount();
    GridLayoutManager gridLayoutManager = new GridLayoutManager(context, columns);

    GridLayoutManager.SpanSizeLookup spanSizeLookup = getSpanSizeLookup(gridLayoutManager);
    if (spanSizeLookup != null) {
        gridLayoutManager.setSpanSizeLookup(spanSizeLookup);
    }
    RecyclerView.ItemDecoration itemDecoration = getItemDecoration();
    recyclerView.addItemDecoration(itemDecoration);
    return gridLayoutManager;
}
 
Example 12
Source File: FrageForums.java    From Ruisi with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    userImg = mRootView.findViewById(R.id.img);
    formsList = mRootView.findViewById(R.id.recycler_view);
    formsList.setClipToPadding(false);
    formsList.setPadding(0, 0, 0, (int) getResources().getDimension(R.dimen.bottombarHeight));
    mRootView.findViewById(R.id.search).setOnClickListener(this);
    adapter = new ForumsAdapter(getActivity());

    int spanCount = Math.max(4, DimenUtils.px2dip(getResources(), Resources.getSystem().getDisplayMetrics().widthPixels) / 75);

    GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), spanCount);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int type = adapter.getItemViewType(position);
            if (type == ForumsAdapter.TYPE_HEADER || type == ForumsAdapter.TYPE_WATER) {
                return spanCount;
            } else {
                // 4 / 1 = 4 列
                return 1;
            }
        }
    });
    userImg.setOnClickListener(this);
    formsList.setLayoutManager(layoutManager);
    formsList.setAdapter(adapter);
    return mRootView;
}
 
Example 13
Source File: AlbumListFragment.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_library_page, container, false);
    mRecyclerView = view.findViewById(R.id.library_page_list);

    int numColumns = ViewUtils.getNumberOfGridColumns(getActivity(), R.dimen.grid_width);

    GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), numColumns);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return mAlbums.isEmpty() ? numColumns : 1;
        }
    });
    mRecyclerView.setLayoutManager(layoutManager);

    mRecyclerView.addItemDecoration(new BackgroundDecoration());
    mRecyclerView.addItemDecoration(new GridSpacingDecoration(
            (int) getResources().getDimension(R.dimen.grid_margin), numColumns));

    if (mAdapter == null) {
        setupAdapter();
    } else {
        mRecyclerView.setAdapter(mAdapter);
    }

    int paddingH = (int) getActivity().getResources().getDimension(R.dimen.global_padding);
    view.setPadding(paddingH, 0, paddingH, 0);

    return view;
}
 
Example 14
Source File: MainActivity.java    From gallery-app-android with MIT License 5 votes vote down vote up
private void createLayoutManager() {
  layoutManager = new GridLayoutManager(this, GRID_SPAN_COUNT);

  layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override public int getSpanSize(int position) {
      return adapter.isSection(position) ? GRID_SPAN_COUNT : 1;
    }
  });
}
 
Example 15
Source File: Example5Fragment.java    From SectionedRecyclerViewAdapter with MIT License 5 votes vote down vote up
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container,
                         @Nullable final Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.fragment_ex5, container, false);

    sectionedAdapter = new SectionedRecyclerViewAdapter();

    final LoadMoviesUseCase loadMoviesUseCase = new LoadMoviesUseCase();
    sectionedAdapter.addSection(new MovieSection(getString(R.string.top_rated_movies_topic),
            loadMoviesUseCase.execute(requireContext(), R.array.top_rated_movies), this));
    sectionedAdapter.addSection(new MovieSection(getString(R.string.most_popular_movies_topic),
            loadMoviesUseCase.execute(requireContext(), R.array.most_popular_movies), this));

    final RecyclerView recyclerView = view.findViewById(R.id.recyclerview);

    final GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
    glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(final int position) {
            if (sectionedAdapter.getSectionItemViewType(position) == SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER) {
                return 2;
            }
            return 1;
        }
    });
    recyclerView.setLayoutManager(glm);
    recyclerView.setAdapter(sectionedAdapter);

    return view;
}
 
Example 16
Source File: TimelineAdapter.java    From leafpicrevived with GNU General Public License v3.0 5 votes vote down vote up
public void setGridLayoutManager(GridLayoutManager gridLayoutManager) {
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            TimelineItem timelineItem = getItem(position);

            // If we have a header item, occupy the entire width
            if (timelineItem.getTimelineType() == TimelineItem.TYPE_HEADER)
                return timelineGridSize;

            // Else, a media item takes up a single space
            return 1;
        }
    });
}
 
Example 17
Source File: FragmentOverall.java    From FlexibleAdapter with Apache License 2.0 5 votes vote down vote up
@Override
protected GridLayoutManager createNewGridLayoutManager() {
    mAdapter.setAnimationEntryStep(false);
    GridLayoutManager gridLayoutManager = new SmoothScrollGridLayoutManager(getActivity(), mColumnCount);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return mAdapter.getItem(position).getSpanSize(mColumnCount, position);
        }
    });
    return gridLayoutManager;
}
 
Example 18
Source File: DiscoverMoviesFragment.java    From PopularMovies with MIT License 4 votes vote down vote up
private void setupListAdapter() {
    RecyclerView recyclerView = getActivity().findViewById(R.id.rv_movie_list);
    final DiscoverMoviesAdapter discoverMoviesAdapter =
            new DiscoverMoviesAdapter(viewModel);
    final GridLayoutManager layoutManager =
            new GridLayoutManager(getActivity(), getResources().getInteger(R.integer.span_count));

    // draw network status and errors messages to fit the whole row(3 spans)
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch (discoverMoviesAdapter.getItemViewType(position)) {
                case R.layout.item_network_state:
                    return layoutManager.getSpanCount();
                default:
                    return 1;
            }
        }
    });

    // setup recyclerView
    recyclerView.setAdapter(discoverMoviesAdapter);
    recyclerView.setLayoutManager(layoutManager);
    ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getActivity(), R.dimen.item_offset);
    recyclerView.addItemDecoration(itemDecoration);

    // observe paged list
    viewModel.getPagedList().observe(getViewLifecycleOwner(), new Observer<PagedList<Movie>>() {
        @Override
        public void onChanged(PagedList<Movie> movies) {
            discoverMoviesAdapter.submitList(movies);
        }
    });

    // observe network state
    viewModel.getNetworkState().observe(getViewLifecycleOwner(), new Observer<Resource>() {
        @Override
        public void onChanged(Resource resource) {
            discoverMoviesAdapter.setNetworkState(resource);
        }
    });
}
 
Example 19
Source File: LoadMoreFragment.java    From RendererRecyclerViewAdapter with Apache License 2.0 4 votes vote down vote up
@Nullable
	@Override
	public View onCreateView(final LayoutInflater inflater,
	                         @Nullable final ViewGroup container,
	                         @Nullable final Bundle savedInstanceState) {

		final View view = inflater.inflate(R.layout.fragment_list, container, false);

		mAdapter = new RendererRecyclerViewAdapter();
		mAdapter.enableDiffUtil();


//		mAdapter.setLoadMoreModel(new YourLoadMoreModel()); /* you can change the LoadMoreModel if needed */
		mAdapter.registerRenderer(new LoadMoreViewBinder(R.layout.item_load_more));
		mAdapter.registerRenderer(new ViewBinder<>(R.layout.item_simple_square, SimpleViewModel.class,
				(model, finder, payloads) -> finder.setText(R.id.text, model.getText())
		));
//		adapter.registerRenderer(...);
//		adapter.registerRenderer(...);

		mAdapter.setItems(mYourDataProvider.getLoadMoreItems());

		mLayoutManager = new GridLayoutManager(getContext(), COLUMNS_COUNT);
		mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
			@Override
			public int getSpanSize(final int position) {
				final Type type = mAdapter.getType(position);
				if (type.equals(SimpleViewModel.class)) {
					return 1;
				}
				return COLUMNS_COUNT;
			}
		});

		mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
		mRecyclerView.setAdapter(mAdapter);
		mRecyclerView.setLayoutManager(mLayoutManager);
		mRecyclerView.addItemDecoration(new ItemOffsetDecoration(10));
		mRecyclerView.addOnScrollListener(new EndlessScrollListener() {
			@Override
			public void onLoadMore(final int page, final int totalItemsCount) {
				Log.d("#####", "onLoadMore " + page);
				mAdapter.showLoadMore();
//				mAdapter.hideLoadMore(); /* if you need force hide progress or call setItems() */
				mYourDataProvider.getLoadMoreItems(list -> getActivity().runOnUiThread(() -> mAdapter.setItems(list)));
			}
		});

		return view;
	}
 
Example 20
Source File: EasyPhotosActivity.java    From EasyPhotos with Apache License 2.0 4 votes vote down vote up
private void initView() {

        if (albumModel.getAlbumItems().isEmpty()) {
            Toast.makeText(this, R.string.no_photos_easy_photos, Toast.LENGTH_LONG).show();
            if (Setting.isShowCamera) {
                launchCamera(Code.REQUEST_CAMERA);
            } else {
                finish();
            }
            return;
        }

        EasyPhotos.setAdListener(this);
        if (Setting.hasPhotosAd()) {
            findViewById(R.id.m_tool_bar_bottom_line).setVisibility(View.GONE);
        }
        ivCamera = findViewById(R.id.fab_camera);
        if (Setting.isShowCamera && Setting.isBottomRightCamera()) {
            ivCamera.setVisibility(View.VISIBLE);
        }
        if (!Setting.showPuzzleMenu) {
            findViewById(R.id.tv_puzzle).setVisibility(View.GONE);
        }
        mSecondMenus = findViewById(R.id.m_second_level_menu);
        int columns = getResources().getInteger(R.integer.photos_columns_easy_photos);
        tvAlbumItems = findViewById(R.id.tv_album_items);
        tvAlbumItems.setText(albumModel.getAlbumItems().get(0).name);
        tvDone = findViewById(R.id.tv_done);
        rvPhotos = findViewById(R.id.rv_photos);
        ((SimpleItemAnimator) rvPhotos.getItemAnimator()).setSupportsChangeAnimations(false);
        //去除item更新的闪光
        photoList.clear();
        photoList.addAll(albumModel.getCurrAlbumItemPhotos(0));
        int index = 0;
        if (Setting.hasPhotosAd()) {
            photoList.add(index, Setting.photosAdView);
        }
        if (Setting.isShowCamera && !Setting.isBottomRightCamera()) {
            if (Setting.hasPhotosAd()) index = 1;
            photoList.add(index, null);
        }
        photosAdapter = new PhotosAdapter(this, photoList, this);

        gridLayoutManager = new GridLayoutManager(this, columns);
        if (Setting.hasPhotosAd()) {
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (position == 0) {
                        return gridLayoutManager.getSpanCount();//独占一行
                    } else {
                        return 1;//只占一行中的一列
                    }
                }
            });
        }
        rvPhotos.setLayoutManager(gridLayoutManager);
        rvPhotos.setAdapter(photosAdapter);
        tvOriginal = findViewById(R.id.tv_original);
        if (Setting.showOriginalMenu) {
            processOriginalMenu();
        } else {
            tvOriginal.setVisibility(View.GONE);
        }
        tvPreview = findViewById(R.id.tv_preview);

        initAlbumItems();
        shouldShowMenuDone();
        setClick(R.id.iv_album_items, R.id.tv_clear, R.id.iv_second_menu, R.id.tv_puzzle);
        setClick(tvAlbumItems, rootViewAlbumItems, tvDone, tvOriginal, tvPreview, ivCamera);
    }