61.android 简单的RecyclerView列表动画

//效果图就是这样的图,我是随便找的一张图,没办法我嫌录制GIF图,太麻烦了,反正就是这种效果。

//第一步 导入依赖

 

implementation 'jp.wasabeef:recyclerview-animators:2.2.7'

 

//第二步 适配器

 //适配器 就是普普通通的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<String> mList;
    private Context context;

    public MyAdapter(List<String> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_item, null);
        ViewHolder holder = new ViewHolder(inflate);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mText.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public TextView mText;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.mText = (TextView) rootView.findViewById(R.id.mText);
        }

    }
}

 

//第三步 Activity里使用

 

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mRecy = (RecyclerView) findViewById(R.id.mRecy);

        List<String> mList=new ArrayList<>();
        for (int i = 0; i < 300; i++) {
            mList.add("Hello"+i);
        }

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecy.setLayoutManager(linearLayoutManager);
        MyAdapter adapter = new MyAdapter(mList,this);
        //渐变色动画
        AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
        //放大动画,里面联合渐变色动画
        ScaleInAnimationAdapter animationAdapter = new ScaleInAnimationAdapter(alphaAdapter);
        //设置渐变色动画是否只走一次
        alphaAdapter.setFirstOnly(false);
        //设置放大动画是否只走一次
        animationAdapter.setFirstOnly(false);
        //设置渐变色动画的时间
        alphaAdapter.setDuration(1000);
        //设置放大动画的时间
        animationAdapter.setDuration(1000);
        //适配放大动画(放大动画里有渐变色动画)
        mRecy.setAdapter(animationAdapter);
    }
}

//Activity和适配器的布局也顺带粘贴上:

//Activity:activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hasee.a91zidingyiview.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecy"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

 

//适配器布局:layout_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_marginTop="10dp"
        android:background="#cc9c9c"
        android:id="@+id/mText"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

 //---------------------------------------------------------------到这里就完事了-----------------------------------------------------------------------

//以下是一些动画的属性介绍:

Adapter的使用

1.使用

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
MyAdapter adapter = new MyAdapter();
recyclerView.setAdapter(new AlphaInAnimationAdapter(adapter));

2.高级功能

动画时长

MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setDuration(1000);
recyclerView.setAdapter(alphaAdapter);

插值器

MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setInterpolator(new OvershootInterpolator());
recyclerView.setAdapter(alphaAdapter);

是否仅显示一次动画效果

MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
scaleAdapter.setFirstOnly(false);
recyclerView.setAdapter(alphaAdapter);

复合动画

MyAdapter adapter = new MyAdapter();
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
recyclerView.setAdapter(new ScaleInAnimationAdapter(alphaAdapter));

 

//----------------------------------------------------------------------------完--------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值