Java Code Examples for androidx.recyclerview.widget.RecyclerView#LayoutManager

The following examples show how to use androidx.recyclerview.widget.RecyclerView#LayoutManager . 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: SearchActivity.java    From Ruisi with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    mainWindow = findViewById(R.id.main_window);
    findViewById(R.id.btn_back).setOnClickListener(this);
    RecyclerView listView = findViewById(R.id.recycler_view);
    searchInput = findViewById(R.id.search_input);
    searchCard = findViewById(R.id.search_card);
    findViewById(R.id.start_search).setOnClickListener(this);
    findViewById(R.id.nav_search).setOnClickListener(this);
    searchInput.setHint("请输入搜索内容!");
    adapter = new SimpleListAdapter(ListType.SERRCH, this, datas);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    listView.setLayoutManager(layoutManager);
    listView.addItemDecoration(new MyListDivider(this, MyListDivider.VERTICAL));
    listView.addOnScrollListener(new LoadMoreListener((LinearLayoutManager) layoutManager, this, 20));
    listView.setAdapter(adapter);
    adapter.changeLoadMoreState(BaseAdapter.STATE_LOAD_NOTHING);
    navTitle = findViewById(R.id.nav_title);
    findViewById(R.id.nav_back).setOnClickListener(this);
    searchInput.setOnEditorActionListener(this);
}
 
Example 2
Source File: PostFragment.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
private void refreshAdapter() {
    int previousPosition = -1;
    if (mLinearLayoutManager != null) {
        previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition();
    } else if (mStaggeredGridLayoutManager != null) {
        int[] into = new int[2];
        previousPosition = mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0];
    }

    RecyclerView.LayoutManager layoutManager = mPostRecyclerView.getLayoutManager();
    mPostRecyclerView.setAdapter(null);
    mPostRecyclerView.setLayoutManager(null);
    mPostRecyclerView.setAdapter(mAdapter);
    mPostRecyclerView.setLayoutManager(layoutManager);

    if (previousPosition > 0) {
        mPostRecyclerView.scrollToPosition(previousPosition);
    }
}
 
Example 3
Source File: HeaderAndFooterWrapper.java    From RecyclerViewHelper with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(@NonNull final RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();

    // GridLayoutManager
    if (manager instanceof GridLayoutManager) {
        final GridLayoutManager gridManager = ((GridLayoutManager) manager);
        gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                // If the current position is header view or footer view, the item occupy two cells,
                // Normal item occupy a cell.
                int itemViewType = getItemViewType(position);
                if (itemViewType == TYPE_HEADER && mIsMergeHeader) {
                    return gridManager.getSpanCount();
                }
                if (itemViewType == TYPE_FOOTER && mIsMergeFooter) {
                    return gridManager.getSpanCount();
                }
                return 1;
            }
        });
    }
}
 
Example 4
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>last partially</b> visible view
 * [for each span], no matter which Layout is.
 *
 * @return the adapter position of the <b>last partially</b> visible item or {@code RecyclerView.NO_POSITION}
 * if there aren't any visible items.
 * @see #findLastCompletelyVisibleItemPosition()
 * @since 5.0.0-rc1
 */
@Override
public int findLastVisibleItemPosition() {
    RecyclerView.LayoutManager layoutManager = getLayoutManager();
    if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGLM = (StaggeredGridLayoutManager) layoutManager;
        int position = staggeredGLM.findLastVisibleItemPositions(null)[0];
        for (int i = 1; i < getSpanCount(); i++) {
            int nextPosition = staggeredGLM.findLastVisibleItemPositions(null)[i];
            if (nextPosition > position) {
                position = nextPosition;
            }
        }
        return position;
    } else {
        return ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
    }
}
 
