Java Code Examples for android.support.v7.widget.RecyclerView#LayoutManager

The following examples show how to use android.support.v7.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: ItemTouchHelperCallback.java    From SwipeRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY,
    int actionState, boolean isCurrentlyActive) {
    // 判断当前是否是swipe方式:侧滑。
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        //1.ItemView--ViewHolder; 2.侧滑条目的透明度程度关联谁?dX(delta增量,范围:当前条目-width~width)。
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        float alpha = 1;
        if (layoutManager instanceof LinearLayoutManager) {
            int orientation = ((LinearLayoutManager)layoutManager).getOrientation();
            if (orientation == LinearLayoutManager.HORIZONTAL) {
                alpha = 1 - Math.abs(dY) / viewHolder.itemView.getHeight();
            } else if (orientation == LinearLayoutManager.VERTICAL) {
                alpha = 1 - Math.abs(dX) / viewHolder.itemView.getWidth();
            }
        }
        viewHolder.itemView.setAlpha(alpha);//1~0
    }
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
 
Example 2
Source File: ScrollPageHelper.java    From YCBanner with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) {
    int[] out = new int[2];
    if (layoutManager.canScrollHorizontally()) {
        if (gravity == Gravity.START) {
            out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager), false);
        } else { // END
            out[0] = distanceToEnd(targetView, getHorizontalHelper(layoutManager), false);
        }
    } else {
        out[0] = 0;
    }
    if (layoutManager.canScrollVertically()) {
        if (gravity == Gravity.TOP) {
            out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager), false);
        } else { // BOTTOM
            out[1] = distanceToEnd(targetView, getVerticalHelper(layoutManager), false);
        }
    } else {
        out[1] = 0;
    }
    return out;
}
 
Example 3
Source File: ChatListAdapter.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
/**
 * 获取指定区域item的高度
 **/
public int getItemsHeight(int start, int end) {
    int height = 0;
    if (start > end || end > datas.size() - 1) {
        return height;
    }
    start = start < 0 ? 0 : start;
    RecyclerView.LayoutManager layoutManager = mListFragment.getLayoutManager();
    for (int i = start; i <= end; i++) {
        View item = layoutManager.findViewByPosition(i);
        if (item != null) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) item.getLayoutParams();
            height += item.getHeight() + item.getPaddingBottom() + item.getPaddingTop() + layoutParams.topMargin + layoutParams.bottomMargin /*item.getBottom() - item.getTop()*/;
                /*int desiredWidth = View.MeasureSpec.makeMeasureSpec(item.getWidth(), View.MeasureSpec.AT_MOST);
                item.measure(desiredWidth, 0); // 计算子项View 的宽高
                height += item.getMeasuredHeight();*/
        }
    }

    return height;
}
 
Example 4
Source File: PagerGridSnapHelper.java    From pager-layoutmanager with Apache License 2.0 6 votes vote down vote up
/**
 * 获取目标控件的位置下标
 * (获取滚动后第一个View的下标)
 *
 * @param layoutManager 布局管理器
 * @param velocityX     X 轴滚动速率
 * @param velocityY     Y 轴滚动速率
 * @return 目标控件的下标
 */
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager,
                                  int velocityX, int velocityY) {
    int target = RecyclerView.NO_POSITION;
    Loge("findTargetSnapPosition, velocityX = " + velocityX + ", velocityY" + velocityY);
    if (null != layoutManager && layoutManager instanceof PagerGridLayoutManager) {
        PagerGridLayoutManager manager = (PagerGridLayoutManager) layoutManager;
        if (manager.canScrollHorizontally()) {
            if (velocityX > PagerConfig.getFlingThreshold()) {
                target = manager.findNextPageFirstPos();
            } else if (velocityX < -PagerConfig.getFlingThreshold()) {
                target = manager.findPrePageFirstPos();
            }
        } else if (manager.canScrollVertically()) {
            if (velocityY > PagerConfig.getFlingThreshold()) {
                target = manager.findNextPageFirstPos();
            } else if (velocityY < -PagerConfig.getFlingThreshold()) {
                target = manager.findPrePageFirstPos();
            }
        }
    }
    Loge("findTargetSnapPosition, target = " + target);
    return target;
}
 
Example 5
Source File: SupportGridItemDecoration.java    From BookReader 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 6
Source File: LayoutManagers.java    From MVVM-JueJin with MIT License 5 votes vote down vote up
/**
 * A {@link StaggeredGridLayoutManager} with the given spanCount and orientation.
 */
public static LayoutManagerFactory staggeredGrid(final int spanCount, @Orientation final int orientation) {
    return new LayoutManagerFactory() {
        @Override
        public RecyclerView.LayoutManager create(RecyclerView recyclerView) {
            return new StaggeredGridLayoutManager(spanCount, orientation);
        }
    };
}
 
Example 7
Source File: StagesFragment.java    From triviums with MIT License 5 votes vote down vote up
private void initRecyclerView() {
    adapter = new StagesAdapter(getActivity(), totalPage, listener);
    recyclerView = binding.stageView;
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}
 
