Android recycleview实现混合itemview,以及recycleview添加头部尾部

需要引入的module链接:https://pan.baidu.com/s/1kcVye2vT4M0mdCtzYpUVKw

添加module方法步骤:

(1)File----->New------>Import Module找到下载的banner文件

(2)app的build.gradle下的dependencies下添加

implementation project(path: ':library')

如:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23+'
    compile 'com.android.support:recyclerview-v7:23+'
    compile "com.github.bumptech.glide:glide:3.7.0"
    compile 'com.facebook.fresco:fresco:0.12.0'
    compile 'com.zxy.android:recovery:0.0.8'
 implementation project(path: ':library')
//    compile 'com.youth.banner:banner:+'
}

(3)settings.gradle下添加‘banner’

include ':app', ':library'

1、主要adapter

/**
 * 作者:created by meixi
 * 邮箱:[email protected]
 * 日期:2018/8/31 11
 */

public class NewsAdapter extends BaseMultiItemQuickAdapter<TestEntity, BaseViewHolder> {
    public NewsAdapter(@Nullable List<TestEntity> data) {
        super(data);
        addItemType(TestEntity.style_0, R.layout.item_add_groups_number);//头部item
        addItemType(TestEntity.style_1, R.layout.item_news_1);//混合1item
        addItemType(TestEntity.style_2, R.layout.item_news_2);//混合2item
    }

    @Override
    protected void convert(BaseViewHolder helper, TestEntity item) {
        switch (helper.getItemViewType()) {
            case TestEntity.style_0:
                helper.setText(R.id.item_company_member_name, item.getOnes());//标题
//                initConvenientBanner((ConvenientBanner) helper.getView(R.id.bannerNews), item.getBanner());
                break;
            case TestEntity.style_1:
            case TestEntity.style_2:
//                helper.setImageUrl(R.id.iv, item.getContent());
//                ImageManager.loadRoundCornerImageWithMargin(mContext, item.getImg(), R.drawable.list_img, (ImageView) helper.getView(R.id.item_gank_image), 0);//da
                helper.setText(R.id.item_gank_desc, item.getTitle());//标题

//                helper.setText(R.id.item_read, "阅读" + String.valueOf(item.getBrowse() + item.getRandomBrowse()));

                break;
        }
    }
}

2、entity类

public class TestEntity implements Serializable, MultiItemEntity {

    private String title;
    private String https;
    private String ones;
    private boolean ifshow;
    private int itemType;

    public static final int style_0 = 0;
    public static final int style_1 = 1;
    public static final int style_2 = 2;//重要参数

    public String getOnes() {
        return ones;
    }

    public void setOnes(String ones) {
        this.ones = ones;
    }

    public int getItemType() {
        return itemType;
    }

    public void setItemType(int itemType) {
        this.itemType = itemType;
    }

    public boolean isIfshow() {
        return ifshow;
    }

    public void setIfshow(boolean ifshow) {
        this.ifshow = ifshow;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getHttps() {
        return https;
    }

    public void setHttps(String https) {
        this.https = https;
    }
}

3、MainActivity调用recycleview

初始化

private List<TestEntity> testEntities = new ArrayList<>();
private RecyclerView recyclerView;
private NewsAdapter mNewsAdapter;
recyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
mNewsAdapter = new NewsAdapter(testEntities);
mNewsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Toast.makeText(MainActivity.this,position+"nnnn",Toast.LENGTH_LONG).show();
    }
});
recyclerView.setAdapter(mNewsAdapter);

赋值

         for (int i=0;i<29;i++){
             TestEntity testEntity = new TestEntity();

             if (i%4==0&&i!=0){
                 testEntity.setItemType(2);
             }else {
                 testEntity.setItemType(1);
             }
             if (i==0){
                 testEntity.setItemType(0);
                 testEntity.setHttps("baidu.com");
                 testEntity.setTitle("百度。com");
                 testEntity.setOnes("头部");
             }
             if (i==28){
                 testEntity.setItemType(0);
                 testEntity.setHttps("baidu.com");
                 testEntity.setTitle("百度。com");
                 testEntity.setOnes("尾部");
             }
             testEntity.setHttps("baidu.com");
             testEntity.setTitle("百度。com");
             testEntities.add(testEntity);
             if (i==28){
//                 newgroupsAdapter.notifyDataSetChanged();
                 mNewsAdapter.notifyLoadMoreToLoading();
             }
         }

