Android基类设计方法详解

1 为什么要设计基类

        为什么要给程序设计基类呢?主要是出于2个原因,一是方便代码编写,减少重复代码和冗余逻辑,优化代码;二是优化程序架构,降低耦合度,方便拓展、修改。

        ok,编写代码是程序员的第一步,那么第二步就是要编写高质量的代码,代码能实现功能是一方面,写的优美则是另一方面,这也是我们所有攻城狮们应该追求的境界。

2 设计基类的基本思路

        那么,哪些东西我们需要抽象到基类中呢? 

        2.1 重复的代码:如果一个逻辑是大多数子类都需要使用的

        2.2 臭而长的代码:典型的findviewbyid、Toast

        2.3 需要修改原生行为的:要改变某控件的样式

        2.4 为子类创建标准流程模板:模板设计模式

        2.5 常用的方法:转换dip、px

3 常用基类的设计实例

        3.1 BaseActivity
[java]  view plain copy print ?
  1. public abstract class BaseActivity extends Activity {  
  2.   
  3.     protected int mScreenWidth;  
  4.     protected int mScreenHeight;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.   
  10.         // 公共属性  
  11.         DisplayMetrics metric = new DisplayMetrics();  
  12.         getWindowManager().getDefaultDisplay().getMetrics(metric);  
  13.         mScreenWidth = metric.widthPixels;  
  14.         mScreenHeight = metric.heightPixels;  
  15.   
  16.         // 定制流程  
  17.         setContentView();  
  18.         initViews();  
  19.         initListeners();  
  20.         initData();  
  21.     }  
  22.   
  23.     Toast mToast;  
  24.   
  25.     public abstract void setContentView();  
  26.   
  27.     public abstract void initViews();  
  28.   
  29.     public abstract void initListeners();  
  30.   
  31.     public abstract void initData();  
  32.   
  33.     // 常用方法  
  34.     public void ShowToast(String text) {  
  35.         if (!TextUtils.isEmpty(text)) {  
  36.             if (mToast == null) {  
  37.                 mToast = Toast.makeText(getApplicationContext(), text,  
  38.                         Toast.LENGTH_SHORT);  
  39.             } else {  
  40.                 mToast.setText(text);  
  41.             }  
  42.             mToast.show();  
  43.         }  
  44.     }  
  45.   
  46.     // 常用方法  
  47.     public int getStateBar() {  
  48.         Rect frame = new Rect();  
  49.         getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
  50.         int statusBarHeight = frame.top;  
  51.         return statusBarHeight;  
  52.     }  
  53.   
  54.     // 常用方法  
  55.     public static int dip2px(Context context, float dipValue) {  
  56.         float scale = context.getResources().getDisplayMetrics().density;  
  57.         return (int) (scale * dipValue + 0.5f);  
  58.     }  
  59. }  

        3.2 BaseAdapter
