Created
May 11, 2016 18:41
-
-
Save dGorod/bc38b4c866ecb0c79befa75b46ec2b9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.dgorod.tourbag.presentation.adapter.base; | |
| import android.content.Context; | |
| import android.support.annotation.CallSuper; | |
| import android.support.annotation.LayoutRes; | |
| import android.support.annotation.NonNull; | |
| import android.support.annotation.Nullable; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.BaseAdapter; | |
| import com.dgorod.tourbag.commons.Preconditions; | |
| import com.dgorod.tourbag.presentation.adapter.holder.base.BaseHolder; | |
| import java.util.List; | |
| /** | |
| * Wrapper above {@link BaseAdapter} to hide ConvertView approach boilerplate code. | |
| * Adapter's generics must be specified. | |
| * DataType - class that will be used in adapter data. | |
| * ViewHolder - class to be instantiated as view holder. | |
| * | |
| * Created by Dmytro Gorodnytskyi on 01-Apr-16. | |
| */ | |
| public abstract class BaseListAdapter<DataType, ViewHolder extends BaseHolder> extends BaseAdapter { | |
| private final int itemLayout; | |
| private LayoutInflater inflater; | |
| protected List<DataType> data; | |
| public BaseListAdapter(@LayoutRes int itemLayout, @NonNull Context context) { | |
| Preconditions.checkNotNull(context); | |
| this.itemLayout = itemLayout; | |
| this.inflater = LayoutInflater.from(context); | |
| } | |
| @NonNull | |
| public abstract ViewHolder onCreateViewHolder(@NonNull View itemView); | |
| public abstract void onBindViewHolder(@NonNull ViewHolder holder, int position); | |
| @Nullable | |
| public List<DataType> getData() { | |
| return data; | |
| } | |
| public void setData(@Nullable List<DataType> data) { | |
| this.data = data; | |
| notifyDataSetChanged(); | |
| } | |
| @CallSuper | |
| @Override | |
| public int getCount() { | |
| return data == null ? 0 : data.size(); | |
| } | |
| @CallSuper | |
| @Override | |
| public DataType getItem(int position) { | |
| return data.get(position); | |
| } | |
| @SuppressWarnings("unchecked cast") | |
| @Override | |
| public final View getView(int position, View convertView, ViewGroup parent) { | |
| View resultView = convertView; | |
| ViewHolder holder; | |
| if (convertView == null) { | |
| resultView = inflater.inflate(itemLayout, parent, false); | |
| holder = this.onCreateViewHolder(resultView); | |
| resultView.setTag(holder); | |
| } | |
| else { | |
| holder = (ViewHolder) resultView.getTag(); | |
| } | |
| this.onBindViewHolder(holder, position); | |
| return resultView; | |
| } | |
| public int getItemPosition(long itemId) | |
| { | |
| for (int i = 0; i < getCount(); i++) { | |
| if (getItemId(i) == itemId) { | |
| return i; | |
| } | |
| } | |
| return -1; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment