码迷,mamicode.com
首页 > 其他好文 > 详细

万能适配器

时间:2015-09-02 15:59:08      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

初学Android,通过网上视频、资料整理了万能适配器的编写,记录一下,方便以后使用。

CommonAdapter

技术分享
public abstract class CommonAdapter<T> extends BaseAdapter {

    protected Context mContext;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;
    private int layoutId;
    
    public CommonAdapter(Context context, List<T> datas, int layoutId){
        this.mContext = context;
        this.mDatas = datas;
        mInflater = LayoutInflater.from(context);
        this.layoutId = layoutId;
    }
    
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View cnvertview, ViewGroup parent) {
        ViewHolder holder = ViewHolder.get(mContext, cnvertview, parent, position, layoutId);
        View v = holder.getConvertView();
        convert(holder, getItem(position));
        return v;
    }
    
    public abstract void convert(ViewHolder holder,T t);

}
View Code

ViewHolder

技术分享
public class ViewHolder {
    
    private SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;
    
    public int getPosition() {
        return mPosition;
    }

    public ViewHolder(Context context, ViewGroup parent, int layoutId, int position){
        this.mPosition = position;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,false);
        mConvertView.setTag(this);
    }
    
    public static ViewHolder get(Context context, View convertView,ViewGroup parent, int position, int layoutId){
        if(convertView == null){
            return new ViewHolder(context,parent,layoutId,position);
        }else{
            ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.mPosition = position;
            return holder;
        }
    }
    
    //通过viewid获取控件
    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if(view == null){
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T)view;
    }
    
    public View getConvertView(){
        return mConvertView;
    }
    
    /**
     * 设置TextView的值
     */
    public ViewHolder setText(int viewId, String text){
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }
    /**
     * ImageView
     */
    public ViewHolder setImageResource(int viewId, int resId){
        ImageView iv = getView(viewId);
        iv.setImageResource(resId);
        return this;
    }
    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap){
        ImageView iv = getView(viewId);
        iv.setImageBitmap(bitmap);
        return this;
    }
    public ViewHolder setImageURI(int viewId, String url){
        ImageView iv = getView(viewId);
        new ImageLoader().showImageByThread(iv, url);
        return this;
    }
    
    //控件....
}
View Code

ImageView加载(url)

技术分享
public class ImageLoader {

    private ImageView mImageView;
    
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            mImageView.setImageBitmap((Bitmap) msg.obj);
        };
    };
    
    public Bitmap getBitmapForUrl(String urlString){
        Bitmap bitmap;
        InputStream is = null;
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            is = new BufferedInputStream(connection.getInputStream());
            bitmap = BitmapFactory.decodeStream(is);
            connection.disconnect();
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    public void showImageByThread(ImageView imageView, final String url){
        mImageView = imageView;
        new Thread(){
            @Override
            public void run() {
                super.run();
                Bitmap bitmap = getBitmapForUrl(url);
                Message message = Message.obtain();
                message.obj = bitmap;
                handler.sendMessage(message);
            }
        }.start();
    }
    
    public void showImageByAsyncTask(ImageView imageView, String url){
        new ImageLoaderAsyncTask(imageView).execute(url);
    }
    
    private class ImageLoaderAsyncTask extends AsyncTask<String, Void, Bitmap>{
        
        private ImageView mImageView;
        
        public ImageLoaderAsyncTask(ImageView imageView){
            mImageView = imageView;
        }
        
        @Override
        protected Bitmap doInBackground(String... params) {
            return getBitmapForUrl(params[0]);
        }
        
        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            mImageView.setImageBitmap(result);
        }
        
    }
}
View Code

 

万能适配器

标签:

原文地址:http://www.cnblogs.com/xmlovejaejoong/p/4778780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!