Example 5
Source File: BaseAdapter.java    From AndroidProject with Apache License 2.0 6 votes vote down vote up
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
    mRecyclerView = recyclerView;
    //用户设置了滚动监听,需要给RecyclerView设置监听
    if (mScrollListener != null) {
        //添加滚动监听
        mRecyclerView.addOnScrollListener(mScrollListener);
    }
    //判断当前的布局管理器是否为空,如果为空则设置默认的布局管理器
    if (mRecyclerView.getLayoutManager() == null) {
        RecyclerView.LayoutManager layoutManager = generateDefaultLayoutManager(mContext);
        if (layoutManager != null) {
            mRecyclerView.setLayoutManager(layoutManager);
        }
    }
}
 
Example 6
Source File: GridMarginsItemDecoration.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
public GridMarginsItemDecoration(RecyclerView recyclerView,
                                 int gridMargins, int singleSpanMargins,
                                 int gridCardRadius, int singleSpanCardRadius) {
    this.gridMargins = gridMargins;
    this.singleSpanMargins = singleSpanMargins;
    this.gridCardRadius = gridCardRadius;
    this.singleSpanCardRadius = singleSpanCardRadius;

    RecyclerView.LayoutManager layoutManager = getLayoutManager(recyclerView);
    if (layoutManager instanceof StaggeredGridLayoutManager) {
        singleSpan = ((StaggeredGridLayoutManager) layoutManager).getSpanCount() == 1;
    } else if (layoutManager instanceof GridLayoutManager) {
        singleSpan = ((GridLayoutManager) layoutManager).getSpanCount() == 1;
    } else if (layoutManager instanceof LinearLayoutManager) { // linear layout manager.
        singleSpan = true;
    } else if (MysplashApplication.isDebug(recyclerView.getContext())) {
        throw new RuntimeException("Null layout manager.");
    } else {
        singleSpan = false;
    }
    setParentPadding(
            recyclerView,
            (singleSpan ? singleSpanMargins : gridMargins) / 2,
            getWindowInset(recyclerView)
    );
}
 
Example 7
Source File: InfoBarAdapter.java    From Field-Book with GNU General Public License v2.0 5 votes vote down vote up
public void configureDropdownArray(String plotId) {
    this.plotId = plotId;
    selectorsView.setHasFixedSize(true);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.context);
    selectorsView.setLayoutManager(layoutManager);

    selectorsView.setAdapter(this);
}
 
Example 8
Source File: SuperDividerItemDecoration.java    From RecyclerViewHelper with Apache License 2.0 5 votes vote down vote up
/**
 * Draw a vertical line.
 *
 * @param canvas Canvas
 * @param parent RecyclerView
 */
private void drawVerticalLine(Canvas canvas, RecyclerView parent) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager == null || mDivider == null) {
        return;
    }

    canvas.save();
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        if (isLastColumn(child, parent)) {
            continue;
        }

        layoutManager.getDecoratedBoundsWithMargins(child, mBounds);
        final int top = mBounds.top;
        final int bottom = mBounds.bottom;
        int right = mBounds.right + Math.round(child.getTranslationX());
        // Vertical GridLayout display in the top, margin left or right mDividerWidth / 2.
        if (mOrientation == VERTICAL && layoutManager instanceof GridLayoutManager) {
            right += mDividerWidth / 2;
        }
        final int left = right - mDividerWidth;
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}
 
Example 9
Source File: ListViewDecoration.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (divider != null) {
        outRect.set(0, 0, 0, divider.getIntrinsicHeight());
    } else {
        int position = parent.getChildAdapterPosition(view); // item position
        boolean hasHeader = false;
        if (parent instanceof SuperRecyclerView) {
            SuperRecyclerView superRecyclerView = (SuperRecyclerView) parent;
            if (superRecyclerView.getHeaderContainer().getChildCount() > position) {
                return;
            }
            int headerCount = superRecyclerView.getHeaderContainer().getChildCount();
            position -= headerCount;
            hasHeader = headerCount > 0;
        }
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {

        } else if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) layoutManager);
            if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
                outRect.right = divideSize;
                if (hasHeader && position == 0) {
                    outRect.left = divideSize;
                }
            } else {
                outRect.bottom = divideSize;
                if (position == 0) {
                    outRect.top = divideSize;
                }
            }

        }
    }
}
 
