Java Code Examples for android.support.v7.widget.GridLayoutManager#setSpanSizeLookup()

The following examples show how to use android.support.v7.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: GridFragment.java    From RecyclerView_Divider with Apache License 2.0 6 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    mRecyclerView = (RecyclerView) view;
    GridLayoutManager layoutManager = new GridLayoutManager(view.getContext(), span);
    mRecyclerView.setLayoutManager(layoutManager);
    adapter = new GridAdapter(list);
    mRecyclerView.setAdapter(adapter);
    DividerItemDecoration itemDecoration = new DividerItemDecoration();
    itemDecoration.setDividerLookup(new AgileDividerLookup());
    mRecyclerView.addItemDecoration(itemDecoration);

    if (type == MainActivity.TYPE_AGILE) {
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return span / adapter.getItemViewType(position);
            }
        });
    }

}
 
Example 2
Source File: BindSuperAdapter.java    From LazyRecyclerAdapter with MIT License 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    adapter.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) {
                return (isHeader(position) || isFooter(position) || isRefreshHeader(position))
                        ? gridManager.getSpanCount() : 1;
            }
        });
    }
}
 
Example 3
Source File: MainActivity.java    From easy-header-footer-adapter with MIT License 6 votes vote down vote up
private RecyclerView.LayoutManager getLayoutManager() {
    switch (layoutManagerType) {
        case LayoutManager.LINEAR:
            return new LinearLayoutManager(this);
        case LayoutManager.GRID:
            GridLayoutManager result = new GridLayoutManager(this, 2);

            result.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (position % 3 == 0) { // Something to show
                        return 2;
                    } else {
                        return 1;
                    }
                }
            });

            return result;
        case LayoutManager.STAGGERED_GRID:
            return new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        default:
            throw new IllegalStateException("layoutManagerType has a unknown value: " +
                    layoutManagerType);
    }
}
 
Example 4
Source File: RecommendFragment.java    From LQRBiliBlili with MIT License 6 votes vote down vote up
@Override
public void setRecyclerAdapter(RecommendMultiItemAdapter adapter) {
    GridLayoutManager gridLayoutManager = new GridLayoutManager(_mActivity, 2);
    mRecyclerView.setLayoutManager(gridLayoutManager);
    mRecyclerView.setAdapter(adapter);
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // 上拉加载更多视图
            if (position == adapter.getData().size()) {
                return 2;
            }
            return adapter.getData().get(position).getSpanSize();
        }
    });
}
 
Example 5
Source File: CursorLoaderGridRecyclerFragment.java    From aptoide-client with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setLayoutManager(final RecyclerView recyclerView) {
    final GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerView.getContext(), getColumnSize() * getColumnMultiplier());
    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {

            if(!(recyclerView.getAdapter() instanceof SpannableRecyclerAdapter)){
                throw new IllegalStateException("RecyclerView adapter must extend SpannableRecyclerAdapter");
            }

            return AptoideUtils.UI.getSpanSize(recyclerView, position);
        }
    });

    gridLayoutManager.setSpanCount(getColumnSize() * getColumnMultiplier());
    recyclerView.setLayoutManager(gridLayoutManager);
}
 
Example 6
Source File: ChannelManagerActivity.java    From NetEasyNews with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_channel_manager);
    getIntentData();
    context = this;
    initToolbar();
    listDataSave = new ListDataSave(this, "channel");
    mRecyclerView = (RecyclerView) findViewById(com.example.channelmanager.R.id.id_tab_recycler_view);
    GridLayoutManager gridLayout = new GridLayoutManager(context, 4);
    gridLayout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            boolean isHeader = mRecyclerAdapter.getItemViewType(position) == IChannelType.TYPE_MY_CHANNEL_HEADER ||
                    mRecyclerAdapter.getItemViewType(position) == IChannelType.TYPE_REC_CHANNEL_HEADER;
            return isHeader ? 4 : 1;
        }
    });
    mRecyclerView.setLayoutManager(gridLayout);
    mRecyclerView.addItemDecoration(new GridItemDecoration(APPConst.ITEM_SPACE));
    initData();
    mRecyclerAdapter = new ChannelAdapter(context, mRecyclerView, mMyChannelList, mRecChannelList, 1, 1);
    mRecyclerAdapter.setChannelItemClickListener(this);
    mRecyclerView.setAdapter(mRecyclerAdapter);
}
 
Example 7
Source File: SearchFragment.java    From Pasta-for-Spotify with Apache License 2.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_recycler, container, false);
    unbinder = ButterKnife.bind(this, rootView);

    if (list == null) list = new ArrayList<>();
    else spinner.setVisibility(View.GONE);

    adapter = new SectionedOmniAdapter((AppCompatActivity) getActivity(), list);
    manager = new GridLayoutManager(getContext(), PreferenceUtils.getColumnNumber(getContext(), false));
    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (adapter.getItemViewType(position) == 0 || adapter.getItemViewType(position) == 4)
                return manager.getSpanCount();
            else return 1;
        }
    });
    recycler.setLayoutManager(manager);
    recycler.setAdapter(adapter);

    return rootView;
}
 
Example 8
Source File: HeaderAndFooterWrapper.java    From styT with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    //为了兼容GridLayout
    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) {
                if (isHeaderViewPos(position)) {
                    return gridLayoutManager.getSpanCount();
                } else if (isFooterViewPos(position)) {
                    return gridLayoutManager.getSpanCount();
                }
                if (spanSizeLookup != null)
                    return spanSizeLookup.getSpanSize(position);
                return 1;
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }

}
 
Example 9
Source File: RcvSectionAdapter.java    From FamilyChat 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 (isInSectionPos(position)
                        || isInHeadViewPos(position)
                        || isInFootViewPos(position)
                        || isInLoadMorePos(position)
                        || isInEmptyStatus())
                    return gridManager.getSpanCount();
                else
                    return 1;
            }
        });
        gridManager.setSpanCount(gridManager.getSpanCount());
    }
}
 
Example 10
Source File: MultipleHeaderBottomFragment.java    From AndroidRecyclerViewDemo with Apache License 2.0 6 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (type == TYPE_GRID_LAYOUT) {
        gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(gridLayoutManager);//这里用线性宫格显示 类似于grid view
    } else if (type == TYPE_STAGGERED_GRID_LAYOUT) {
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
    } else {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//这里用线性显示 类似于list view
    }
    mAdapter = new HeaderBottomItemAdapter(getActivity());
    mRecyclerView.setAdapter(mAdapter);
    if (gridLayoutManager != null) {//设置头部及底部View占据整行空间
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (mAdapter.isHeaderView(position) || mAdapter.isBottomView(position)) ? gridLayoutManager.getSpanCount() : 1;
            }
        });
    }
}
 
Example 11
Source File: VoiceToolsActivity.java    From Android-Application-ZJB with Apache License 2.0 6 votes vote down vote up
private void initView() {
    mAdapter = new VoiceToolsAdapter();
    mAdapter.setOnVoicePlayListener(this);
    GridLayoutManager mLayoutManager = new GridLayoutManager(this, 4);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (position == 0 || position == 9) {
                return 4;
            }
            return 1;
        }
    });
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setItemAnimator(null);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setAdapter(mAdapter);
}
 
Example 12
Source File: RecyclerArrayAdapter.java    From YCRefreshView with Apache License 2.0 5 votes vote down vote up
/**
 * 页面进入时,显示RecyclerView,调用onAttachedToRecyclerView,做一些注册工作;
 * 页面退出时,销毁RecyclerView,调用onDetachedFromRecyclerView,做一些解注册和其他资源回收的操作。
 * @param recyclerView                      recyclerView
 */
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    //增加对RecyclerArrayAdapter奇葩操作的修复措施
    registerAdapterDataObserver(new FixDataObserver(recyclerView));
    if (mSetHeaderAndFooterSpan){
        //下面是处理grid试图上拉加载的问题
        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) {
                    // 如果当前是footer的位置,那么该item占据2个单元格,正常情况下占据1个单元格
                    int itemViewType = getItemViewType(position);
                    //注意,具体可以看DefaultEventDelegate类中的EventFooter类代码
                    //如果是头部header或者底部footer,则直接
                    if (itemViewType>=DefaultEventDelegate.EventFooter.HIDE &&
                            itemViewType<=DefaultEventDelegate.EventFooter.SHOW_NO_MORE){
                        RefreshLogUtils.d("onAttachedToRecyclerView----这个是header和footer");
                        return gridManager.getSpanCount();
                    }else {
                        RefreshLogUtils.d("onAttachedToRecyclerView----");
                        return 1;
                    }
                }
            });
        }
    }
}
 
