Android UI 军火库(1)

作者 : 开心源码 本文共4053个字,预计阅读时间需要11分钟 发布时间: 2022-05-12 共193人阅读

在 2018 下半年工作重点从 web 应用转向了 Android 原生开发。经过半年的努力开发应用终于上线了。开发过程中积累少量经验,可能是对的,也可能错误观点。
视图模型实现 Visitable 这个接口,实现 type 这个方法。我们是通过下面 TypeFact 来实现的。int type 接受一个实现了 TypeFactory 接口的类,让后调用 typeFactory的 type 方法将自己作为参数传入给 typeFactory.type 方法来获取对应的布局 id

public interface Visitable {    int type(TypeFactory typeFactory);}
public class RepoModel implements Visitable{  // stuff doint type(TypeFactory typeFactory){  return typeFactory.type(this)}}

我们定义 TypeFactory 接口,而后定义方法 type, type 接受 Visitable 类型数据模型,而后返回一个 int 也就是视图 id,type 作用是将数据模型和视图建立关系。让每一个数据模型可以对应资源 id,从可以根据 id 获取布局 xml。第二个方法是创立 ViewHolder,viewholder 想必大家作为一个 android developer 对此应该并不陌生。根据输入视图和资源id 返回 BaseViewHolder 对象,代码在后面可以找到。

public interface TypeFactory {    int type(RepoModel repoModel);    BaseViewHolder createViewHolder(int type, View itemView);}

ItemTypeFactory 是 TypeFactory 具体实现,这里我们通过定义一个静态变量为我们组件视图的 id,id 对应一个我们定义好的视图布局 xml。例如 R.layout.panel_repo 就对应一个布局 xml 文件。而后复写 type 方法,这里假如我们传入的类型为 RepoModel 就将返回一 PANEL_SEPERATOR 这个刚刚定义好的资源静态变量。

public int type(RepoModel repoModel) {        return PANEL_SEPERATOR;    }

createViewHolder 是根据 type 来对应创立 ViewHolder 而后将 view 传入到 ViewHolder 作为参数,在

typeFactory.createViewHolder(viewType,view); 会根据视图类型创立一个 viewholder 而后 view 作为 viewholder 的参数来获取 viewholder
public class ItemTypeFactory implements TypeFactory {    public static final int PANEL_SEPERATOR =            R.layout.panel_repo;    @Override    public int type(RepoModel repoModel) {        return PANEL_SEPERATOR;    }    @Override    public BaseViewHolder createViewHolder(int type, View itemView) {        switch (type){            case PANEL_SEPERATOR:                return new RepoViewHolder(itemView);            default:                return null;        }    }}
<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/panel_repo_title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginStart="8dp"        android:layout_marginLeft="8dp"        android:layout_marginTop="8dp"        android:layout_marginEnd="8dp"        android:layout_marginRight="8dp"        android:layout_marginBottom="8dp"        android:text="@string/app_name"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>

定义一个笼统类 BaseViewHolder,这里定义了一个泛型确定这里模型 T 为 Visiable 类型的模型类,而后继承于 RecyclerView.ViewHolder。在其中定义一个 SparseArray<View> 定义视图数组和一个视图对象。
而后定义一个笼统方法 bindViewData 这个将数据模型绑定到视图

public abstract class BaseViewHolder<T extends Visitable> extends RecyclerView.ViewHolder {    private SparseArray<View> mViews;    private View mItemView;    public BaseViewHolder(View itemView) {        super(itemView);        mItemView = itemView;        mViews = new SparseArray<>();    }    /**     * 根据id 获取视图     * @param resId     * @return     */    public View getView(int resId){        View view = mViews.get(resId);        if(view == null){            view = mItemView.findViewById(resId);            mViews.put(resId, view);        }        return view;    }    /**     * 将数据与视图绑定     * @param data     */    public abstract void bindViewData(T data);}

我们这里创立一个 recyclerview 的 dapter,首先这里有一个 List<Visitable> 还记得我们所有的数据模型都继承这个类,我们每次将不同类型的数据模型(都实现了 Visitable)接口的类都增加到这个 mData 中,而后值得说明的是顶一个 WeakHasMap

public class MultiRecyclerViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {    List<Visitable> mData;    TypeFactory typeFactory;    LayoutInflater layoutInflater;    WeakHashMap<Integer,BaseViewHolder> hashMap = new WeakHashMap<>();    public MultiRecyclerViewAdapter(List<Visitable> mData) {        this.mData = mData;        this.typeFactory = new ItemTypeFactory();    }    public List<Visitable> getmData() {        return mData;    }    public void setmData(List<Visitable> mData) {        this.mData = mData;    }    @NonNull    @Override    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {        layoutInflater = (LayoutInflater) parent.getContext()                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        View view = layoutInflater.inflate(viewType,parent,false);        return typeFactory.createViewHolder(viewType,view);    }    @Override    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {        holder.bindViewData(mData.get(position));    }    @Override    public int getItemViewType(int position) {        return mData.get(position).type(typeFactory);    }    @Override    public int getItemCount() {        return (mData != null ? mData.size() : 0);    }}
  • 而后在 recyclerView 的视图绑定环节,可以调用 holder 的 bindViewData 的方法来讲数据绑定到视图上。

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Android UI 军火库(1)

发表回复