RecycleView多布局的实现

匿名 (未验证) 提交于 2019-12-03 00:22:01

http://www.jianshu.com/p/f6418086a1b3
介绍了RecycleView的简单使用,并实现了RecycleView的点击事件和长按事件,本篇文章主要讲解的是RecycleView实现多布局。
现在来看下效果图如下:

[ͼ1.png]

图二:

ͼ2.png

图三:

图三巴纽

上图+实现了四种布局:现在来看看主布局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:background="#E0F2F1"     android:paddingBottom="@dimen/activity_vertical_margin"     tools:context=".main.MainActivity">  <android.support.v7.widget.RecyclerView         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/recycleViewlist"         /> </RelativeLayout> 

其他四种布局:
布局一:item_01.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="200dp">     <ImageView         android:id="@+id/iv_item1"         android:src="@mipmap/ic_launcher"         android:layout_width="match_parent"         android:layout_height="200dp" />     <TextView         android:id="@+id/tv_item1"         android:text="title"         android:textColor="#e5ffffff"         android:textSize="20sp"         android:textStyle="bold"         android:layout_marginLeft="15dp"         android:layout_marginTop="20dp"         android:layout_marginRight="15dp"         android:layout_width="match_parent"         android:layout_height="wrap_content" />  </RelativeLayout> 

布局二:item_list_recycle.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:background="@android:color/holo_orange_dark"     android:layout_height="350dp">     <RelativeLayout         android:layout_width="match_parent"         android:layout_margin="10dp"         android:background="@android:color/white"         android:layout_height="330dp">         <ImageView             android:id="@+id/img_shopping_img"             android:src="@drawable/shop02"             android:layout_width="match_parent"             android:layout_margin="5dp"             android:layout_height="150dp" />           <LinearLayout             android:id="@+id/line_shoppong"             android:layout_width="match_parent"             android:orientation="vertical"             android:gravity="center"             android:layout_below="@id/img_shopping_img"             android:layout_height="wrap_content">             <TextView                 android:id="@+id/tv_shopping_name"                 android:text="手机"                 android:padding="5dp"                 android:textSize="15sp"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content" />             <TextView                 android:id="@+id/tv_shopping_desc"                 android:text="手机放寒假"                 android:padding="5dp"                 android:textSize="12sp"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content" />             <TextView                 android:id="@+id/tv_shopping_new_money"                 android:text="¥5567"                 android:padding="5dp"                 android:textSize="14sp"                 android:textColor="@android:color/holo_red_light"                 android:layout_width="wrap_content"                 android:layout_marginRight="10dp"                 android:layout_height="wrap_content" />             <View                 android:layout_width="0dp"                 android:layout_weight="1"                 android:layout_height="2dp"/>             <Button                 android:id="@+id/item_shopping_order"                 android:layout_width="120dp"                 android:text="立即购买"                 android:layout_marginBottom="10dp"                 android:layout_marginRight="10dp"                 android:background="@android:color/holo_red_light"                 android:textColor="@android:color/white"                 android:layout_height="30dp" />          </LinearLayout>     </RelativeLayout> </LinearLayout> 

布局三和布局五一样:item_grid_recycle.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:background="@android:color/holo_orange_light">      <RelativeLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margin="10dp"         android:background="@android:color/white">          <ImageView             android:id="@+id/img_shopping_img"             android:layout_width="match_parent"             android:layout_height="150dp"             android:layout_margin="5dp"             android:src="@drawable/shop02" />           <LinearLayout             android:id="@+id/line_shoppong"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_below="@id/img_shopping_img"             android:orientation="horizontal">              <TextView                 android:id="@+id/tv_shopping_name"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:padding="5dp"                 android:text="手机放寒假"                 android:textSize="15sp" />              <View                 android:layout_width="0dp"                 android:layout_height="2dp"                 android:layout_weight="1" />              <TextView                 android:id="@+id/tv_shopping_new_money"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_marginRight="10dp"                 android:padding="5dp"                 android:text="¥5567"                 android:textColor="@android:color/holo_red_light"                 android:textSize="14sp" />              <TextView                 android:id="@+id/tv_shopping_old_money"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:layout_marginRight="10dp"                 android:padding="5dp"                 android:text="原价:¥8200"                 android:textSize="12sp" />          </LinearLayout>          <LinearLayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_below="@id/line_shoppong"             android:layout_marginBottom="5dp"             android:orientation="horizontal">              <TextView                 android:id="@+id/tv_shopping_desc"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:padding="5dp"                  android:text="手机放寒假"                 android:textSize="12sp" />              <View                 android:layout_width="0dp"                 android:layout_height="2dp"                 android:layout_weight="1" />              <Button                 android:id="@+id/item_shopping_order"                 android:layout_width="120dp"                 android:layout_height="30dp"                 android:layout_marginBottom="10dp"                 android:layout_marginRight="10dp"                 android:background="@android:color/holo_red_light"                 android:text="立即购买"                 android:textColor="@android:color/white" />           </LinearLayout>       </RelativeLayout> </LinearLayout> 

布局四:item_recycle_02.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView     xmlns:card_view="http://schemas.android.com/apk/res-auto"     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:foreground="?android:attr/selectableItemBackground"     card_view:cardCornerRadius="4dp"     card_view:cardBackgroundColor="#dcb00f"     card_view:cardElevation="4dp"     >      <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"          android:orientation="vertical"         >          <TextView             android:id="@+id/tv_item2_text"             android:textColor="#eff1f0ef"             android:gravity="center"             android:text="手机话费框架和"             android:textSize="23sp"             android:padding="4dp"             android:layout_width="match_parent"             android:layout_height="wrap_content"/>         <TextView             android:id="@+id/tv_item2_text1"             android:gravity="center"             android:text="手机话费框架和"             android:textSize="14sp"             android:padding="4dp"             android:textColor="#eff1f0ef"             android:layout_width="match_parent"             android:layout_height="wrap_content"/>     </LinearLayout>  </android.support.v7.widget.CardView> 