[java]  view plain copy print ?
  1. /** 
  2.  * Copyright 2013 Joan Zapata 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *     http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. import android.annotation.TargetApi;  
  18. import android.content.Context;  
  19. import android.graphics.Bitmap;  
  20. import android.graphics.Typeface;  
  21. import android.graphics.drawable.Drawable;  
  22. import android.os.Build;  
  23. import android.text.util.Linkify;  
  24. import android.util.SparseArray;  
  25. import android.view.LayoutInflater;  
  26. import android.view.View;  
  27. import android.view.ViewGroup;  
  28. import android.view.animation.AlphaAnimation;  
  29. import android.widget.ImageView;  
  30. import android.widget.ProgressBar;  
  31. import android.widget.RatingBar;  
  32. import android.widget.TextView;  
  33.   
  34. import com.squareup.picasso.Picasso;  
  35. import com.squareup.picasso.RequestCreator;  
  36.   
  37. /** 
  38.  * Allows an abstraction of the ViewHolder pattern.<br> 
  39.  * <br> 
  40.  * <p/> 
  41.  * <b>Usage</b> 
  42.  * <p/> 
  43.  * <pre> 
  44.  * return BaseAdapterHelper.get(context, convertView, parent, R.layout.item) 
  45.  *         .setText(R.id.tvName, contact.getName()) 
  46.  *         .setText(R.id.tvEmails, contact.getEmails().toString()) 
  47.  *         .setText(R.id.tvNumbers, contact.getNumbers().toString()) 
  48.  *         .getView(); 
  49.  * </pre> 
  50.  */  
  51. @TargetApi(Build.VERSION_CODES.HONEYCOMB)  
  52. public class BaseAdapterHelper {  
  53.   
  54.     /** Views indexed with their IDs */  
  55.     private final SparseArray<View> views;  
  56.   
  57.     private final Context context;  
  58.   
  59.     private int position;  
  60.   
  61.     private View convertView;  
  62.   
  63.     private BaseAdapterHelper(Context context, ViewGroup parent, int layoutId, int position) {  
  64.         this.context = context;  
  65.         this.position = position;  
  66.         this.views = new SparseArray<View>();  
  67.         convertView = LayoutInflater.from(context) //  
  68.                 .inflate(layoutId, parent, false);  
  69.         convertView.setTag(this);  
  70.     }  
  71.   
  72.     /** 
  73.      * This method is the only entry point to get a BaseAdapterHelper. 
  74.      * @param context     The current context. 
  75.      * @param convertView The convertView arg passed to the getView() method. 
  76.      * @param parent      The parent arg passed to the getView() method. 
  77.      * @return A BaseAdapterHelper instance. 
  78.      */  
  79.     public static BaseAdapterHelper get(Context context, View convertView, ViewGroup parent, int layoutId) {  
  80.         return get(context, convertView, parent, layoutId, -1);  
  81.     }  
  82.   
  83.     /** This method is package private and should only be used by QuickAdapter. */  
  84.     static BaseAdapterHelper get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {  
  85.         if (convertView == null) {  
  86.             return new BaseAdapterHelper(context, parent, layoutId, position);  
  87.         }  
  88.         return (BaseAdapterHelper) convertView.getTag();  
  89.     }  
  90.   
  91.     /** 
  92.      * This method allows you to retrieve a view and perform custom 
  93.      * operations on it, not covered by the BaseAdapterHelper.<br/> 
  94.      * If you think it's a common use case, please consider creating 
  95.      * a new issue at https://github.com/JoanZapata/base-adapter-helper/issues. 
  96.      * @param viewId The id of the view you want to retrieve. 
  97.      */  
  98.     public <T extends View> T getView(int viewId) {  
  99.         return retrieveView(viewId);  
  100.     }  
  101.   
  102.     /** 
  103.      * Will set the text of a TextView. 
  104.      * @param viewId The view id. 
  105.      * @param value  The text to put in the text view. 
  106.      * @return The BaseAdapterHelper for chaining. 
  107.      */  
  108.     public BaseAdapterHelper setText(int viewId, String value) {  
  109.         TextView view = retrieveView(viewId);  
  110.         view.setText(value);  
  111.         return this;  
  112.     }  
  113.   
  114.     /** 
  115.      * Will set the image of an ImageView from a resource id. 
  116.      * @param viewId     The view id. 
  117.      * @param imageResId The image resource id. 
  118.      * @return The BaseAdapterHelper for chaining. 
  119.      */  
  120.     public BaseAdapterHelper setImageResource(int viewId, int imageResId) {  
  121.         ImageView view = retrieveView(viewId);  
  122.         view.setImageResource(imageResId);  
  123.         return this;  
  124.     }  
  125.   
  126.     /** 
  127.      * Will set background color of a view. 
  128.      * @param viewId The view id. 
  129.      * @param color  A color, not a resource id. 
  130.      * @return The BaseAdapterHelper for chaining. 
  131.      */  
  132.     public BaseAdapterHelper setBackgroundColor(int viewId, int color) {  
  133.         View view = retrieveView(viewId);  
  134.         view.setBackgroundColor(color);  
  135.         return this;  
  136.     }  
  137.   
  138.     /** 
  139.      * Will set background of a view. 
  140.      * @param viewId        The view id. 
  141.      * @param backgroundRes A resource to use as a background. 
  142.      * @return The BaseAdapterHelper for chaining. 
  143.      */  
  144.     public BaseAdapterHelper setBackgroundRes(int viewId, int backgroundRes) {  
  145.         View view = retrieveView(viewId);  
  146.         view.setBackgroundResource(backgroundRes);  
  147.         return this;  
  148.     }  
  149.   
  150.     /** 
  151.      * Will set text color of a TextView. 
  152.      * @param viewId    The view id. 
  153.      * @param textColor The text color (not a resource id). 
  154.      * @return The BaseAdapterHelper for chaining. 
  155.      */  
  156.     public BaseAdapterHelper setTextColor(int viewId, int textColor) {  
  157.         TextView view = retrieveView(viewId);  
  158.         view.setTextColor(textColor);  
  159.         return this;  
  160.     }  
  161.   
  162.     /** 
  163.      * Will set the image of an ImageView from a drawable. 
  164.      * @param viewId   The view id. 
  165.      * @param drawable The image drawable. 
  166.      * @return The BaseAdapterHelper for chaining. 
  167.      */  
  168.     public BaseAdapterHelper setImageDrawable(int viewId, Drawable drawable) {  
  169.         ImageView view = retrieveView(viewId);  
  170.         view.setImageDrawable(drawable);  
  171.         return this;  
  172.     }  
  173.   
  174.     /** 
  175.      * Will download an image from a URL and put it in an ImageView.<br/> 
  176.      * It uses Square's Picasso library to download the image asynchronously and put the result into the ImageView.<br/> 
  177.      * Picasso manages recycling of views in a ListView.<br/> 
  178.      * If you need more control over the Picasso settings, use {BaseAdapterHelper#setImageBuilder}. 
  179.      * @param viewId   The view id. 
  180.      * @param imageUrl The image URL. 
  181.      * @return The BaseAdapterHelper for chaining. 
  182.      */  
  183.     public BaseAdapterHelper setImageUrl(int viewId, String imageUrl) {  
  184.         ImageView view = retrieveView(viewId);  
  185.         Picasso.with(context).load(imageUrl).into(view);  
  186.         return this;  
  187.     }  
  188.   
  189.     /** 
  190.      * Will download an image from a URL and put it in an ImageView.<br/> 
  191.      * @param viewId         The view id. 
  192.      * @param requestBuilder The Picasso request builder. (e.g. Picasso.with(context).load(imageUrl)) 
  193.      * @return The BaseAdapterHelper for chaining. 
  194.      */  
  195.     public BaseAdapterHelper setImageBuilder(int viewId, RequestCreator requestBuilder) {  
  196.         ImageView view = retrieveView(viewId);  
  197.         requestBuilder.into(view);  
  198.         return this;  
  199.     }  
  200.   
  201.     /** Add an action to set the image of an image view. Can be called multiple times. */  
  202.     public BaseAdapterHelper setImageBitmap(int viewId, Bitmap bitmap) {  
  203.         ImageView view = retrieveView(viewId);  
  204.         view.setImageBitmap(bitmap);  
  205.         return this;  
  206.     }  
  207.   
  208.     /** 
  209.      * Add an action to set the alpha of a view. Can be called multiple times. 
  210.      * Alpha between 0-1. 
  211.      */  
  212.     public BaseAdapterHelper setAlpha(int viewId, float value) {  
  213.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {  
  214.             retrieveView(viewId).setAlpha(value);  
  215.         } else {  
  216.             // Pre-honeycomb hack to set Alpha value  
  217.             AlphaAnimation alpha = new AlphaAnimation(value, value);  
  218.             alpha.setDuration(0);  
  219.             alpha.setFillAfter(true);  
  220.             retrieveView(viewId).startAnimation(alpha);  
  221.         }  
  222.         return this;  
  223.     }  
  224.   
  225.     /** 
  226.      * Set a view visibility to VISIBLE (true) or GONE (false). 
  227.      * @param viewId  The view id. 
  228.      * @param visible True for VISIBLE, false for GONE. 
  229.      * @return The BaseAdapterHelper for chaining. 
  230.      */  
  231.     public BaseAdapterHelper setVisible(int viewId, boolean visible) {  
  232.         View view = retrieveView(viewId);  
  233.         view.setVisibility(visible ? View.VISIBLE : View.GONE);  
  234.         return this;  
  235.     }  
  236.   
  237.     /** 
  238.      * Add links into a TextView. 
  239.      * @param viewId The id of the TextView to linkify. 
  240.      * @return The BaseAdapterHelper for chaining. 
  241.      */  
  242.     public BaseAdapterHelper linkify(int viewId) {  
  243.         TextView view = retrieveView(viewId);  
  244.         Linkify.addLinks(view, Linkify.ALL);  
  245.         return this;  
  246.     }  
  247.   
  248.     /** Apply the typeface to the given viewId */  
  249.     public BaseAdapterHelper setTypeface(int viewId, Typeface typeface) {  
  250.         TextView view = retrieveView(viewId);  
  251.         view.setTypeface(typeface);  
  252.         return this;  
  253.     }  
  254.   
  255.     /** Apply the typeface to all the given viewIds */  
  256.     public BaseAdapterHelper setTypeface(Typeface typeface, int... viewIds) {  
  257.         for (int viewId : viewIds) {  
  258.             TextView view = retrieveView(viewId);  
  259.             view.setTypeface(typeface);  
  260.         }  
  261.         return this;  
  262.     }  
  263.   
  264.     /** 
  265.      * Sets the progress of a ProgressBar. 
  266.      * @param viewId   The view id. 
  267.      * @param progress The progress. 
  268.      * @return The BaseAdapterHelper for chaining. 
  269.      */  
  270.     public BaseAdapterHelper setProgress(int viewId, int progress) {  
  271.         ProgressBar view = retrieveView(viewId);  
  272.         view.setProgress(progress);  
  273.         return this;  
  274.     }  
  275.   
  276.     /** 
  277.      * Sets the progress and max of a ProgressBar. 
  278.      * @param viewId   The view id. 
  279.      * @param progress The progress. 
  280.      * @param max      The max value of a ProgressBar. 
  281.      * @return The BaseAdapterHelper for chaining. 
  282.      */  
  283.     public BaseAdapterHelper setProgress(int viewId, int progress, int max) {  
  284.         ProgressBar view = retrieveView(viewId);  
  285.         view.setProgress(progress);  
  286.         view.setMax(max);  
  287.         return this;  
  288.     }  
  289.   
  290.     /** 
  291.      * Sets the range of a ProgressBar to 0...max. 
  292.      * @param viewId The view id. 
  293.      * @param max    The max value of a ProgressBar. 
  294.      * @return The BaseAdapterHelper for chaining. 
  295.      */  
  296.     public BaseAdapterHelper setMax(int viewId, int max) {  
  297.         ProgressBar view = retrieveView(viewId);  
  298.         view.setMax(max);  
  299.         return this;  
  300.     }  
  301.   
  302.     /** 
  303.      * Sets the rating (the number of stars filled) of a RatingBar. 
  304.      * @param viewId The view id. 
  305.      * @param rating The rating. 
  306.      * @return The BaseAdapterHelper for chaining. 
  307.      */  
  308.     public BaseAdapterHelper setRating(int viewId, float rating) {  
  309.         RatingBar view = retrieveView(viewId);  
  310.         view.setRating(rating);  
  311.         return this;  
  312.     }  
  313.   
  314.     /** 
  315.      * Sets the rating (the number of stars filled) and max of a RatingBar. 
  316.      * @param viewId The view id. 
  317.      * @param rating The rating. 
  318.      * @param max    The range of the RatingBar to 0...max. 
  319.      * @return The BaseAdapterHelper for chaining. 
  320.      */  
  321.     public BaseAdapterHelper setRating(int viewId, float rating, int max) {  
  322.         RatingBar view = retrieveView(viewId);  
  323.         view.setRating(rating);  
  324.         view.setMax(max);  
  325.         return this;  
  326.     }  
  327.   
  328.     /** Retrieve the convertView */  
  329.     public View getView() {  
  330.         return convertView;  
  331.     }  
  332.   
  333.     /** 
  334.      * Retrieve the overall position of the data in the list. 
  335.      * @throws IllegalArgumentException If the position hasn't been set at the construction of the this helper. 
  336.      */  
  337.     public int getPosition() {  
  338.         if (position == -1)  
  339.             throw new IllegalStateException("Use BaseAdapterHelper constructor " +  
  340.                     "with position if you need to retrieve the position.");  
  341.         return position;  
  342.     }  
  343.   
  344.     @SuppressWarnings("unchecked")  
  345.     private <T extends View> T retrieveView(int viewId) {  
  346.         View view = views.get(viewId);  
  347.         if (view == null) {  
  348.             view = convertView.findViewById(viewId);  
  349.             views.put(viewId, view);  
  350.         }  
  351.         return (T) view;  
  352.     }  
  353.   
  354. }  

