RecyclerView sets the approach animation, sets the spacing between children, and solves the problem of constantly refreshing the spacing of children

RecyclerView sets the approach animation, sets the spacing between children, and solves the problem of constantly refreshing the spacing of children

Effect picture:

Insert picture description here

1. Set the approach animation

By mRecyclerView.setLayoutAnimation(LayoutAnimationController controller)setting:

Step I: Write animation effects (located under the anim file), create a new anim_item_slide_right.xmlfile

<?xml version="1.0" encoding="utf-8"?>

<!--
    动画效果:从右边滑入,从完全不可见到完全可见
-->

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fillAfter="true"
    >

    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        />

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        />
</set>

Step Ⅱ: write layoutanimationfile (file located in anim), the new layout_anim_item_right_slipe.xmlfile

<?xml version="1.0" encoding="utf-8"?>

<!--
    效果:子项从右边滑入,延时10%
-->

<layoutAnimation 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/anim_item_slide_right"
    android:delay="10%"
    android:animationOrder="normal"
    />

Step Ⅲ: Set up animation in the code

// RecyclerView和适配器
private RecyclerView mRecyclerView;
private RcvSearchAdapter mAdapter;

// 创建布局动画控制类 LayoutAnimationController ,待会给RecyclerView设置动画
LayoutAnimationController lac_right_slide = AnimationUtils.loadLayoutAnimation(getContext(), R.anim.layout_anim_item_right_slipe);

// 设置动画
mRecyclerView.setLayoutAnimation(lac_right_slide);

mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// 需要重新启动布局时调用此方法
mRecyclerView.scheduleLayoutAnimation();

Two, set the spacing between sub-items

Methods: The definition of a child decorative ItemSpaceDecorationinheritance RecyclerView.ItemDecoration, here I defined in the adapter:

public class RcvSearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
    
    ......
        
    /**
     * 用来调节Item之间的间距的装饰类
     */
    public static class ItemSpaceDecoration extends RecyclerView.ItemDecoration{
    
    
        // item 之间的间距
        private int itemSpace;

        public ItemSpaceDecoration(int itemSpace) {
    
    
            this.itemSpace = itemSpace;
        }

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    
    
            if(parent.getChildPosition(view) != 0){
    
    
                outRect.top = itemSpace;
            }
        }
    }
    
    ......
}

Then RecyclerViewcall the setItemDecoration(@NonNull ItemDecoration decor)method to set child spacing:

/**
 * RecyclerView子项之间的间距
 */
private static final int ITEM_SPACE = 30;

/**
 * 子项装饰类
 */
 private RcvSearchAdapter.ItemSpaceDecoration mDecoration; 

 // 这里间距是 ITEM_SPACE
 mDecoration = new RcvSearchAdapter.ItemSpaceDecoration(ITEM_SPACE);
 // 设置子项之间的间距
 mRecyclerView.addItemDecoration(mDecoration);

3. Solve the problem of constantly refreshing the gap between sub-items

① reason: When we give RecyclerViewset decoration (ItemDecoration), the called addItemDecoration()method, but this method did not clear the inside decoration, resulting in each refresh will be set once, child spacing growing problem occurs

Source code:

Insert picture description here

② Solution: Clear the settings before refreshing ItemDecoration:

// 这里需要清除RecyclerView的 ItemDecoration,不然不断刷新的时候会导致子项之间的间距越来越大
mRecyclerView.removeItemDecoration(mDecoration);

Guess you like

Origin blog.csdn.net/C_biubiubiu/article/details/109852543