Example 10
Source File: LayoutManagerUtil.java    From RvHelper with Apache License 2.0 5 votes vote down vote up
private static int caseStaggeredGrid(RecyclerView.LayoutManager layoutManager, boolean findFirst) {
    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
    int[] positions = new int[staggeredGridLayoutManager.getSpanCount()];

    if (findFirst) {
        staggeredGridLayoutManager.findFirstVisibleItemPositions(positions);
        Arrays.sort(positions);
        return positions[0];
    } else {
        staggeredGridLayoutManager.findLastVisibleItemPositions(positions);
        Arrays.sort(positions);
        return positions[positions.length - 1];
    }
}
 
Example 11
Source File: HorizontalScrollCompat.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
public static boolean canAutoRefresh(View view) {
    if (ViewCatcherUtil.isRecyclerView(view)) {
        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager == null) {
            return false;
        }
        int firstVisiblePosition = -1;
        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
            if (linearManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            firstVisiblePosition = linearManager.findFirstVisibleItemPosition();
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) manager;
            if (gridLayoutManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            int[] firstPositions = new int[gridLayoutManager.getSpanCount()];
            gridLayoutManager.findFirstVisibleItemPositions(firstPositions);
            for (int value : firstPositions) {
                if (value == 0) {
                    firstVisiblePosition = 0;
                    break;
                }
            }
        }
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        return adapter != null && firstVisiblePosition == 0;
    }
    return false;
}
 
Example 12
Source File: MyItemDecoration.java    From RendererRecyclerViewAdapter with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(final Rect outRect, final View view, final RecyclerView parent, final RecyclerView.State state) {
	final RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

	final int itemPosition = parent.getChildAdapterPosition(view);
	if (itemPosition != RecyclerView.NO_POSITION) {
		if (layoutManager instanceof GridLayoutManager) {
			final RendererRecyclerViewAdapter adapter = (RendererRecyclerViewAdapter) parent.getAdapter();
			final ViewModel item = adapter.getItem(itemPosition);
			if (item instanceof RecyclerViewModel) {
				outRect.left = dpToPixels(-10);
				outRect.right = dpToPixels(-10);
				outRect.top = dpToPixels(5);
				outRect.bottom = dpToPixels(5);
			} else if (item instanceof CategoryModel) {
				outRect.left = dpToPixels(5);
				outRect.right = dpToPixels(5);
				outRect.top = dpToPixels(10);
				outRect.bottom = dpToPixels(2);
			} else {
				outRect.left = dpToPixels(5);
				outRect.right = dpToPixels(5);
				outRect.top = dpToPixels(5);
				outRect.bottom = dpToPixels(5);
			}
		} else {
			throw new UnsupportedClassVersionError("Unsupported LayoutManager");
		}
	}
}
 
Example 13
Source File: DefaultItemDecoration.java    From SwipeRecyclerView with Apache License 2.0 5 votes vote down vote up
private int getOrientation(RecyclerView.LayoutManager layoutManager) {
    if (layoutManager instanceof LinearLayoutManager) {
        return ((LinearLayoutManager)layoutManager).getOrientation();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        return ((StaggeredGridLayoutManager)layoutManager).getOrientation();
    }
    return RecyclerView.VERTICAL;
}
 
Example 14
Source File: AlbumMediaAdapter.java    From Matisse with Apache License 2.0 5 votes vote down vote up
private int getImageResize(Context context) {
    if (mImageResize == 0) {
        RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager();
        int spanCount = ((GridLayoutManager) lm).getSpanCount();
        int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        int availableWidth = screenWidth - context.getResources().getDimensionPixelSize(
                R.dimen.media_grid_spacing) * (spanCount - 1);
        mImageResize = availableWidth / spanCount;
        mImageResize = (int) (mImageResize * mSelectionSpec.thumbnailScale);
    }
    return mImageResize;
}
 
