Friday, 3 June 2016

RecyclerView with Animations

content_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"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:context="com.sgp.anil.recyclerview.MainActivity"
   
tools:showIn="@layout/activity_main">

    <
android.support.v7.widget.RecyclerView
       
xmlns:android="http://schemas.android.com/apk/res/android"
       
android:id="@+id/recyclerview"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"/>
</
LinearLayout>

 row_layout.xml
----------------------------
<android.support.v7.widget.CardView
   
android:id="@+id/cardView"
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
android:layout_marginBottom="@dimen/activity_vertical_margin"
   
app:cardCornerRadius="@dimen/activity_vertical_margin"
   
app:cardElevation="@dimen/activity_vertical_margin"
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto">

    <
RelativeLayout
       
android:layout_width="match_parent"
       
android:layout_height="^"
       
android:padding="16dp">

        <
ImageView
           
android:id="@+id/imageView"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:layout_alignParentLeft="true"
           
android:layout_alignParentTop="true"
           
android:layout_marginRight="16dp" />

        <
TextView
           
android:id="@+id/title"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:layout_alignParentTop="true"
           
android:layout_toRightOf="@+id/imageView"
           
android:text="Title"
           
android:textSize="20sp" />

        <
TextView
           
android:id="@+id/description"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:layout_below="@+id/title"
           
android:layout_toRightOf="@+id/imageView"
           
android:text="Description" />

    </
RelativeLayout>

</
android.support.v7.widget.CardView>
Data.Java
-----------------
package com.sgp.anil.recyclerview;

/**
 * Created by Anil on 03-06-2016.
 */
public class Data {
   
public String title;
   
public String description;
   
public int imageId;

    Data(String title, String description,
int imageId) {
       
this.title = title;
       
this.description = description;
       
this.imageId = imageId;
    }
}
 Recycler_View_Adapter.Java
-----------------------------------
package com.sgp.anil.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import java.util.Collections;
import java.util.List;

/**
 * Created by Anil on 03-06-2016.
 */
public class Recycler_View_Adapter extends RecyclerView.Adapter<View_Holder> {

    List<Data>
list = Collections.emptyList();
    Context
context;

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

   
@Override
   
public View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        
//Inflate the layout, initialize the View Holder
       
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
        View_Holder holder =
new View_Holder(v);
       
return holder;

    }

   
@Override
   
public void onBindViewHolder(View_Holder holder, int position) {

       
//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
       
holder.title.setText(list.get(position).title);
        holder.
description.setText(list.get(position).description);
        holder.
imageView.setImageResource(list.get(position).imageId);

        animate(holder);

    }

   
@Override
   
public int getItemCount() {
       
//returns the number of elements the RecyclerView will display
       
return list.size();
    }

   
@Override
   
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
       
super.onAttachedToRecyclerView(recyclerView);
    }

   
// Insert a new item to the RecyclerView on a predefined position
   
public void insert(int position, Data data) {
       
list.add(position, data);
        notifyItemInserted(position);
    }

   
// Remove a RecyclerView item containing a specified Data object
   
public void remove(Data data) {
       
int position = list.indexOf(data);
       
list.remove(position);
        notifyItemRemoved(position);
    }
   
public void animate(RecyclerView.ViewHolder viewHolder) {
       
final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(context, R.anim.anticipateovershoot_interpolator);
        viewHolder.
itemView.setAnimation(animAnticipateOvershoot);
    }
}
View_Holder.Java
--------------------------
package com.sgp.anil.recyclerview;

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Anil on 03-06-2016.
 */
public class View_Holder  extends RecyclerView.ViewHolder {

    CardView
cv;
    TextView
title;
    TextView
description;
    ImageView
imageView;

    View_Holder(View itemView) {
       
super(itemView);
       
cv = (CardView) itemView.findViewById(R.id.cardView);
       
title = (TextView) itemView.findViewById(R.id.title);
       
description = (TextView) itemView.findViewById(R.id.description);
       
imageView = (ImageView) itemView.findViewById(R.id.imageView);
    }
}
  
anticipateovershoot_interpolator.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   
android:interpolator="@android:anim/anticipate_overshoot_interpolator">
    <
translate
       
android:fromYDelta="-50%p"
       
android:toYDelta="0"
       
android:duration="1000"
       
/>
</
set>

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

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/bounce_interpolator">

    <translate

        android:duration="1500"

        android:fromYDelta="-150%p"

        android:toYDelta="0"

        />

</set>
Output:
------------------


No comments:

Post a Comment