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

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

添加module方法步骤:https://blog.csdn.net/meixi_android/article/details/84655666

1、主要adapter

/**
 * 作者:created by meixi
 * 邮箱:13164716840@163.com
 * 日期: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;
    }
}

 

public class ShareRecylerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Handler.Callback{
   private List<ShareListItemInEntity> listEntity;
   private Context context;
   private LayoutInflater inflater;
   private ListOnItemListener onItemListener;
   private ShotOnClickListener onShotListener;
   private Handler handler;

   public void setOnItemListener(ListOnItemListener onItemListener){
      this.onItemListener = onItemListener;
   }

   public void setOnShotListener(ShotOnClickListener onShotListener){
      this.onShotListener = onShotListener;
   }

   public ShareRecylerViewAdapter(Context context, List<ShareListItemInEntity> entitys) {
      this.context = context;
      this.listEntity = entitys;
      inflater =LayoutInflater.from(context);
      handler = new Handler(Looper.getMainLooper(), this);
   }

   @Override
   public int getItemViewType(int position) {//item类型方法
      if (listEntity.get(position).getType() == SharePlatformType.DIRECT_SHARE_PLAT) {
         return SharePlatformType.DIRECT_SHARE_PLAT;
      } else if (listEntity.get(position).getType() == SharePlatformType.TITLE_SHARE_PLAT) {
         return SharePlatformType.TITLE_SHARE_PLAT;
      } else  {
         return SharePlatformType.FOREIGN_SHARE_PLAT;
      }
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//itemlayout加载
      final View view;
      if (viewType == SharePlatformType.DIRECT_SHARE_PLAT) {
         view = inflater.inflate(R.layout.layout_share_yanshi_item,parent,false);
         return new ShareViewHolder(view);//绑定数据
      } else if (viewType == SharePlatformType.TITLE_SHARE_PLAT) {
         view = inflater.inflate(R.layout.share_title_content,parent,false);
         return new ShareViewHolderTitle(view);
      } else {
         view = inflater.inflate(R.layout.share_content_text,parent,false);
         return new ShareViewHolderContext(view);
      }
   }

   @Override
   public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {//绑定事件
      if(holder instanceof ShareViewHolderTitle){
         ShareViewHolderTitle viewHolderTitle = (ShareViewHolderTitle)holder;
         viewHolderTitle.setData(position);
      }
      if(holder instanceof ShareViewHolder){
         ShareViewHolder shareViewHolder = (ShareViewHolder)holder;
         shareViewHolder.shareSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//             onOneKeyShare();
            }
         });
         shareViewHolder.shotSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onShotListener.onClick();
            }
         });
         shareViewHolder.sharkSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onShotListener.onSharkClick();
            }
         });
      }
      if(holder instanceof ShareViewHolderContext){
         final ShareViewHolderContext viewHolderContext = (ShareViewHolderContext)holder;
         viewHolderContext.setData(position);
         viewHolderContext.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onItemListener.onClick(viewHolderContext.itemView,viewHolderContext.getLayoutPosition());
            }
         });
      }
   }
   private void showToast(String text){
      Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
   }



   @Override
   public int getItemCount() {
      return listEntity.size();
   }

   @Override
   public boolean handleMessage(Message msg) {
      String toastMsg = (String) msg.obj;
      showToast(toastMsg);
      return false;
   }

   class ShareViewHolder extends RecyclerView.ViewHolder {
      public ImageView shareSelected;
      public ImageView shotSelected;
      public ImageView sharkSelected;
      public ShareViewHolder(View itemView) {
         super(itemView);
         shareSelected =(ImageView) itemView.findViewById(R.id.mSelected);
         shotSelected = (ImageView) itemView.findViewById(R.id.mShot);
         sharkSelected = (ImageView) itemView.findViewById(R.id.mShark);
      }
   }
   class ShareViewHolderTitle extends RecyclerView.ViewHolder {
      public TextView textView;
      public ShareViewHolderTitle(View itemView) {
         super(itemView);
         textView =(TextView) itemView.findViewById(R.id.titleTxt);
      }

      public void setData(int position){
         textView.setText(listEntity.get(position).getName());
      }
   }

   class ShareViewHolderContext extends RecyclerView.ViewHolder {
      public ImageView icon;
      public TextView name;
      public RelativeLayout layout;
      public ShareViewHolderContext(View itemView) {
         super(itemView);
         icon = (ImageView)itemView.findViewById(R.id.mIcon);
         name = (TextView)itemView.findViewById(R.id.mTitle);
         layout = (RelativeLayout) itemView.findViewById(R.id.onMainLayout);
      }

      public void setData(int postion){
         name.setText(listEntity.get(postion).getName());
         icon.setImageResource(listEntity.get(postion).getIcon());
      }
   }

   public interface ListOnItemListener{
      public void onClick(View view,int position);
   }

   public interface ShotOnClickListener{
      public void onClick();
      public void onSharkClick();
   }
}

 

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值