Search in sources :

Example 1 with BaseViewHolder

use of com.chad.library.adapter.base.viewholder.BaseViewHolder in project BaseRecyclerViewAdapterHelper by CymChad.

the class GridSectionAverageGapItemDecoration method getItemOffsets.

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (parent.getLayoutManager() instanceof GridLayoutManager && parent.getAdapter() instanceof BaseSectionQuickAdapter) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        BaseSectionQuickAdapter<SectionEntity, BaseViewHolder> adapter = (BaseSectionQuickAdapter) parent.getAdapter();
        if (mAdapter != adapter) {
            setUpWithAdapter(adapter);
        }
        int spanCount = layoutManager.getSpanCount();
        int position = parent.getChildAdapterPosition(view) - mAdapter.getHeaderLayoutCount();
        SectionEntity entity = adapter.getItem(position);
        if (entity == null || entity.isHeader()) {
            // 不处理header
            outRect.set(0, 0, 0, 0);
            // Log.w("GridAverageGapItem", "pos=" + position + "," + outRect.toShortString());
            return;
        }
        Section section = findSectionLastItemPos(position);
        if (gapHSizePx < 0 || gapVSizePx < 0) {
            transformGapDefinition(parent, spanCount);
        }
        outRect.top = gapVSizePx;
        outRect.bottom = 0;
        // 下面的visualPos为单个Section内的视觉Pos
        int visualPos = position + 1 - section.startPos;
        if (visualPos % spanCount == 1) {
            // 第一列
            outRect.left = sectionEdgeHPaddingPx;
            outRect.right = eachItemHPaddingPx - sectionEdgeHPaddingPx;
        } else if (visualPos % spanCount == 0) {
            // 最后一列
            outRect.left = eachItemHPaddingPx - sectionEdgeHPaddingPx;
            outRect.right = sectionEdgeHPaddingPx;
        } else {
            outRect.left = gapHSizePx - (eachItemHPaddingPx - sectionEdgeHPaddingPx);
            outRect.right = eachItemHPaddingPx - outRect.left;
        }
        if (visualPos - spanCount <= 0) {
            // 第一行
            outRect.top = sectionEdgeVPaddingPx;
        }
        if (isLastRow(visualPos, spanCount, section.getCount())) {
            // 最后一行
            outRect.bottom = sectionEdgeVPaddingPx;
        // Log.w("GridAverageGapItem", "last row pos=" + position);
        }
    // Log.w("GridAverageGapItem", "pos=" + position + ",vPos=" + visualPos + "," + outRect.toShortString());
    } else {
        super.getItemOffsets(outRect, view, parent, state);
    }
}
Also used : BaseSectionQuickAdapter(com.chad.library.adapter.base.BaseSectionQuickAdapter) BaseViewHolder(com.chad.library.adapter.base.viewholder.BaseViewHolder) GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) SectionEntity(com.chad.library.adapter.base.entity.SectionEntity)

Example 2 with BaseViewHolder

use of com.chad.library.adapter.base.viewholder.BaseViewHolder in project BaseRecyclerViewAdapterHelper by CymChad.

the class GridSectionAverageGapItemDecoration method markSections.

private void markSections() {
    if (mAdapter != null) {
        BaseSectionQuickAdapter<SectionEntity, BaseViewHolder> adapter = mAdapter;
        mSectionList.clear();
        SectionEntity sectionEntity = null;
        Section section = new Section();
        for (int i = 0, size = adapter.getItemCount(); i < size; i++) {
            sectionEntity = adapter.getItem(i);
            if (sectionEntity != null && sectionEntity.isHeader()) {
                // 找到新Section起点
                if (section != null && i != 0) {
                    // 已经有待添加的section
                    section.endPos = i - 1;
                    mSectionList.add(section);
                }
                section = new Section();
                section.startPos = i + 1;
            } else {
                section.endPos = i;
            }
        }
        // 处理末尾情况
        if (!mSectionList.contains(section)) {
            mSectionList.add(section);
        }
    // Log.w("GridAverageGapItem", "section list=" + mSectionList);
    }
}
Also used : BaseViewHolder(com.chad.library.adapter.base.viewholder.BaseViewHolder) SectionEntity(com.chad.library.adapter.base.entity.SectionEntity)

Example 3 with BaseViewHolder

