美文网首页
andorid BaseRecyclerViewAdapterH

andorid BaseRecyclerViewAdapterH

作者: 苍天霸气诀 | 来源:发表于2018-01-27 14:27 被阅读0次
    dan.jpg

    第三方 库 git 地址 https://github.com/CymChad/BaseRecyclerViewAdapterHelper

    常用功能:

    • 添加Item事件
    • Item的点击事件
    • Item的长按事件
    • Item子控件的点击事件
    • Item子控件的长按事件
    • 添加列表加载动画
    • 一行代码轻松切换5种默认动画。
    • 添加头部、尾部
    • 一行代码搞定,感觉又回到ListView时代。
    • 自动加载
    • 上拉加载无需监听滑动事件,可自定义加载布局,显示异常提示,自定义异常提示。同时支持下拉加载。
    • 分组布局
    • 随心定义分组头部。
    • 多布局
    • 简单配置、无需重写额外方法。
    • 设置空布局
    • 比Listview的setEmptyView还要好用。
    • 添加拖拽、滑动删除
    • 开启,监听即可,就是这么简单。
    • 树形列表
    • 比ExpandableListView还要强大,支持多级。
    • 自定义ViewHolder
    • 支持自定义ViewHolder,让开发者随心所欲。
    • 扩展框架
    • 组合第三方框架,轻松实现更多需求定制。

    框架引入

    先在 build.gradle

    buildscript {
        
        repositories {
            google()
            jcenter(
            )
            maven { url "https://jitpack.io" }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.1'
            
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
            maven { url "https://jitpack.io" }
        }
    
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    然后在 build.gradle(Module:app) 的 dependencies 添加:

        dependencies {
                compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
        }
    
    

    多布局

    RecyclerView 使用和数据添加

     rv_list= (RecyclerView) findViewById(R.id.rv_list);
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
            rv_list.setLayoutManager(linearLayoutManager);
    
            List<ChatMultipleItem> list=new ArrayList<>();
            for (int i=0;i<8;i++){
                int itemType=i%2;
                ChatMultipleItem chatMultipleItem=new ChatMultipleItem(itemType,"第"+i+"个");
                list.add(chatMultipleItem);
            }
            ChatMainAdapter adapter=new ChatMainAdapter(this,list);
            rv_list.setAdapter(adapter);
    

    ChatMainAdapter 代码

    public class ChatMainAdapter extends BaseMultiItemQuickAdapter<ChatMultipleItem,BaseViewHolder> {
    
        private String imge1="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517034699540&di=6a9afc9b394eafeec7dd4234dd42aca1&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fa044ad345982b2b713b5ad7d3aadcbef76099b65.jpg";
        private String imge2="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517034295939&di=3ceafe0c6f289a68585dddea96bf1daf&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F503d269759ee3d6db032f61b48166d224e4ade6e.jpg";
    
        public ChatMainAdapter(Context context,List<ChatMultipleItem> data) {
            super(data);
            // 添加类型
            addItemType(ChatMultipleItem.CHATTYPETEXT, R.layout.chat_item_text_view);
            addItemType(ChatMultipleItem.CHATTYPETEXTANDIMG,R.layout.chat_item_image_view);
        }
        // 图片地址
    //    https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517034295939&di=3ceafe0c6f289a68585dddea96bf1daf&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F503d269759ee3d6db032f61b48166d224e4ade6e.jpg
    
        @Override
        protected void convert(BaseViewHolder helper, ChatMultipleItem item) {
            if(helper.getItemViewType()==ChatMultipleItem.CHATTYPETEXT){
                // 第一个类型
                helper.setText(R.id.tv,item.getContent());
            }else if(helper.getItemViewType()==ChatMultipleItem.CHATTYPETEXTANDIMG){
                ImageView imageView=helper.getView(R.id.iv);
                Log.i("position",helper.getLayoutPosition()%2+"  "+helper.getLayoutPosition());
                if(helper.getLayoutPosition()%2 ==0){
                    Glide.with(mContext).load(imge1).crossFade().into(imageView);
                }else{
                    Glide.with(mContext).load(imge2).crossFade().into(imageView);
                }
    
    
            }
        }
    }
    

    ChatMultipleItem 代码 一定要继承 MultiItemEntity

    public class ChatMultipleItem implements MultiItemEntity {
    
        public static int CHATTYPETEXT=0;
        public static  int CHATTYPETEXTANDIMG=1;
        private int itemType;
        private String content;
    
    
        public ChatMultipleItem(int itemType,String content) {
            this.itemType = itemType;
            this.content=content;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        @Override
        public int getItemType() {
            return itemType;
        }
    }
    
    

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:textSize="18sp"
                android:textStyle="bold"
    
                />
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="@android:color/darker_gray"
                android:textSize="14sp"
                android:text="If you would hit the mark, you must aim a little above it. Every arrow that flies feels the attraction of earth. -Henry Wadsworth Longfellow."
                />
        </LinearLayout>
    </android.support.v7.widget.CardView>
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="center"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true">
        <ImageView
            android:id="@+id/iv"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:scaleType="fitXY"
            />
    </android.support.v7.widget.CardView>
    

    一个布局的adapter

    public class ChatQuickAdapter extends BaseQuickAdapter<ChatMultipleItem,BaseViewHolder> {
        private String imge1="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1517034699540&di=6a9afc9b394eafeec7dd4234dd42aca1&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fa044ad345982b2b713b5ad7d3aadcbef76099b65.jpg";
    
        public ChatQuickAdapter(int layoutResId, @Nullable List<ChatMultipleItem> data) {
            super(layoutResId, data);
        }
    
        @Override
        protected void convert(BaseViewHolder helper, ChatMultipleItem item) {
            helper.setText(R.id.tv,item.getContent());
            ImageView imageView=helper.getView(R.id.chat_iv_bg);
            Glide.with(mContext).load(imge1).into(imageView);
        }
    }
    
    MultiItem.jpg

    参考链接
    https://www.jianshu.com/p/b343fcff51b0

    相关文章

      网友评论

          本文标题:andorid BaseRecyclerViewAdapterH

          本文链接:https://www.haomeiwen.com/subject/pspnaxtx.html