Adapter


An_adapter_manages the data model and adapts it to the individual entries in the widget. It extends theRecyclerView.Adapterclass and is assigned to the recycler view via theRecyclerView.setAdaptermethod. The input to the adapter of an recycler view can be any arbitrary Java objects. Based on this input the adapter must return the total number of items via itsgetItemCount()method.

You’ll see that I’ve created aViewHolderclass, which extends from the original.This is because the Adapter needs an implementation of the original abstract class.

The Adapter’s structure would be the following, self-generating the necessary methods:

public class HospitalAdapter extends RecyclerView.Adapter<HospitalAdapter.ViewHolder> {



    private List<Hospital> mHospital;
    private String customFont = "fonts/aller/Aller_Rg.ttf";
    private Typeface typeface;
    SessionManager sessionManager;
    private int lastPosition = -1;

    Context context;

    public HospitalAdapter(Context context, List<Hospital> objects) {
        this.mHospital = objects;
        this.context = context;
        sessionManager = new SessionManager(context);
    }

    public void clear() {
        mHospital.clear();
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        // each data item is just a string in this case

        @BindView(R.id.item_icon)
        ImageView itemIcon;
        @BindView(R.id.icon_layout)
        RelativeLayout iconLayout;
        @BindView(R.id.item_name)
        TextView name;

        public ViewHolder(View v) {
            super(v);
            ButterKnife.bind(this, v);

        }
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        typeface = Typeface.createFromAsset(context.getAssets(), customFont);

        Hospital hospital = mHospital.get(position);

        holder.name.setText(hospital.getTitle());
        holder.itemIcon.setImageResource(hospital.getIcon());
        //setupfont
        holder.name.setTypeface(typeface);
        if(position==0||position==1||position==2||position==5) {
            if (!sessionManager.isLoggedIn()) {
                holder.iconLayout.setBackgroundResource(R.drawable.circle4);
            }
        }
      //  setAnimation(holder.itemView, position);

    }

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

        //   context =parent.getContext();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.list_hospital2, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }
    /**
     * Here is the key method to apply the animation
     */
    private void setAnimation(View viewToAnimate, int position)
    {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition)
        {
            Animation animation = AnimationUtils.loadAnimation(context,R.anim.slideup);
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }
    @Override
    public int getItemCount() {
        return mHospital.size();
    }

The Constructor

The adapter needs to receive the items and a listener by parameter. The result is something like this :

public HospitalAdapter(Context context, List<Hospital> objects) {
this.mHospital = objects;
this.context = context;
sessionManager = new SessionManager(context);
}

The ViewHolder

The adapter prepares the layout of the items by inflating the correct layout for the individual data elements. This work is done in theonCreateViewHoldermethod. It returns an object of typeViewHolderper visual entry in the recycler view.

This instance is used to access the views in the inflated layout. TheonCreateViewHoldermethod is only called then a new view must be created.

Every visible entry in a recycler view is filled with the correct data model item by the adapter. Once a data item becomes visible, the adapter assigns this data to the individual widgets which he inflated earlier. This work in done in theonBindViewHoldermethod.

TheViewHolderwill assign the values from the model to their corresponding views:

public static class ViewHolder extends RecyclerView.ViewHolder {

// each data item is just a string in this case

@BindView(R.id.item_icon)
ImageView itemIcon;
@BindView(R.id.icon_layout)
RelativeLayout iconLayout;
@BindView(R.id.item_name)
TextView name;

public ViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);

}
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
typeface = Typeface.createFromAsset(context.getAssets(), customFont);

Hospital hospital = mHospital.get(position);

holder.name.setText(hospital.getTitle());
holder.itemIcon.setImageResource(hospital.getIcon());
//setupfont
holder.name.setTypeface(typeface);
if(position==0||position==1||position==2||position==5) {
if (!sessionManager.isLoggedIn()) {
holder.iconLayout.setBackgroundResource(R.drawable.circle4);
}
}
// setAnimation(holder.itemView, position);

}

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

// context =parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_hospital2, parent, false);
ViewHolder viewHolder = new ViewHolder(view);

return viewHolder;
}

results matching ""

    No results matching ""