Showing posts with label Recyclerview. Show all posts
Showing posts with label Recyclerview. Show all posts

Wednesday, September 5, 2018

How to add Recyclerview item Animations like from left,right and Rotate

when we Displaying List type of Data Recycler view Plays a Major Role, I have already explained how to create Recyclerview if you wan u can check this article  android-recyclerview

when we loading data in Recyclerview we can add animations also, for example, I have added the following




full source code is available for Download Github


Share:

Android RecyclerView How to change the color of Alternate rows




 In Android Application some cases we have to show List with alternate colors
To achieve this we have used following small snippet, and full Sourcecode is available in Github
Recyclerview_alternate_rows



 //add  following code in onBindViewHolder
       if(position %2 == 1)
        {
            holder.lyt_content_view.setBackgroundColor(Color.WHITE);
        }
        else
        {
            holder.lyt_content_view.setBackgroundColor(Color.GRAY);
        }

add Following snippet in Adapter on Bind for reference i have added Adapter class Following



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);
    }

    @SuppressLint("ResourceAsColor")
    @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 (Previusposition == 0) {
            AnimationUtil.animate(holder, true);
        } else if (position > Previusposition) {
            AnimationUtil.animate(holder, true);
        } else {
            AnimationUtil.animate(holder, false);
        }
        Previusposition = position;
        if(position %2 == 1)
        {
            holder.lyt_content_view.setBackgroundColor(Color.WHITE);
        }
        else
        {
            holder.lyt_content_view.setBackgroundColor(Color.GRAY);
        }


    }

    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;
        LinearLayout lyt_content_view;

        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);
            lyt_content_view= itemView.findViewById(R.id.lyt_content_view);

        }
    }
}
Full source code is available ins Github  Source_code
Share:

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:

BTemplates.com

Search This Blog

Powered by Blogger.

Blog Archive

Labels