头部与尾部item_add_groups_number.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:melove="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/item_company_member_layout"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        >

        <ImageView
            android:id="@+id/item_information_choose_image"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="12dp"
            android:src="@drawable/ic_launcher_background"
            android:visibility="invisible"/>

        <ImageView
            android:id="@+id/item_company_member_head_image"
            android:layout_width="44dp"
            android:layout_height="44dp"
            android:layout_below="@+id/item_information_choose_image"
            android:layout_centerHorizontal="true"
            android:scaleType="centerCrop"
            android:visibility="gone"
            android:src="@drawable/ic_launcher_background"
            />

            <TextView
                android:id="@+id/item_company_member_name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="8dp"
                android:layout_below="@+id/item_company_member_head_image"
                android:background="@color/colorPrimary"
                android:layout_centerHorizontal="true"
                android:text="林晓"
                android:gravity="center"
                android:textSize="18sp"/>
    </RelativeLayout>

</LinearLayout>

混合1   item_news_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:melove="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/holder_reward_layout"
              android:layout_width="match_parent"
              android:layout_height="98dp"
              android:orientation="vertical">
    <!--android:background="?attr/selectableItemBackground"-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="6"
        android:gravity="center_vertical"
        android:orientation="horizontal">



        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:orientation="vertical">

            <TextView
                android:id="@+id/item_gank_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="4dp"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="资讯"
                android:textColor="#323232"
                android:textSize="15dp"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/item_gank_who"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="5dp"
                    android:ellipsize="end"
                    android:maxLength="6"
                    android:text="name"
                    android:textColor="#aeaeae"
                    android:textSize="12.5sp"/>
                <LinearLayout
                    android:layout_width="2dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="2dp"
                    android:layout_marginTop="3dp"
                    android:layout_marginBottom="2dp"></LinearLayout>

                <TextView
                    android:id="@+id/item_gank_createdAt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="2017年12月"
                    android:textColor="#aeaeae"
                    android:textSize="12.5sp"
                    android:layout_marginLeft="3dp"/>

                <LinearLayout
                    android:layout_width="2dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="3dp"
                    android:layout_marginTop="3dp"
                    android:layout_marginBottom="2dp"></LinearLayout>

                <TextView
                    android:id="@+id/item_read"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="name"
                    android:textColor="#aeaeae"
                    android:layout_marginLeft="3dp"
                    android:textSize="12.5sp"/>
            </LinearLayout>

        </FrameLayout>

        <ImageView
            android:id="@+id/item_gank_image"
            android:layout_width="0dp"
            android:layout_height="85dp"
            android:layout_weight="2"
            android:src="@drawable/ic_launcher_background"
            android:scaleType="centerCrop"/>


    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="3dp" />


</LinearLayout>

混合2   item_news_2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:melove="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/holder_reward_layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

            <ImageView
                android:id="@+id/item_gank_image"
                android:layout_width="match_parent"
                android:layout_height="165dp"
                android:layout_weight="2"
                android:scaleType="centerCrop"
                android:layout_alignParentTop="true"
                android:src="@drawable/ic_launcher_background"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:layout_weight="4"
        android:layout_alignBottom="@+id/item_gank_image"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_gank_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="4dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="资讯"
            android:textSize="15dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/item_gank_createdAt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="2dp"
                android:text="2017年12月"
                android:textSize="13sp"/>

            <TextView
                android:id="@+id/item_gank_who"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="2dp"
                android:text="name"

                android:textSize="13sp"/>

            <TextView
                android:id="@+id/item_read"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="2dp"
                android:text="name"

                android:textSize="13sp"/>
        </LinearLayout>

    </LinearLayout>
    <!--</android.support.v7.widget.CardView>-->

</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/82256319