Android Open Source - android-blog Binding Adapter






From Project

Back to project page android-blog.

License

The source code is released under:

MIT License

If you think the Android project android-blog listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.kevinmrohr.android_blog.adapter;
//  ww  w.j  av a 2  s.c om
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * An implementation of {@link android.widget.BaseAdapter} which uses the new/bind pattern and
 * view holder pattern for its views.
 * <p/>
 * Inspired by/baesd on Jake Wharton's BindingAdapter:
 * https://gist.github.com/JakeWharton/5423616
 * <p/>
 * Pulled from Patrick Hammond's https://gist.github.com/patrickhammond/6094827
 */
public abstract class BindingAdapter<T, S> extends BaseAdapter {
  private final Context context;
  private final LayoutInflater inflater;

  public BindingAdapter(Context context) {
    this.context = context;
    this.inflater = LayoutInflater.from(context);
  }

  public Context getContext() {
    return context;
  }

  @Override public abstract T getItem(int position);

  @Override public final View getView(int position, View view, ViewGroup container) {
    if (view == null) {
      view = newView(inflater, position, container);
      if (view == null) {
        throw new IllegalStateException("newView result must not be null.");
      }

      associateViewHolder(view, position);
    }

    bindView(getItem(position), position, view, getViewHolder(view));
    return view;
  }

  /**
   * Create a new instance of a view for the specified position.
   */
  public abstract View newView(LayoutInflater inflater, int position, ViewGroup container);

  /**
   * If your ViewHolder implementation looks something like this:
   * <pre>
   * {@code
   * static class ViewHolder {
   *     final TextView textView;
   *
   *     ViewHolder(View view) {
   *         textView = (TextView) view.findViewById(R.id.textView);
   *     }
   * }
   * </pre>
   * <p/>
   * This method only needs this as its implementation:
   * <pre>
   * {@code
   * return new ViewHolder(view);
   * }
   * </pre>
   * <p/>
   * If implementations do not need/want a view holder, just return <code>null</code>.
   */
  public abstract S buildViewHolder(View view, int position);

  /**
   * Bind the data for the specified {@code position} to the view using a
   * {@code viewHolder} created from {@link #buildViewHolder(android.view.View, int)}.
   */
  public abstract void bindView(T item, int position, View view, S vh);

  @Override public final View getDropDownView(int position, View view, ViewGroup container) {
    if (view == null) {
      view = newDropDownView(inflater, position, container);
      if (view == null) {
        throw new IllegalStateException("newDropDownView result must not be null.");
      }

      associateViewHolder(view, position);
    }

    bindDropDownView(getItem(position), position, view, getViewHolder(view));
    return view;
  }

  private void associateViewHolder(View view, int position) {
    S viewHolder = buildViewHolder(view, position);
    view.setTag(viewHolder);
  }

  @SuppressWarnings("unchecked")
  private S getViewHolder(View view) {
    return (S) view.getTag();
  }

  /**
   * Create a new instance of a drop-down view for the specified position.
   */
  public View newDropDownView(LayoutInflater inflater, int position, ViewGroup container) {
    return newView(inflater, position, container);
  }

  /**
   * Bind the data for the specified {@code position} to the drop-down view.
   */
  public void bindDropDownView(T item, int position, View view, S viewHolder) {
    bindView(item, position, view, viewHolder);
  }
}




Java Source Code List

com.android.volley.AuthFailureError.java
com.android.volley.CacheDispatcher.java
com.android.volley.Cache.java
com.android.volley.DefaultRetryPolicy.java
com.android.volley.ExecutorDelivery.java
com.android.volley.NetworkDispatcher.java
com.android.volley.NetworkError.java
com.android.volley.NetworkResponse.java
com.android.volley.Network.java
com.android.volley.NoConnectionError.java
com.android.volley.ParseError.java
com.android.volley.RequestQueue.java
com.android.volley.Request.java
com.android.volley.ResponseDelivery.java
com.android.volley.Response.java
com.android.volley.RetryPolicy.java
com.android.volley.ServerError.java
com.android.volley.TimeoutError.java
com.android.volley.VolleyError.java
com.android.volley.VolleyLog.java
com.android.volley.toolbox.AndroidAuthenticator.java
com.android.volley.toolbox.Authenticator.java
com.android.volley.toolbox.BasicNetwork.java
com.android.volley.toolbox.ByteArrayPool.java
com.android.volley.toolbox.ClearCacheRequest.java
com.android.volley.toolbox.DiskBasedCache.java
com.android.volley.toolbox.HttpClientStack.java
com.android.volley.toolbox.HttpHeaderParser.java
com.android.volley.toolbox.HttpStack.java
com.android.volley.toolbox.HurlStack.java
com.android.volley.toolbox.ImageLoader.java
com.android.volley.toolbox.ImageRequest.java
com.android.volley.toolbox.JsonArrayRequest.java
com.android.volley.toolbox.JsonObjectRequest.java
com.android.volley.toolbox.JsonRequest.java
com.android.volley.toolbox.NetworkImageView.java
com.android.volley.toolbox.NoCache.java
com.android.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.volley.toolbox.RequestFuture.java
com.android.volley.toolbox.StringRequest.java
com.android.volley.toolbox.Volley.java
org.kevinmrohr.android_blog.activity.BlogViewActivity$$ViewInjector.java
org.kevinmrohr.android_blog.activity.BlogViewActivity.java
org.kevinmrohr.android_blog.activity.ListBlogsActivity$$InjectAdapter.java
org.kevinmrohr.android_blog.activity.ListBlogsActivity$$ViewInjector.java
org.kevinmrohr.android_blog.activity.ListBlogsActivity.java
org.kevinmrohr.android_blog.adapter.BindingAdapter.java
org.kevinmrohr.android_blog.adapter.BlogListAdapter.java
org.kevinmrohr.android_blog.async.BlogPostLoader.java
org.kevinmrohr.android_blog.fragments.BlogDetailFragment.java
org.kevinmrohr.android_blog.fragments.BlogListFragment.java
org.kevinmrohr.android_blog.model.BlogPost.java
org.kevinmrohr.android_blog.module.ListBlogsModule$$ModuleAdapter.java
org.kevinmrohr.android_blog.module.ObjectMappingModule.java
org.kevinmrohr.android_blog.serialization.CustomDateDeserializer.java
org.kevinmrohr.android_blog.serialization.CustomDateSerializer.java
org.kevinmrohr.android_blog.service.BlogPostService.java