Example 15
Source File: PagingOverScroller.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    if (mRecyclerView == null)
        return;
    final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
    if (layoutManager == null) {
        stop();
        return; // no layout, cannot scroll.
    }
    disableRunOnAnimationRequests();
    // keep a local reference so that if it is changed during onAnimation method, it won't
    // cause unexpected behaviors
    final OverScroller scroller = mScroller;

    if (scroller.computeScrollOffset()) {
        final int x = scroller.getCurrX();
        final int y = scroller.getCurrY();
        final int dx = x - mLastFlingX;
        final int dy = y - mLastFlingY;
        mLastFlingX = x;
        mLastFlingY = y;
        if (dx != 0 || dy != 0)
            mRecyclerView.scrollBy(dx, dy);
        if (scroller.isFinished()) {
            if (layoutManager instanceof PagingLayoutManager) {
                ((PagingLayoutManager) layoutManager).onFlingFinish();
            }
        } else {
            postOnAnimation();
        }
    }
    enableRunOnAnimationRequests();
}
 
Example 16
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 17
Source File: HomeCategoriesFragment.java    From GooglePlayCloned with Apache License 2.0 5 votes vote down vote up
private void configureRecyclerViews() {
    topCategoriesRecyclerView = view.findViewById(R.id.rv_top_categories);
    topCategoriesRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager horizontalLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    topCategoriesRecyclerView.setLayoutManager(horizontalLayoutManager);

    allCategoriesRecyclerView = view.findViewById(R.id.rv_all_categories);
    allCategoriesRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager verticalLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    allCategoriesRecyclerView.setLayoutManager(verticalLayoutManager);

    loadDataAndSetAdapter();
}
 
Example 18
Source File: LayoutManagerFactory.java    From proteus with Apache License 2.0 4 votes vote down vote up
public RecyclerView.LayoutManager create(@NonNull String type, @NonNull ProteusRecyclerView view, @NonNull ObjectValue config) {
  return builders.get(type).create(view, config);
}
 
Example 19
Source File: EndlessRecyclerOnScrollListener.java    From mimi-reader with Apache License 2.0 4 votes vote down vote up
public EndlessRecyclerOnScrollListener(RecyclerView.LayoutManager layoutManager) {
    this.layoutManager = layoutManager;
}
 
Example 20
Source File: EditorProfileListFragment.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
void startProfilePreferencesActivity(Profile profile, int predefinedProfileIndex)
{
    /*
    PPApplication.logE("EditorProfileListFragment.startProfilePreferencesActivity", "profile="+profile);
    if (profile != null)
        PPApplication.logE("EditorProfileListFragment.startProfilePreferencesActivity", "profile._name="+profile._name);
    PPApplication.logE("EditorProfileListFragment.startProfilePreferencesActivity", "predefinedProfileIndex="+predefinedProfileIndex);
    */

    int editMode;

    if (profile != null)
    {
        // edit profile
        int profilePos = profileListAdapter.getItemPosition(profile);
        //PPApplication.logE("EditorProfileListFragment.startProfilePreferencesActivity", "profilePos="+profilePos);
        /*int last = listView.getLastVisiblePosition();
        int first = listView.getFirstVisiblePosition();
        if ((profilePos <= first) || (profilePos >= last)) {
            listView.setSelection(profilePos);
        }*/
        RecyclerView.LayoutManager lm = listView.getLayoutManager();
        if (lm != null)
            lm.scrollToPosition(profilePos);

        boolean startTargetHelps = getArguments() != null && getArguments().getBoolean(START_TARGET_HELPS_ARGUMENT, false);
        if (startTargetHelps)
            showAdapterTargetHelps();

        editMode = EDIT_MODE_EDIT;
    }
    else
    {
        // add new profile
        editMode = EDIT_MODE_INSERT;
    }

    //PPApplication.logE("EditorProfileListFragment.startProfilePreferencesActivity", "editMode="+editMode);

    // Notify the active callbacks interface (the activity, if the
    // fragment is attached to one) one must start profile preferences
    onStartProfilePreferencesCallback.onStartProfilePreferences(profile, editMode, predefinedProfileIndex);
}