Example 13
Source File: ArchiveFragment.java    From tribbble with Apache License 2.0 5 votes vote down vote up
@Override public void bind(List<Shot> shots) {
  mRecyclerView.setAdapter(mAdapter = new ArchiveAdapter(shots, mRecyclerView, mItemListener));
  GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
  layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override public int getSpanSize(int position) {
      return mAdapter.getItemViewType(position) == ArchiveAdapter.TYPE_SHOT ? 1 : 2;
    }
  });
  mRecyclerView.setLayoutManager(layoutManager);
}
 
Example 14
Source File: GridLoadMoreActivity.java    From LightAdapter with Apache License 2.0 5 votes vote down vote up
private void init() {
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    adapter = new LightAdapter();
    GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
    layoutManager.setSpanSizeLookup(adapter.spanSizeLookup(2, 2));
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
    swipeRefreshLayout.setOnRefreshListener(this);
}
 
Example 15
Source File: Folders.java    From CatanArchitecture with Apache License 2.0 5 votes vote down vote up
@Override protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.folders);

  foldersRecyclerView = (RecyclerView) findViewById(R.id.folders);

  foldersRecyclerView.setHasFixedSize(true);

  int squareSideLengthInPixels = getResources().getDimensionPixelSize(R.dimen.square_side_length);
  float radius = radius(squareSideLengthInPixels);
  float adjacent = adjacent(radius);

  int spacingInPixels = -(Math.round(radius - adjacent));

  foldersRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

  layoutManager = new GridLayoutManager(this, SPAN_COUNT, LinearLayoutManager.VERTICAL, false);
  layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override public int getSpanSize(int position) {
      return position % 3 == 0 ? 2 : 1;
    }
  });

  foldersRecyclerView.setLayoutManager(layoutManager);

  eventsPort = obtainEventsPort();
  foldersPresenter = new FoldersPresenter(this, eventsPort);
}
 
Example 16
Source File: HomeFragment.java    From mosby with Apache License 2.0 5 votes vote down vote up
@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.fragment_home, container, false);
  unbinder = ButterKnife.bind(this, view);

  adapter = new HomeAdapter(inflater, this);
  layoutManager = new GridLayoutManager(getActivity(), spanCount);
  layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override public int getSpanSize(int position) {

      int viewType = adapter.getItemViewType(position);
      if (viewType == HomeAdapter.VIEW_TYPE_LOADING_MORE_NEXT_PAGE
          || viewType == HomeAdapter.VIEW_TYPE_SECTION_HEADER) {
        return spanCount;
      }

      return 1;
    }
  });

  /*
  recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount,
      getResources().getDimensionPixelSize(R.dimen.grid_spacing), true));
      */

  recyclerView.setAdapter(adapter);
  recyclerView.setLayoutManager(layoutManager);

  return view;
}
 
Example 17
Source File: CoachRankActivity.java    From Android-Application-ZJB with Apache License 2.0 5 votes vote down vote up
private void initView() {
    mAdapter = new CoachRankAdapter();
    GridLayoutManager layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (position == 0) {
                return 3;
            }
            return 1;
        }
    });
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.addItemDecoration(new GridSpacingItemDecoration((int) PixelUtil.dp2px(20), 3, 1));
}
 
Example 18
Source File: EBookCategoryDetailActivity.java    From MaterialHome with Apache License 2.0 5 votes vote down vote up
@Override
protected void initEvents() {
    major = getIntent().getStringExtra("major");
    gender = getIntent().getStringExtra("gender");
    setTitle(major == null ? "category" : major);

    int spanCount = getResources().getInteger(R.integer.home_span_count);
    eBookRankPresenter = new EBookPresenterImpl(this);
    bookInfoResponses = new ArrayList<>();
    mSwipeRefreshLayout.setColorSchemeResources(R.color.recycler_color1, R.color.recycler_color2,
            R.color.recycler_color3, R.color.recycler_color4);

    //设置布局管理器
    mLayoutManager = new GridLayoutManager(this, spanCount);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return mListAdapter.getItemColumnSpan(position);
        }
    });
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(mLayoutManager);

    //设置adapter
    mListAdapter = new EBookListAdapter(this, bookInfoResponses, spanCount);
    mRecyclerView.setAdapter(mListAdapter);

    //设置Item增加、移除动画
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.addOnScrollListener(new RecyclerViewScrollDetector());
    mSwipeRefreshLayout.setOnRefreshListener(this);
    onRefresh();
}
 