Example 8
Source File: ItemDragHelperCallback.java    From NetEasyNews with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    int dragFlags;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager || layoutManager instanceof StaggeredGridLayoutManager) {
        dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END;
    } else {
        dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
    }
    int swipeFlags = 0;
    return makeMovementFlags(dragFlags, swipeFlags);
}
 
Example 9
Source File: MyRoutesTab.java    From kute with Apache License 2.0 5 votes vote down vote up
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    fab_add=(FloatingActionButton)v.findViewById(R.id.fab);
    fab_add.setOnClickListener(this);
    pg=(ProgressBar)v.findViewById(R.id.progressBar);
    my_routes_recycler = (RecyclerView) v.findViewById(R.id.routeRecycler);
    recycler_adapter = new MyRoutesRecyclerAdapter(my_routes_list,"My",this);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    my_routes_recycler.setLayoutManager(mLayoutManager);
    my_routes_recycler.setItemAnimator(new DefaultItemAnimator());
    my_routes_recycler.setAdapter(recycler_adapter);
    routes_async_load=new LoadPersonRoutesAsyncTask(this);
    routes_async_load.execute(getActivity().getSharedPreferences("user_credentials", 0).getString("Id", null));
}
 
Example 10
Source File: RecyclerViewTV.java    From AndroidTVWidget with Apache License 2.0 5 votes vote down vote up
/**
 * 最后的位置.
 */
public int findLastVisibleItemPosition() {
    RecyclerView.LayoutManager layoutManager = getLayoutManager();
    if (layoutManager != null) {
        if (layoutManager instanceof LinearLayoutManager) {
            return ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        }
        if (layoutManager instanceof GridLayoutManager) {
            return ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
        }
    }
    return RecyclerView.NO_POSITION;
}
 
Example 11
Source File: RecyclerViewOnScroll.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    if (layoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LayoutManagerType.LinearLayout;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LayoutManagerType.GridLayout;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LayoutManagerType.StaggeredGridLayout;
        } else {
            throw new RuntimeException(
                    "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }

    switch (layoutManagerType) {
        case LinearLayout:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GridLayout:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case StaggeredGridLayout:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            break;
    }
}
 
Example 12
Source File: GridDividerDecorator.java    From AlbumSelector with Apache License 2.0 5 votes vote down vote up
private int getSpanCount(RecyclerView parent) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        return ((GridLayoutManager) layoutManager).getSpanCount();
    } else {
        return -1;
    }
}
 
Example 13
Source File: AbstractAmiiboInformationFragment.java    From amiibo with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    ButterKnife.bind(this, view);

    RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity());
    _recycler.setLayoutManager(manager);

    _recycler.setAdapter(getAdapter());
}
 
Example 14
Source File: GridManager.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
private static void setupLayoutManagerInternal(RecyclerView recyclerView, final ColumnInfo columnInfo) {
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        ((GridLayoutManager) layoutManager).setSpanCount(columnInfo.count);
    }
}
 
Example 15
Source File: SegmentBitmapsFragment.java    From ml with Apache License 2.0 4 votes vote down vote up
@Override
protected RecyclerView.LayoutManager onCreateLayoutManager() {
    return null;
}
 
Example 16
Source File: StartSnapHelper.java    From GankGirl with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
    VegaLayoutManager custLayoutManager = (VegaLayoutManager) layoutManager;
    return custLayoutManager.findSnapView();
}
 
Example 17
Source File: SearchIView.java    From MeiZiNews with MIT License 3 votes vote down vote up
private void initRecycler_list(AppCompatActivity appCompatActivity){

        recycler_list.setHasFixedSize(true);


        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(appCompatActivity);
        recycler_list.setLayoutManager(mLayoutManager);
        recycler_list.setItemAnimator(new DefaultItemAnimator());

        mySearchAdapter = new MySearchAdapter(appCompatActivity, R.layout.fragment_dev_week_item);
        recycler_list.setAdapter(mySearchAdapter);
    }
 
Example 18
Source File: CollectIView.java    From MeiZiNews with MIT License 3 votes vote down vote up
private void initRecycler_list(final Context context) {

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
        recycler_list.setLayoutManager(mLayoutManager);
        recycler_list.setItemAnimator(new DefaultItemAnimator());

        collectAdapter = new CollectAdapter(context);

        recycler_list.setAdapter(collectAdapter);
    }
 
Example 19
Source File: SnapHelper.java    From GridPagerSnapHelper with Apache License 2.0 2 votes vote down vote up
/**
 * Override to provide a particular adapter target position for snapping.
 *
 * @param layoutManager the {@link RecyclerView.LayoutManager} associated with the attached
 *                      {@link RecyclerView}
 * @param velocityX     fling velocity on the horizontal axis
 * @param velocityY     fling velocity on the vertical axis
 * @return the target adapter position to you want to snap or {@link RecyclerView#NO_POSITION}
 * if no snapping should happen
 */
public abstract int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX,
                                           int velocityY);
 
Example 20
Source File: BaseExampleActivity.java    From TvRecyclerView with Apache License 2.0 votes vote down vote up
protected abstract RecyclerView.LayoutManager getLayoutManager();