Wednesday, September 5, 2018

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:

0 comments:

Post a Comment

BTemplates.com

Search This Blog

Powered by Blogger.

Blog Archive