如何使用呢:
[java]  view plain copy print ?
  1. /** 
  2.  * Copyright 2013 Joan Zapata 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *     http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. import android.content.Context;  
  18. import android.view.Gravity;  
  19. import android.view.View;  
  20. import android.view.ViewGroup;  
  21. import android.widget.BaseAdapter;  
  22. import android.widget.FrameLayout;  
  23. import android.widget.ProgressBar;  
  24.   
  25. import java.util.ArrayList;  
  26. import java.util.List;  
  27.   
  28. /** 
  29.  * Abstraction class of a BaseAdapter in which you only need 
  30.  * to provide the convert() implementation.<br/> 
  31.  * Using the provided BaseAdapterHelper, your code is minimalist. 
  32.  * @param <T> The type of the items in the list. 
  33.  */  
  34. public abstract class BaseQuickAdapter<T, H extends BaseAdapterHelper> extends BaseAdapter {  
  35.   
  36.     protected static final String TAG = BaseQuickAdapter.class.getSimpleName();  
  37.   
  38.     protected final Context context;  
  39.   
  40.     protected final int layoutResId;  
  41.   
  42.     protected final List<T> data;  
  43.   
  44.     protected boolean displayIndeterminateProgress = false;  
  45.   
  46.     /** 
  47.      * Create a QuickAdapter. 
  48.      * @param context     The context. 
  49.      * @param layoutResId The layout resource id of each item. 
  50.      */  
  51.     public BaseQuickAdapter(Context context, int layoutResId) {  
  52.         this(context, layoutResId, null);  
  53.     }  
  54.   
  55.     /** 
  56.      * Same as QuickAdapter#QuickAdapter(Context,int) but with 
  57.      * some initialization data. 
  58.      * @param context     The context. 
  59.      * @param layoutResId The layout resource id of each item. 
  60.      * @param data        A new list is created out of this one to avoid mutable list 
  61.      */  
  62.     public BaseQuickAdapter(Context context, int layoutResId, List<T> data) {  
  63.         this.data = data == null ? new ArrayList<T>() : new ArrayList<T>(data);  
  64.         this.context = context;  
  65.         this.layoutResId = layoutResId;  
  66.     }  
  67.   
  68.     @Override  
  69.     public int getCount() {  
  70.         int extra = displayIndeterminateProgress ? 1 : 0;  
  71.         return data.size() + extra;  
  72.     }  
  73.   
  74.     @Override  
  75.     public T getItem(int position) {  
  76.         if (position >= data.size()) return null;  
  77.         return data.get(position);  
  78.     }  
  79.   
  80.     @Override  
  81.     public long getItemId(int position) {  
  82.         return position;  
  83.     }  
  84.   
  85.     @Override  
  86.     public int getViewTypeCount() {  
  87.         return 2;  
  88.     }  
  89.   
  90.     @Override  
  91.     public int getItemViewType(int position) {  
  92.         return position >= data.size() ? 1 : 0;  
  93.     }  
  94.   
  95.     @Override  
  96.     public View getView(int position, View convertView, ViewGroup parent) {  
  97.         if (getItemViewType(position) == 0) {  
  98.             final H helper = getAdapterHelper(position, convertView, parent);  
  99.             convert(helper, getItem(position));  
  100.             return helper.getView();  
  101.         }  
  102.   
  103.         return createIndeterminateProgressView(convertView, parent);  
  104.     }  
  105.   
  106.     private View createIndeterminateProgressView(View convertView, ViewGroup parent) {  
  107.         if (convertView == null) {  
  108.             FrameLayout container = new FrameLayout(context);  
  109.             container.setForegroundGravity(Gravity.CENTER);  
  110.             ProgressBar progress = new ProgressBar(context);  
  111.             container.addView(progress);  
  112.             convertView = container;  
  113.         }  
  114.         return convertView;  
  115.     }  
  116.   
  117.     @Override  
  118.     public boolean isEnabled(int position) {  
  119.         return position < data.size();  
  120.     }  
  121.   
  122.     public void add(T elem) {  
  123.         data.add(elem);  
  124.         notifyDataSetChanged();  
  125.     }  
  126.   
  127.     public void addAll(List<T> elem) {  
  128.         data.addAll(elem);  
  129.         notifyDataSetChanged();  
  130.     }  
  131.   
  132.     public void set(T oldElem, T newElem) {  
  133.         set(data.indexOf(oldElem), newElem);  
  134.     }  
  135.   
  136.     public void set(int index, T elem) {  
  137.         data.set(index, elem);  
  138.         notifyDataSetChanged();  
  139.     }  
  140.   
  141.     public void remove(T elem) {  
  142.         data.remove(elem);  
  143.         notifyDataSetChanged();  
  144.     }  
  145.   
  146.     public void remove(int index) {  
  147.         data.remove(index);  
  148.         notifyDataSetChanged();  
  149.     }  
  150.   
  151.     public boolean contains(T elem) {  
  152.         return data.contains(elem);  
  153.     }  
  154.   
  155.     /** Clear data list */  
  156.     public void clear() {  
  157.         data.clear();  
  158.         notifyDataSetChanged();  
  159.     }  
  160.   
  161.     public void showIndeterminateProgress(boolean display) {  
  162.         if (display == displayIndeterminateProgress) return;  
  163.         displayIndeterminateProgress = display;  
  164.         notifyDataSetChanged();  
  165.     }  
  166.   
  167.     /** 
  168.      * Implement this method and use the helper to adapt the view to the given item. 
  169.      * @param helper A fully initialized helper. 
  170.      * @param item   The item that needs to be displayed. 
  171.      */  
  172.     protected abstract void convert(H helper, T item);  
  173.   
  174.     /** 
  175.      * You can override this method to use a custom BaseAdapterHelper in order to fit your needs 
  176.      * 
  177.      * @param position The position of the item within the adapter's data set of the item whose view we want. 
  178.      * @param convertView The old view to reuse, if possible. Note: You should check that this view 
  179.      *        is non-null and of an appropriate type before using. If it is not possible to convert 
  180.      *        this view to display the correct data, this method can create a new view. 
  181.      *        Heterogeneous lists can specify their number of view types, so that this View is 
  182.      *        always of the right type (see {@link #getViewTypeCount()} and 
  183.      *        {@link #getItemViewType(int)}). 
  184.      * @param parent The parent that this view will eventually be attached to 
  185.      * @return An instance of BaseAdapterHelper 
  186.      */  
  187.     protected abstract H getAdapterHelper(int position, View convertView, ViewGroup parent);  
  188.   
  189. }  