Example 19
Source File: MainFragment.java    From outlay with Apache License 2.0 4 votes vote down vote up
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ((DrawerActivity) getActivity()).setupDrawer(currentUser);

    initStaticContent();

    GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4);
    gridLayoutManager.setSpanSizeLookup(onSpanSizeLookup);
    categoriesGrid.setLayoutManager(gridLayoutManager);
    adapter = new CategoriesGridAdapter();
    categoriesGrid.setAdapter(adapter);

    adapter.attachNumpadEditable(new NumpadEditable() {
        @Override
        public String getText() {
            return amountText.getText().toString();
        }

        @Override
        public void setText(String text) {
            amountText.setText(text);
        }
    }, validator);
    adapter.setOnCategoryClickListener(category -> {
        if (validator.valid(amountText.getText().toString())) {
            Expense e = new Expense();
            e.setCategory(category);
            if (!TextUtils.isEmpty(expenseNote.getText())) {
                e.setNote(expenseNote.getText().toString());
            }
            e.setAmount(new BigDecimal(amountText.getText().toString()));
            e.setReportedWhen(selectedDate);
            analytics().trackExpenseCreated(e);
            if(!TextUtils.isEmpty(e.getNote())) {
                analytics().trackNoteEntered();
            }
            presenter.createExpense(e);
            cleanAmountInput();
        } else {
            validator.onInvalidInput(amountText.getText().toString());
        }
    });


    expensesAdapter = new TimelineExpensesAdapter();
    expensesAdapter.setOnExpenseClickListener(e -> Navigator.goToExpenseDetails(getActivity(), e, true));
    decor = new StickyHeaderDecoration(expensesAdapter);
    LinearLayoutManager stickyHeaderLayoutManager = new LinearLayoutManager(getActivity());
    timelineRecycler.setLayoutManager(stickyHeaderLayoutManager);
    timelineRecycler.setAdapter(expensesAdapter);
    timelineRecycler.addItemDecoration(decor);
}
 
Example 20
Source File: ListPopularMoviesActivity.java    From Movie-Check with Apache License 2.0 4 votes vote down vote up
@Override
public void showMovies(final List<Movie> newMovieList) {
    if (movieList == null) {
        movieList = newMovieList;
    } else {
        movieList.addAll(newMovieList);
    }
    linearLayoutAnyFounded.setVisibility(View.GONE);
    linearLayoutLoadFailed.setVisibility(View.GONE);
    recyclerViewMovies.setVisibility(View.VISIBLE);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, columns, GridLayoutManager.VERTICAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return position >= movieList.size() ? columns : 1;
        }
    });
    recyclerViewMovies.setLayoutManager(layoutManager);
    recyclerViewMovies.setItemAnimator(new DefaultItemAnimator());
    listViewAdapter = new ListViewAdapterWithPagination(
            new MovieListAdapter(movieList, new OnItemClickListener<Movie>() {
                @Override
                public void onClick(Movie movie, View view) {
                    startActivity(MovieProfileActivity.newIntent(ListPopularMoviesActivity.this, movie), ActivityOptionsCompat.makeSceneTransitionAnimation(ListPopularMoviesActivity.this, view.findViewById(R.id.imageview_poster), "moviePoster").toBundle());
                }

                @Override
                public void onLongClick(Movie movie, View view) {

                }
            }
            ),
            new OnShowMoreListener() {
                @Override
                public void showMore() {
                    scrollToItem = layoutManager.findFirstVisibleItemPosition();
                    presenter.loadMovies(++page);
                }
            },
            itensPerPage);
    recyclerViewMovies.setAdapter(listViewAdapter);
    recyclerViewMovies.scrollToPosition(scrollToItem);
}