其中方法getItemViewType(位置)获取数据源某个数据的类型;
onBindViewHolder(ViewHolder保持器,INT位置)根据不同的viewtype把数据绑定到对应的布局中

public class RecycleViewMulitAdapter extends RecyclerView.Adapter<RecycleViewMulitAdapter.ViewHolder> implements View.OnClickListener {      Context mContext;     List<RecycleListMode> listRecyDatas;     LayoutInflater inflate;     RecyclerView mRecycleView;     private OnItemClickListener clickListener;     public RecycleViewMulitAdapter(Context mContext, List<RecycleListMode> listRecyDatas) {          this.mContext=mContext;         this.listRecyDatas=listRecyDatas;         inflate= LayoutInflater.from(mContext);     }       @Override     public void onAttachedToRecyclerView(RecyclerView recyclerView) {         super.onAttachedToRecyclerView(recyclerView);          mRecycleView=recyclerView;     }      public void addRes(List<RecycleListMode> data){         if (data!=null){             this.listRecyDatas=data;             notifyDataSetChanged();         }      }      public void addLoadMore(List<RecycleListMode> dataload){         if (dataload.size()>0){             listRecyDatas.addAll(dataload);             notifyDataSetChanged();         }      }      public interface OnItemClickListener{         void setItemClickListener(int position);     }      public void setClickListener(RecycleViewMulitAdapter.OnItemClickListener clickListener){         this.clickListener=clickListener;      }      private static final String TAG = "RecycleViewMulitAdapter";     @Override     public void onClick(View v) {         int childAdapterPosition=mRecycleView.getChildAdapterPosition(v);         if (clickListener!=null){             clickListener.setItemClickListener(childAdapterPosition);              Log.i(TAG, "onClick: "+childAdapterPosition);         }      }     @Override     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {          View itemView=null;         switch (viewType){             case 0:                  itemView=inflate.inflate(R.layout.item_01,parent,false);                 break;             case 1:                  itemView=inflate.inflate(R.layout.item_list_recycle,parent,false);                 break;             case 2:                 itemView=inflate.inflate(R.layout.item_grid_recycle,parent,false);                 break;             case 3:                 itemView=inflate.inflate(R.layout.item_recycle_02,parent,false);                 break;             case 4:                 itemView=inflate.inflate(R.layout.item_list_recycle,parent,false);                 break;         }          ViewHolder holder=new ViewHolder(itemView);          itemView.setOnClickListener(this);          return holder;     }      @Override     public int getItemViewType(int position) {         return listRecyDatas.get(position).getStyle();     }      @Override     public void onBindViewHolder(ViewHolder holder, int position) {          switch (getItemViewType(position)){             case 0:                 ImageView imgitem= (ImageView) holder.getView(R.id.iv_item1);                 Glide.with(mContext).load(listRecyDatas.get(position).getImgURL()).into(imgitem);                  break;             case 1:             case 4:                 TextView tvName= (TextView) holder.getView(R.id.tv_shopping_name);                TextView tvDesc= (TextView) holder.getView(R.id.tv_shopping_desc);                TextView tvPrice= (TextView) holder.getView(R.id.tv_shopping_new_money);                ImageView imgOrder= (ImageView) holder.getView(R.id.img_shopping_img);                  tvName.setText(listRecyDatas.get(position).getName());                 tvDesc.setText(listRecyDatas.get(position).getContent());                 tvPrice.setText(listRecyDatas.get(position).getPrice());                 Glide.with(mContext).load(listRecyDatas.get(position).getImgURL()).into(imgOrder);                   break;             case 2:                  TextView tvName2= (TextView) holder.getView(R.id.tv_shopping_name);                 TextView tvDesc2= (TextView) holder.getView(R.id.tv_shopping_desc);                 TextView tvPrice2= (TextView) holder.getView(R.id.tv_shopping_new_money);                 ImageView imgOrder2= (ImageView) holder.getView(R.id.img_shopping_img);                  tvName2.setText(listRecyDatas.get(position).getName());                 tvDesc2.setText(listRecyDatas.get(position).getContent());                 tvPrice2.setText(listRecyDatas.get(position).getPrice());                 Glide.with(mContext).load(listRecyDatas.get(position).getImgURL()).into(imgOrder2);                  break;              case 3:               TextView textView= (TextView) holder.getView(R.id.tv_item2_text);               TextView textView1= (TextView) holder.getView(R.id.tv_item2_text1);                  textView.setText(listRecyDatas.get(position).getName());                 textView1.setText(listRecyDatas.get(position).getContent());          }      }      @Override     public int getItemCount() {         return listRecyDatas.size()>0? listRecyDatas.size():0;     }        // ViewHolder的创建使用共同的ViewHolder     public class ViewHolder extends RecyclerView.ViewHolder {          private Map<Integer,View> mCachView;         public ViewHolder(View itemView) {             super(itemView);              mCachView=new HashMap<>();          }          public View getView(int resId){             View view;             if (mCachView.containsKey(resId)){                 view=mCachView.get(resId);             }else{                 view=itemView.findViewById(resId);                 mCachView.put(resId,view);             }              return view;          }     } } 

在主页代码中:RecycleViewMuliteActivity实现数据危机

好,现在RecycleView的所布局已实现完毕。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!