Tuesday, September 4, 2018

Android Recyclerview with Picasso And item loading animation


Any Application Listview Always Plays a Major Role, in every Application,
we have lots of data example Users List, Company names, students List etc.  ListViews are very useful for Display this type of data
In Android Applications Recyclerview Very useful For Display list type of data.
In this example, I have created a recycle view with Sample data
Step: 1
Create new Project and Add following dependencies

    // RCV && CardView
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'

Step : 2

Create local data for Recyclerview content

public class PropertyData {
    public static List<PropertyModel> getPropertyData() {

        List<PropertyModel> pro_list = new ArrayList<>();
        pro_list.add(new PropertyModel(1, "pro_1", "pro_1_ads", "https://homepages.cae.wisc.edu/~ece533/images/airplane.png"));
        pro_list.add(new PropertyModel(2, "pro_2", "pro_2_ads", "https://homepages.cae.wisc.edu/~ece533/images/arctichare.png"));
        pro_list.add(new PropertyModel(3, "pro_3", "pro_3_ads", "https://homepages.cae.wisc.edu/~ece533/images/baboon.png"));
        pro_list.add(new PropertyModel(4, "pro_4", "pro_4_ads", "https://homepages.cae.wisc.edu/~ece533/images/barbara.png"));
        pro_list.add(new PropertyModel(5, "pro_5", "pro_5_ads", "https://homepages.cae.wisc.edu/~ece533/images/boat.png"));

        pro_list.add(new PropertyModel(6, "pro_6", "pro_6_ads", "https://homepages.cae.wisc.edu/~ece533/images/airplane.png"));
        pro_list.add(new PropertyModel(7, "pro_7", "pro_7_ads", "https://homepages.cae.wisc.edu/~ece533/images/arctichare.png"));
        pro_list.add(new PropertyModel(8, "pro_8", "pro_8_ads", "https://homepages.cae.wisc.edu/~ece533/images/baboon.png"));
        pro_list.add(new PropertyModel(9, "pro_9", "pro_9_ads", "https://homepages.cae.wisc.edu/~ece533/images/barbara.png"));
        pro_list.add(new PropertyModel(10, "pro_10", "pro_10_ads", "https://homepages.cae.wisc.edu/~ece533/images/airplane.png"));

        pro_list.add(new PropertyModel(11, "pro_11", "pro_11_ads", "https://homepages.cae.wisc.edu/~ece533/images/arctichare.png"));
        pro_list.add(new PropertyModel(12, "pro_12", "pro_12_ads", "https://homepages.cae.wisc.edu/~ece533/images/baboon.png"));
        pro_list.add(new PropertyModel(13, "pro_13", "pro_13_ads", "https://homepages.cae.wisc.edu/~ece533/images/barbara.png"));
        pro_list.add(new PropertyModel(14, "pro_14", "pro_14_ads", "https://homepages.cae.wisc.edu/~ece533/images/boat.png"));
        pro_list.add(new PropertyModel(15, "pro_15", "pro_15_ads", "https://homepages.cae.wisc.edu/~ece533/images/cat.png"));

        return pro_list;

    }

}

Step : 3

Create recycle-view Item 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginBottom="5dp"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <android.support.v7.widget.CardView
        android:layout_marginBottom="@dimen/rcv_margin"
        android:background="@color/colorWhite"
        android:layout_width="match_parent"
        android:layout_height="@dimen/rcv_row_height"
        app:cardCornerRadius="@dimen/rcv_row_corner">

        <LinearLayout
            android:weightSum="3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="@dimen/rcv_row_lyt_margin"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img_pro"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_weight="1"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_gravity="center_vertical"
                android:textAlignment="center"
                android:textSize="24dp"
                android:id="@+id/pro_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Sample"
                android:textColor="@color/colorBlack" />

            <TextView
                android:id="@+id/pro_ads"
                android:layout_gravity="center_vertical"
                android:textAlignment="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Sample"
                android:textColor="@color/colorPrimary" />

        </LinearLayout>

    </android.support.v7.widget.CardView>
</RelativeLayout>
Step : 4

Create Activity for Recyclerview


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".view.MainActivity"
    android:background="@color/colorWhiteSmoke">

   <android.support.v7.widget.RecyclerView

       android:layout_margin="@dimen/rcv_row_lyt_margin"
       android:id="@+id/rcv_pro"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
   </android.support.v7.widget.RecyclerView>

</RelativeLayout>


Main activity.java


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private RecyclerView rcv_pro;
    private LinearLayoutManager layoutManager;
    private List<PropertyModel> propertyDataList;
    private PropertyListAdapter propertyListAdapter;


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



    }

    private void bind() {

        propertyDataList = PropertyData.getPropertyData();
        propertyListAdapter = new PropertyListAdapter(propertyDataList,mContext,rcv_pro);
        rcv_pro.setAdapter(propertyListAdapter);

    }


    private void init() {
        mContext =MainActivity.this;
        layoutManager = new LinearLayoutManager(mContext);
        rcv_pro = findViewById(R.id.rcv_pro);
        rcv_pro.setLayoutManager(layoutManager);


    }

}

Step 4: create Adapter for Recyclerview


public class PropertyListAdapter extends RecyclerView.Adapter<PropertyListAdapter.RcvViewHolder> {

    private List<PropertyModel> propertyModelList = new ArrayList<>();
    private Context mContext;
    private RecyclerView rcvPro;
 int Previusposition=0;

    public PropertyListAdapter(List<PropertyModel> propertyModelList,Context mContext,RecyclerView rcvPro) {

        this.propertyModelList = propertyModelList;
        this.mContext= mContext;
        this.rcvPro = rcvPro;

    }

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

        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.rcv_pro_row,null);
        return new RcvViewHolder(v);
    }

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

        Picasso.with(mContext).load(propertyModelList.get(position).getPro_image()).transform(new CommonUtl.CircleTransform()).into(holder.img_pr);
        holder.txt_name.setText(propertyModelList.get(position).getPro_name());
        holder.txt_ads.setText(propertyModelList.get(position).getPro_adrs());

        if(position > Previusposition)
        {
            AnimationUtil.animate(holder,true);
        }else {
            AnimationUtil.animate(holder,false);
        }
        Previusposition = position;

    }
    public void setFadeAnimation(View view) {
        AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
        anim.setDuration(1000);
        view.startAnimation(anim);
    }

    @Override
    public int getItemCount() {
        if(propertyModelList.size() > 0)
        return propertyModelList.size();
        else
            return 0;
    }
    class RcvViewHolder extends RecyclerView.ViewHolder{
        ImageView img_pr;
        TextView txt_name,txt_ads;
        public RcvViewHolder(View itemView) {
            super(itemView);
            img_pr = itemView.findViewById(R.id.img_pro);
            txt_name = itemView.findViewById(R.id.pro_name);
            txt_ads = itemView.findViewById(R.id.pro_ads);

        }
    }
}


Full Source code Github_recyclerview_with_item_animation , sample_recyclerview
Download apk : apk Link 
Share:

0 comments:

Post a Comment

BTemplates.com

Search This Blog

Powered by Blogger.

Blog Archive