最后再看下我们在设置adapter的时候如何使用:
[java]  view plain copy print ?
  1. /** 
  2.  * Copyright 2013 Joan Zapata 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *     http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. import java.util.List;  
  18.   
  19. import android.content.Context;  
  20. import android.view.View;  
  21. import android.view.ViewGroup;  
  22. import static com.bmob.lostfound.adapter.BaseAdapterHelper.get;  
  23.   
  24. /** 
  25.  * Abstraction class of a BaseAdapter in which you only need 
  26.  * to provide the convert() implementation.<br/> 
  27.  * Using the provided BaseAdapterHelper, your code is minimalist. 
  28.  * @param <T> The type of the items in the list. 
  29.  */  
  30. public abstract class QuickAdapter<T> extends BaseQuickAdapter<T, BaseAdapterHelper> {  
  31.   
  32.     /** 
  33.      * Create a QuickAdapter. 
  34.      * @param context     The context. 
  35.      * @param layoutResId The layout resource id of each item. 
  36.      */  
  37.     public QuickAdapter(Context context, int layoutResId) {  
  38.         super(context, layoutResId);  
  39.     }  
  40.   
  41.     /** 
  42.      * Same as QuickAdapter#QuickAdapter(Context,int) but with 
  43.      * some initialization data. 
  44.      * @param context     The context. 
  45.      * @param layoutResId The layout resource id of each item. 
  46.      * @param data        A new list is created out of this one to avoid mutable list 
  47.      */  
  48.     public QuickAdapter(Context context, int layoutResId, List<T> data) {  
  49.         super(context,layoutResId,data);  
  50.     }  
  51.   
  52.     protected BaseAdapterHelper getAdapterHelper(int position, View convertView, ViewGroup parent) {  
  53.         return get(context, convertView, parent, layoutResId, position);  
  54.     }  
  55.   
  56. }  

是不算少写很多代码,也许有人会说你写前面那么多代码,比你后面少写的还要多。确实,但是你忽略了一点就是上面的基类是通用的,也就是说,你一句话不用改就可以在其他项目中用了,你的程序员生涯就再也不用写那么多adapter了。

以上,关于Android基类设计方法就到这里
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值