Twitter Bird Gadget

Sunday 14 May 2017

RecyclerView adapter example

MyAdapter.java

public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.ViewHolder> {
    Context mContext;
    Activity mActivity;
    ArrayList<CustomerModel> list;

    public CustomerAdapter(Context context, ArrayList<CustomerModel> list) {
        this.list = list;
        this.mContext = context;
        this.mActivity = (Activity) context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.row_customerlist, parent,
                false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int i) {
        final CustomerModel item = list.get(i);
        holder.txt_customer_name.setText(item.getCust_name());
        holder.txt_customer_mobileno.setText(item.getCust_mobileno());

        Font.RobotoRegular.apply(mContext, holder.txt_customer_name);
        Font.RobotoLight.apply(mContext, holder.txt_customer_mobileno);


        try {
            Picasso.with(mContext)
                    .load(item.getCust_profilepic())
                    //.transform(new RoundedTransformation(100, -25)).fit()
                    .placeholder(R.drawable.ic_loading).fit()
                    //.transform(new RoundedTransformation(20, 0)).fit()
                    .error(R.drawable.ic_loading).into(holder.ivProfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
     
      if (mContext instanceof PlanActivity) {
            holder.btnServiceSell.setVisibility(View.VISIBLE);
        } else {
            holder.btnServiceSell.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(mContext, ListActivity.class);
                i.putExtra(Connection.Tag_customer_id, item.getCust_id());
                Log.e("ciddd-", item.getCust_id());
                mContext.startActivity(i);

            }
        });

    }

    @Override
    public int getItemCount() {
        return (null != list ? list.size() : 0);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView txt_customer_name, txt_customer_mobileno;
        CircularImageView ivProfile;

        public ViewHolder(View v) {
            super(v);
            txt_customer_name = (TextView) v.findViewById(R.id.tvusername);
            txt_customer_mobileno = (TextView) v.findViewById(R.id.tvusermobileno);
            ivProfile = (CircularImageView) v.findViewById(R.id.imguserpic);
        }

        @Override
        public void onClick(View v) {
            Log.e("------------", "------- ");
        }
    }
}

No comments :

Post a Comment