Android-RecyclerView package multiple layouts

Encapsulation of multiple layout, the main development processing data binding interfaces, the interface returns the layout view, data binding: Providing a tag holder class attributes, initialization holder will return viewtype view layout as the identification, so that when the method bindData switch (holder.gettag ()) to determine the holder type, then get ItemView holder, which in turn findViewById, binding data. Back view layouts: the main handle multiple logical layouts, layout return what circumstances under

adapter: adapter for generating a generic package holder; id has parameters simultaneously view and layout in the layout process onCreateViewHolder, the id attribute is the tag holder compound added subsequent: to identify different layouts holder;

public abstract class RecycleViewAdapterUtil extends RecyclerView.Adapter<ViewHolder>{
    List list;
    Context context;
    public RecycleViewAdapterUtil(Context context, List list){
        this.list = list;
        this.context = context;
    }
    @Override
    public int getItemViewType(int position) {
        return this.get_ItemViewType(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(i, viewGroup, false),i);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
       bindData(holder,position);
    }

    @Override
    public int getItemCount() {
        return get_ItemCount();
    }
    public abstract int get_ItemCount();//此抽象方法用来返回自定义的item总个数
    public abstract int get_ItemViewType(int position);//此抽象方法是用来抽象出去让开发者自己定义返回的layout的对应Id
    public abstract void bindData(ViewHolder holder, int i);//因为自定义复杂布局所以不知道当前的item布局,需要抽象出去让开发者自己根据当前布局处理数据
}

holder class: the addition of several helper methods getview, set *** in holder class; after all, in the words written on the activity, it is too much. SaprseArray Android sparse array in more than save memory map.

public class ViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> Views;
    View myItemView;
    int tag;//viewholder标识符,来标识不同的viewholder
    private View.OnClickListener onItemClickLitener;
    private View.OnLongClickListener onLongClickListener;

    public ViewHolder(@NonNull View itemView, int tag) {
        super(itemView);
        this.myItemView = itemView;
        this.tag = tag;
        this.Views = new SparseArray<View>();
    }

    public int getTag() {
        return tag;
    }

    public View getView(int viewID) {
        View view = Views.get(viewID);
        if (view == null)
        {
            view = myItemView.findViewById(viewID);
            Views.put(viewID, view);
        }
        return view;
    }

    //设置监听的方法
    public void setOnItemClickLitener(View.OnClickListener onItemClickLitener) {
        myItemView.setOnClickListener(onItemClickLitener);
    }

    //设置长按监听
    public void setOnLongClickListener(View.OnLongClickListener onLongClickListener) {
        myItemView.setOnLongClickListener(onLongClickListener);
    }

    public ViewHolder setText(int viewID, String str) {
        TextView textView = (TextView) this.getView(viewID);
        textView.setText(str);
        return this;
    }

    public ViewHolder setImage(int viewID, String imgStr, Context context) {
        ImageView imageView = (ImageView) this.getView(viewID);
        Glide.with(context).load(imgStr).into(imageView);//这里用了Glide框架我用来连接网络图片
        return this;
    }

Call: For multi-layout, the most important thing is to return the layout type of logic, that is, get_ItemViewType method returns layout (R.layout.bujuitem), in get_ItemViewType specific implementation method can return a different layout according to the logic of what you want to achieve the layout itemId. This is just below the bottom added a loading bar, item total list.size () + 1; if the layout is mix kind of data can be placed in the list, the type of data stored list, e.g. list <user>, user class can list the type of property type1,2,3 (such as this type1,2,3: student groups, teacher groups, school groups may assume different layouts logo, type1.size () is the number 1 of this layout ,,,)

As for data binding, just to get the current layout of the holder in accordance with holder.getTag () method, fill data directly on the line.

ryviewdapter = new RecycleViewAdapterUtil(cookActivity.this, list) {

            @Override
            public int get_ItemViewType(int position) {//返回布局
                if (position == list.size()) {
                    return R.layout.viewfooter;
                }
                return R.layout.cook_namelist;
            }

            @Override
            public int get_ItemCount() {
                return list.size() + 1;
            }

            @Override
            public void bindData(final ViewHolder holder, int position) {
                switch (holder.getTag()) {
                    case R.layout.cook_namelist:
                        JsonObject jsonObject = list.get(position);
                        holder.setImage(R.id.list_cook_image, jsonObject.get("pic").getAsString(), cookActivity.this)
                                .setText(R.id.list_cook_name, "《" + jsonObject.get("name").getAsString() + "》" + "(" + jsonObject.get("peoplenum").getAsString() + ")(" + jsonObject.get("cookingtime").getAsString() + ")")
                                .setText(R.id.list_cook_content, jsonObject.get("content").getAsString())
                                .setText(R.id.list_cook_tag, "饮食标签:" + jsonObject.get("tag").getAsString() + position)
                                .setOnItemClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(cookActivity.this, cook_detail.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putString("cook_selected", new Gson().toJson(list.get(holder.getAdapterPosition())));
                                        intent.putExtras(bundle);
                                        cookActivity.this.startActivity(intent);
                                    }
                                });
                        break;
                    case R.layout.viewfooter:
                        if (data.ishavemore && data.isloadmore) {
                            holder.getView(R.id.view_foot_progressbar).setVisibility(View.VISIBLE);
                            ((TextView) holder.getView(R.id.view_foot_more)).setText("加载中");
                        } else if (data.isloadmore) {
                            holder.getView(R.id.view_foot_progressbar).setVisibility(View.GONE);
                            ((TextView) holder.getView(R.id.view_foot_more)).setText("无结果");
                        }
                        break;
                }

            }
        };

Attach a layout similar to the dialogue that customer service

Then attach the code:

写了机器人的xml在左边显示,人在右边显示(至于需要图片显示为圆形的,自行在网上找CircleImageView)

robot:xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/eyeopen"
        android:background="@color/colorAccent"/>
    <TextView
        android:id="@+id/robot_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginLeft="10dp"
        android:background="#d1d1d1"
        android:layout_gravity="center_vertical"
        android:text="robot的回复消息"/>
</LinearLayout>

people:xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right">

    <TextView
        android:id="@+id/people_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="10dp"
        android:layout_gravity="center_vertical"
        android:background="#d1d1d1"
        android:text="你在吗?" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/primary"
        android:src="@drawable/eyeclose"/>
</LinearLayout>

 

adapterutil:

        adapterUtil = new RecycleViewAdapterUtil(this,list) {
            @Override
            public int get_ItemCount() {
                return list.size();
            }

            @Override
            public int get_ItemViewType(int position) {
                if (list.get(position).getRole()==0){
                    return R.layout.robot_robot_item;
                }
                return R.layout.robot_people_item;
            }

            @Override
            public void bindData(ViewHolder holder, int i) {
                   switch (holder.getTag()){
//图片我都是在xml里写死的,毕竟人的图片,客服的图片又不会变
                       case R.layout.robot_robot_item:
                           holder.setText(R.id.robot_text,list.get(i).getText());
                           break;
                       case R.layout.robot_people_item:
                           holder.setText(R.id.people_text,list.get(i).getText());
                           break;
                   }
            }
        };
       recyclerView.setAdapter(adapterUtil);

实体类:

public class role {
    int role = 0;//初始为0:机器人;1:人
    String text = "";//说的话
    public role(int role,String text){
        this.role = role;
        this.text = text;
    }
    public int getRole() {
        return role;
    }

    public void setRole(int role) {
        this.role = role;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }


}

 

发布了3 篇原创文章 · 获赞 3 · 访问量 2239

Guess you like

Origin blog.csdn.net/qq_38858754/article/details/104200867