| | |
| | | |
| | | import android.graphics.drawable.Drawable; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | |
| | | import androidx.annotation.LayoutRes; |
| | | import androidx.annotation.NonNull; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | import androidx.viewbinding.ViewBinding; |
| | | |
| | | import com.duqing.missions.R; |
| | | import com.duqing.missions.base.activities.BaseActivity; |
| | | import com.duqing.missions.common.NullViewHolder; |
| | | import com.duqing.missions.databinding.LayoutNullBinding; |
| | | import com.duqing.missions.util.DeviceUtil; |
| | | |
| | |
| | | * T 数据类型 |
| | | * V 适配器视图 |
| | | */ |
| | | public abstract class BaseAdapter<B extends ViewBinding,A extends BaseActivity,T> extends RecyclerView.Adapter { |
| | | public abstract class BaseAdapter<B extends ViewBinding,T> extends RecyclerView.Adapter { |
| | | |
| | | protected List<T> mData = new ArrayList<>(); |
| | | |
| | | protected Drawable nullDrawable; |
| | | protected String nullTxt="暂无数据"; |
| | | protected String TAG = "BaseAdapter"; |
| | | protected A activity; |
| | | protected BaseActivity activity; |
| | | |
| | | public BaseAdapter(){ |
| | | } |
| | |
| | | @NonNull |
| | | @Override |
| | | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { |
| | | // get genericity "B" |
| | | Class<B> entityClass = (Class<B>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; |
| | | try { |
| | | /* for(Method method: entityClass.getMethods()){ |
| | | StringBuilder sb = new StringBuilder(); |
| | | for(Class type : method.getParameterTypes()){ |
| | | sb.append(type.getSimpleName()+","); |
| | | } |
| | | Log.e(TAG,String.format("method:%s,return:%s,param:%s",method.getName(),method.getReturnType().getSimpleName(),sb.toString())); |
| | | }*/ |
| | | Method method = entityClass.getMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);//get method from name "inflate"; |
| | | B vBind = (B) method.invoke(entityClass,LayoutInflater.from(parent.getContext()),parent,false);//execute method to create a objct of viewbind; |
| | | return new ViewBindHolder(vBind); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | if(viewType == 1 ){ |
| | | // get genericity "B" |
| | | Class<B> entityClass = (Class<B>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; |
| | | try { |
| | | /* for(Method method: entityClass.getMethods()){ |
| | | StringBuilder sb = new StringBuilder(); |
| | | for(Class type : method.getParameterTypes()){ |
| | | sb.append(type.getSimpleName()+","); |
| | | } |
| | | Log.e(TAG,String.format("method:%s,return:%s,param:%s",method.getName(),method.getReturnType().getSimpleName(),sb.toString())); |
| | | }*/ |
| | | Method method = entityClass.getMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);//get method from name "inflate"; |
| | | B vBind = (B) method.invoke(entityClass,LayoutInflater.from(parent.getContext()),parent,false);//execute method to create a objct of viewbind; |
| | | return new ViewBindHolder(vBind); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return new NullViewHolder( LayoutNullBinding.inflate(LayoutInflater.from(parent.getContext()))); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * 创建视图 |
| | | * @param parent |
| | | * @param layout 视图文件 |
| | | * @param viewType 视图类型 1 加载正常视图 其他则加载空数据 |
| | | * @return |
| | | */ |
| | | protected View getRootView(@NonNull ViewGroup parent, @LayoutRes int layout,int viewType ){ |
| | | //MyLog.i(TAG,"getRootView viewType:"+viewType); |
| | | return LayoutInflater.from(parent.getContext()).inflate(viewType==1?layout: R.layout.layout_null,parent,false); |
| | | return new NullViewHolder( LayoutNullBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false)); |
| | | } |
| | | |
| | | @Override |
| | | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { |
| | | //MyLog.i(TAG,"onBindViewHolder position:"+position+" "+mData.size()+" "+getItemViewType(position)); |
| | | if(getItemViewType(position)==0){ |
| | | return; |
| | | if(activity == null){ |
| | | activity = (BaseActivity) holder.itemView.getContext(); |
| | | } |
| | | try { |
| | | activity = (A) holder.itemView.getContext(); |
| | | }catch (Exception e){} |
| | | bindView((ViewBindHolder) holder,mData.size()==0?null:mData.get(position),position); |
| | | if(getItemViewType(position)==0){ |
| | | bindView((NullViewHolder) holder); |
| | | }else { |
| | | bindView((ViewBindHolder) holder, mData.size() == 0 ? null : mData.get(position), position); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | protected abstract void bindView(ViewBindHolder holder,T data,int position); |
| | | |
| | | |
| | | protected void bindView(NullViewHolder holder){ |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public int getItemCount() { |
| | | //默认显示空视图,若不显示空视图则重写该方法,返回mData.size() |
| | |
| | | public int getItemViewType(int position) { |
| | | //当下标为0,数据集合为0 返回0(意味当前应显示空数据视图)) |
| | | //MyLog.i(TAG,"getItemViewType position:"+position+" mdata:"+mData.size()+" "+(position ==0 && mData.size()==0)); |
| | | return position ==0 && mData.size()==0?0:1; |
| | | return position == 0 && mData.size()==0?0:1; |
| | | } |
| | | |
| | | public class ViewBindHolder extends RecyclerView.ViewHolder{ |
| | | |
| | | B binding; |
| | | |
| | | public B binding; |
| | | public ViewBindHolder( B binding) { |
| | | super(binding.getRoot()); |
| | | this.binding = binding; |
| | | } |
| | | } |
| | | /** |
| | | * 空数据显示 |
| | | * Created by Administrator on 2021/10/28 0028. |
| | | */ |
| | | public class NullViewHolder extends RecyclerView.ViewHolder { |
| | | LayoutNullBinding binding; |
| | | |
| | | public NullViewHolder(LayoutNullBinding binding) { |
| | | super(binding.getRoot()); |
| | | this.binding = binding; |
| | | } |
| | | } |
| | | |
| | | } |