use of com.chad.library.adapter.base.viewholder.BaseViewHolder in project BaseRecyclerViewAdapterHelper by CymChad.

the class DragAndSwipeUseActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_universal_recycler);
    setBackBtn();
    setTitle("Drag And Swipe");
    mRecyclerView = findViewById(R.id.rv);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    // 拖拽监听
    OnItemDragListener listener = new OnItemDragListener() {

        @Override
        public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "drag start");
            final BaseViewHolder holder = ((BaseViewHolder) viewHolder);
            // 开始时,item背景色变化,demo这里使用了一个动画渐变,使得自然
            int startColor = Color.WHITE;
            int endColor = Color.rgb(245, 245, 245);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                ValueAnimator v = ValueAnimator.ofArgb(startColor, endColor);
                v.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        holder.itemView.setBackgroundColor((int) animation.getAnimatedValue());
                    }
                });
                v.setDuration(300);
                v.start();
            }
        }

        @Override
        public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
            Log.d(TAG, "move from: " + source.getAdapterPosition() + " to: " + target.getAdapterPosition());
        }

        @Override
        public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "drag end");
            final BaseViewHolder holder = ((BaseViewHolder) viewHolder);
            // 结束时,item背景色变化,demo这里使用了一个动画渐变,使得自然
            int startColor = Color.rgb(245, 245, 245);
            int endColor = Color.WHITE;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                ValueAnimator v = ValueAnimator.ofArgb(startColor, endColor);
                v.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        holder.itemView.setBackgroundColor((int) animation.getAnimatedValue());
                    }
                });
                v.setDuration(300);
                v.start();
            }
        }
    };
    // 侧滑监听
    OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {

        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "view swiped start: " + pos);
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "View reset: " + pos);
            BaseViewHolder holder = ((BaseViewHolder) viewHolder);
        }

        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d(TAG, "View Swiped: " + pos);
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
            canvas.drawColor(ContextCompat.getColor(DragAndSwipeUseActivity.this, R.color.color_light_blue));
        }
    };
    List<String> mData = generateData(50);
    mAdapter = new DragAndSwipeAdapter(mData);
    mAdapter.getDraggableModule().setSwipeEnabled(true);
    mAdapter.getDraggableModule().setDragEnabled(true);
    mAdapter.getDraggableModule().setOnItemDragListener(listener);
    mAdapter.getDraggableModule().setOnItemSwipeListener(onItemSwipeListener);
    mAdapter.getDraggableModule().getItemTouchHelperCallback().setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
    // mAdapter.getDraggableModule().getItemTouchHelperCallback().setDragMoveFlags(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
            Tips.show("点击了:" + position);
        }
    });
}
Also used : BaseViewHolder(com.chad.library.adapter.base.viewholder.BaseViewHolder) OnItemClickListener(com.chad.library.adapter.base.listener.OnItemClickListener) Canvas(android.graphics.Canvas) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ValueAnimator(android.animation.ValueAnimator) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) DragAndSwipeAdapter(com.chad.baserecyclerviewadapterhelper.adapter.DragAndSwipeAdapter) BaseViewHolder(com.chad.library.adapter.base.viewholder.BaseViewHolder) OnItemDragListener(com.chad.library.adapter.base.listener.OnItemDragListener) BaseQuickAdapter(com.chad.library.adapter.base.BaseQuickAdapter) OnItemSwipeListener(com.chad.library.adapter.base.listener.OnItemSwipeListener)

Aggregations

BaseViewHolder (com.chad.library.adapter.base.viewholder.BaseViewHolder)3 SectionEntity (com.chad.library.adapter.base.entity.SectionEntity)2 ValueAnimator (android.animation.ValueAnimator)1 Canvas (android.graphics.Canvas)1 View (android.view.View)1 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 DragAndSwipeAdapter (com.chad.baserecyclerviewadapterhelper.adapter.DragAndSwipeAdapter)1 BaseQuickAdapter (com.chad.library.adapter.base.BaseQuickAdapter)1 BaseSectionQuickAdapter (com.chad.library.adapter.base.BaseSectionQuickAdapter)1 OnItemClickListener (com.chad.library.adapter.base.listener.OnItemClickListener)1 OnItemDragListener (com.chad.library.adapter.base.listener.OnItemDragListener)1 OnItemSwipeListener (com.chad.library.adapter.base.listener.OnItemSwipeListener)1