Adapter封装

1.先创建一个类  名为BaseRecyclerViewAdapterextends RecyclerView.Adapter

直接上封装代码

package com.orange.clientin.base.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.orange.clientin.R;
import com.orange.clientin.utils.GlideUtil;
import com.rd.veuisdk.manager.Config;

import java.util.List;


public abstract class BaseRecyclerViewAdapter extends RecyclerView.Adapter {
    protected List tList;
    protected Context mContext;
    protected LayoutInflater mLayoutInflater;
    protected int layId;
    private OnItemClickListener mOnItemClickListener;
    protected BaseRecyclerViewAdapter.ViewHolder holder;

    public BaseRecyclerViewAdapter(List tList, Context mContext, int layId) {
        this.tList = tList;
        this.mContext = mContext;
        this.layId=layId;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(layId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final BaseRecyclerViewAdapter.ViewHolder holder, final int position) {
        this.holder=holder;
        convert(holder,tList.get(position),position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(position,holder,view);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return tList.size();
    }

    public abstract void convert(ViewHolder holder,T itemData,int position);

    public class ViewHolder extends RecyclerView.ViewHolder  {

        private SparseArray mViewArray;

        public ViewHolder(View itemView) {
            super(itemView);
            mViewArray=new SparseArray<>();
        }

        /**
         * 通过id得到view
         * @param viewId
         * @param 
         * @return
         */
        public V getViewAtId(int viewId){
            View view = mViewArray.get(viewId);
            if(view==null){
                view=itemView.findViewById(viewId);
                mViewArray.put(viewId,view);
            }
            return (V)view;
        }


        /**
         * 设置Tag
         * @param viewId
         * @param
         * @return
         */
        public ViewHolder setTag(int viewId,Object object){
            ImageView imageView= getViewAtId(viewId);
            imageView.setTag(object);
            return  this;
        }
        /**
         * 设置文字
         * @param viewId
         * @param text
         * @return
         */
        public ViewHolder setText(int viewId,CharSequence text){
            TextView textView= getViewAtId(viewId);
            textView.setText(text);
            return  this;
        }

        /**
         * 得到文字
         * @param viewId
         * @return
         */
        public String getText(int viewId){
            TextView textView= getViewAtId(viewId);
            return  textView.getText().toString();
        }

        /**
         * 设置文字
         * @param viewId
         * @param color
         * @return
         */
        public ViewHolder setTextColor(int viewId,int color){
            TextView textView= getViewAtId(viewId);
            textView.setTextColor(color);
            return  this;
        }

        /**
         * 加载错误显示默认图
         * @param context
         * @param url
         * @param imageView
         */
        private  RequestOptions default_options = RequestOptions.errorOf(R.drawable.jishuang_placeholder).placeholder(R.drawable.jishuang_placeholder);
        public ViewHolder loadUrlDefault(Context context,String url,int viewId){
            ImageView imageView= getViewAtId(viewId);
            imageView.setTag(null);
            if(url!=null && url.startsWith("GZB")){
                StringBuilder sb = new StringBuilder(Config.OSSHEADER);
                sb.append("/");
                sb.append(url);
                url = sb.toString();
            }
            Glide.with(context).load(url).apply(default_options).into(imageView);
            return  this;
        }
        
        /**
         * 加载图片
         * @param viewId
         * @return
         */
        public ViewHolder setImageFromUrl(int viewId,String imgUrl){
            ImageView imageView= getViewAtId(viewId);
            GlideUtil.loadUrlDefault(imageView.getContext(),imgUrl,imageView);
            return this;
        }





        /**
         * 通过资源来设置图片资源
         * @param viewId
         * @param resId
         * @return
         */
        public ViewHolder setImageFromRes(int viewId,int resId){
            ImageView imageView = getViewAtId(viewId);
            imageView.setImageResource(resId);
            return this;
        }
        /**
         * 设置背景颜色
         * @param viewId
         * @param color
         * @return
         */
        public ViewHolder setBackgroundColor(int viewId,int color){
            RelativeLayout relativeLayout = getViewAtId(viewId);
            relativeLayout.setBackgroundColor(color);
            return this;
        }

        /**
         * 设置背景样式
         * @param viewId
         * @param resid
         * @return
         */
        public ViewHolder setBackground(int viewId, int resid){
            RelativeLayout relativeLayout = getViewAtId(viewId);
            if(resid==0){
                relativeLayout.setBackground(null);
            }else relativeLayout.setBackgroundResource(resid);
            return this;
        }

        /**
         * 设置背景样式
         * @param viewId
         * @param resid
         * @return
         */
        public ViewHolder setBackgroundText(int viewId, int resid){
            TextView textView = getViewAtId(viewId);
            textView.setBackgroundResource(resid);
            return this;
        }

        /**
         * 设置点击事件
         * @param viewId
         *@return
         */
        public ViewHolder SetOnItemClickListener(int viewId,View.OnClickListener onClickListener){
            View view = getViewAtId(viewId);
            view.setOnClickListener(onClickListener);
            return this;
        }

        /**
         * 设置控件透明度
         * @param viewId
         * @param visibility
         * @return
         */
        public ViewHolder setViewVisibility(int viewId,int visibility){
            View view = getViewAtId(viewId);
            view.setVisibility(visibility);
            return this;
        }



    }


    /**
     * 点击事件接口
     */
    public interface OnItemClickListener{
        void onItemClick(int position,BaseRecyclerViewAdapter.ViewHolder viewHolder,View view);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;

    }


}


2.再加上Glide封装

public class GlideUtil {
    /**
     * 默认的加载方式
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadUrl(Context context, String url, ImageView imageView){
        imageView.setTag(null);
        Glide.with(context).load(url).into(imageView);
    }

    public static void loadOssUrl(Context context,String url,ImageView imageView){
        imageView.setTag(null);

        if(url!=null && url.startsWith("GZB")){
            StringBuilder sb = new StringBuilder(Config.OSSHEADER);
            sb.append("/");
            sb.append(url);
            url = sb.toString();
        }
        Glide.with(context).load(url).into(imageView);
    }

    private static RequestOptions options = RequestOptions.errorOf(R.drawable.sznc_tx_nan);
    public static void loadOssUrlHeader(Context context,String url,ImageView imageView){
        imageView.setTag(null);
        if(url!=null && url.startsWith("GZB")){
            StringBuilder sb = new StringBuilder(Config.OSSHEADER);
            sb.append("/");
            sb.append(url);
            url = sb.toString();
        }
        Glide.with(context).load(url).apply(options).into(imageView);
    }

    /**
     * 加载错误显示默认图
     * @param context
     * @param url
     * @param imageView
     */
    private static RequestOptions default_options = RequestOptions.errorOf(R.drawable.jishuang_placeholder).placeholder(R.drawable.jishuang_placeholder);
    public static void loadUrlDefault(Context context,String url,ImageView imageView){
        imageView.setTag(null);
        if(url!=null && url.startsWith("GZB")){
            StringBuilder sb = new StringBuilder(Config.OSSHEADER);
            sb.append("/");
            sb.append(url);
            url = sb.toString();
        }else if(url.startsWith("videos")){
            StringBuilder sb = new StringBuilder(Config.OSSOUYEHEADER);
            sb.append("/");
            sb.append(url);
            url = sb.toString();
        }
        Glide.with(context).load(url).apply(default_options).into(imageView);
    }

    public static void loadUrlDefaultOuYe(Context context,String url,ImageView imageView){
        imageView.setTag(null);
        if(url!=null && url.startsWith("GZB")){
            StringBuilder sb = new StringBuilder(Config.OSSOUYEHEADER);
            sb.append("/");
            sb.append(url);
            url = sb.toString();
        }
        Glide.with(context).load(url).apply(default_options).into(imageView);
    }
    /**
     * 加载错误显示默认图   头像
     * @param context
     * @param url
     * @param imageView
     */
    private static RequestOptions default_head = RequestOptions.errorOf(R.drawable.sznc_tx_nan).placeholder(R.drawable.sznc_tx_nan);
    public static void loadUrlHead(Context context,String url,ImageView imageView){
        imageView.setTag(null);
        if(url!=null && url.startsWith("GZB")){
            StringBuilder sb = new StringBuilder(Config.OSSHEADER);
            sb.append("/");
            sb.append(url);
            url = sb.toString();
        }
        Glide.with(context).load(url).apply(default_options).into(imageView);
    }


    public static void loadUrl(Context context, String url, final Simple simpleTarget){

        Glide.with(context).load(url).into(new SimpleTarget() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) {
                simpleTarget.Drawable(resource);
                simpleTarget.Transition(transition);
            }
        });

    }
    public  interface Simple{
        void Drawable(Drawable resource);
        void Transition(Transition resource);
    }


}

3.如何使用 

// 适配器
 public class RecyclerAdapter extends BaseRecyclerViewAdapter {
        public RecyclerAdapter(List lists, Context mContext, int layId, RecyclerView recyclerView) {
            super(lists, mContext, layId);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        }
        @Override
        public void convert(ViewHolder holder, String itemData, int position) {
            holder.setText(R.id.text,"");
        }
 }

4.设置适配器

//主函数用法   str就是你的数据list  R.layout.item_recycler这是你的item布局  recler这是你的recyclevice
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(Str, this, R.layout.item_recycler, recler);
        recler.setAdapter(recyclerAdapter);

        recyclerAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, BaseRecyclerViewAdapter.ViewHolder viewHolder) {
//                viewHolder.setText(R.id.text,"9");
            }
        });

 

 

 

 

 

 

你可能感兴趣的:(Adapter封装)