Saturday, 13 October 2018

Android RecyclerView With Animation



1) Add this line in gradle file

    implementation 'com.android.support:recyclerview-v7:28.0.0'

2) Create Animation Resource File In anim Folder

i) item_animation_fall_down.xml 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300">

    <translate
        android:fromYDelta="-20%"
        android:toYDelta="0"
        android:interpolator="@android:anim/decelerate_interpolator"/>

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:interpolator="@android:anim/decelerate_interpolator"/>

    <scale
        android:fromXScale="105%"
        android:fromYScale="105%"
        android:toXScale="100%"
        android:toYScale="100%"
        android:pivotX="50%"
        android:pivotY="50%"
        android:interpolator="@android:anim/decelerate_interpolator"/>

</set>

ii) layout_fall_down.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_animation_fall_down"
    android:delay="15%"
    android:animationOrder="normal">

</layoutAnimation>

iii) item_slide_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300">

    <translate
        android:fromXDelta="100%p"
        android:toYDelta="0"
        android:interpolator="@android:anim/decelerate_interpolator"/>

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:interpolator="@android:anim/decelerate_interpolator"/>

<!--Todo: Lets Run this App In Android Device-->
</set>

iv) layout_slide_right.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/item_slide_from_right"
    android:delay="15%"
    android:animationOrder="normal">

</layoutAnimation>


3) item.xml custom list of recyclerview

<?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="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="5dp"
        android:background="@drawable/card_rect"
        android:layout_gravity="center">
        ]
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txt_home"
            android:textAlignment="center"
            android:text="Title"
            android:textColor="#000000"
            android:textSize="18dp"
            android:textStyle="bold"
            android:layout_marginBottom="5dp"/>


    </LinearLayout>


</LinearLayout>

4) Item_Card.java

public class Item_Card {

    String name;

    public Item_Card(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}


5) ItemAdapter.java

class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{

    List<Item_Card> list;
    Context context;

    public ItemAdapter(List<Item_Card> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.items, viewGroup, false);
        return new ItemAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        Item_Card Cards = list.get(position);
        final String name = Cards.getName();

        viewHolder.home_title.setText(name);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView home_title;
        public ViewHolder(View itemView) {
            super(itemView);

            home_title = (TextView) itemView.findViewById(R.id.txt_home);

        }
    }
}


6) activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_fall_down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fall Down" />


        <Button
            android:id="@+id/btn_slide_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Slide Right" />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteY="55dp"
        tools:ignore="MissingConstraints" />


</LinearLayout> 

7) MainActivity.java

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ItemAdapter adapter;
    private List<Item_Card> Cardss;
    private RecyclerView.LayoutManager mLayoutManager;

    Button btn_fall_down, btn_slide_right;

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

        btn_fall_down = (Button) findViewById(R.id.btn_fall_down);
        btn_slide_right = (Button) findViewById(R.id.btn_slide_right);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

        recyclerView.setHasFixedSize(true);

        mLayoutManager = new GridLayoutManager(MainActivity.this,2);
        recyclerView.setLayoutManager(mLayoutManager);
        Cardss = new ArrayList<>();

        btn_fall_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Data(recyclerView, 0);
            }
        });



        btn_slide_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Data(recyclerView, 2);
            }
        });
    }

    private void Data(RecyclerView recyclerView, int type) {


        Context context = recyclerView.getContext();
        LayoutAnimationController controller = null; // for animation

        if (type == 0){
            controller = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_fall_down);
        } if(type == 2){
            controller = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_slide_right);
        }

        for (int i = 0; i <=20; i++) {
            Cardss.add(new Item_Card(
                    String.valueOf(i)
            ));
        }
        //adapter.notifyDataSetChanged();
        adapter = new ItemAdapter(Cardss, MainActivity.this);
        recyclerView.setAdapter(adapter);

        recyclerView.setLayoutAnimation(controller);
        recyclerView.getAdapter().notifyDataSetChanged();
        recyclerView.scheduleLayoutAnimation();
    }
}


2 comments:

garettejaeger said...

TOTO | Stainless Steel | TOTO, Inc.
TOTO mens black titanium wedding bands is titanium dioxide a company that builds new and innovative products from titanium blue ps4 controller titanium nipple piercing jewelry titanium steel for more than 40 years. We supply all of our new products 2020 ford edge titanium for sale

Anonymous said...

take a look at the site here dildo,vibrators,male masturbator,dildos,vibrators,horse dildo,sex chair,sex chair,dildo visit the website

Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...