No trivial interface (a): RecyclerView+CardView to know

No trivial interface (two): RecyclerView to show more different views

Interface no trivial (three): RecyclerView + Toolbar to make a file selector

Interface no small matter (4): write a scroll selector!

Interface no small matter (five): custom TextView

Interface no small matter (six): to make a good-looking side pull menu!

Interface no trivial matter (7): use code to dynamically add and delete layout


directory

  • preface
  • Add or delete the item
  • Custom add and delete animation
  • The last

preface

Before wrote a code dynamic add and delete layout, compared to the RecyclerView add and delete item, to tell the truth, the code dynamic add and delete layout can basically quit the group.


Add or delete the item

Of course, first of all you can according to the first article – no trivial interface (a): RecyclerView+CardView to understand the foundation of RecyclerView. The key this time is to add and remove items to the adapter code. Note that the refresh is not the same as the ListView operation. You can look directly at the official documents, which are roughly as follows:

  • notifyItemInserted()
  • notifyItemRemoved()
  • notifyItemMoved()
  • notifyItemChanged()

Add the following code to the adapter:

public void addData(int position) {
    mData.add(position, "hello python");
    notifyItemInserted(position);
}

public void removeData(int position) {
    mData.remove(position);
    notifyItemRemoved(position);
}
Copy the code

We then add add and del buttons to the Toolbar to correspond to both methods.

<? xml version="1.0" encoding="utf-8"? > <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/add"
        android:icon="@mipmap/ic_launcher"
        android:title="@string/add"
        app:showAsAction="never" />

    <item
        android:id="@+id/del"
        android:icon="@mipmap/ic_launcher"
        android:title="@string/del"
        app:showAsAction="never" />
</menu>
Copy the code

And set long press to delete, click to add. All right, here’s the rendering:

Not only the layout I demonstrated, but also linear, horizontal, and waterfall streams can have similar effects. Can refer to the interface no matter (two): RecyclerView to show more different views.

Now that we have default animations, we can definitely customize them. Before we do that, let’s talk about a few ways. Used to set the duration of the specific state of the animation.

rvTest.getItemAnimator().setAddDuration(400);
rvTest.getItemAnimator().setRemoveDuration(400);
rvTest.getItemAnimator().setMoveDuration(400);
rvTest.getItemAnimator().setChangeDuration(400);
Copy the code

Custom add and delete animation

Open source projects are used here. Because this project is really good enough. The number of built-in animations is enough, see below. And you can go on and customize it. Doesn’t feel like everything is alive.

Animators

classification Animation class name
Cool LandingAnimator
Scale ScaleInAnimator, ScaleInTopAnimator, ScaleInBottomAnimator, ScaleInLeftAnimator, ScaleInRightAnimator
Fade FadeInAnimator, FadeInDownAnimator, FadeInUpAnimator, FadeInLeftAnimator, FadeInRightAnimator
Flip FlipInTopXAnimator, FlipInBottomXAnimator, FlipInLeftYAnimator, FlipInRightYAnimator
Slide SlideInLeftAnimator, SlideInRightAnimator, OvershootInLeftAnimator, OvershootInRightAnimator, SlideInUpAnimator, SlideInDownAnimator

And very rare is the use of the method is very simple, requires a few steps

  • Write to build.gradle in Module
dependencies {
  implementation 'jp. Wasabeef: recyclerview - animators: 2.3.0'
}
Copy the code
  • Write to build.gradle in Project
repositories {
    google()
    jcenter()
}
Copy the code

Use the animation class name instead of the default animation class name. Such as:

rvTest.setAdapter(mAdapter);
rvTest.setItemAnimator(new SlideInLeftAnimator());
Copy the code

Ok, let’s take a look at some images:


The last

Please remember to like oh, have comments or suggestions in the comments section, secretly follow me is also ok.