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

发布于:2025-02-10 ⋅ 阅读:(71) ⋅ 点赞:(0)
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 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 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 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>



### 混合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" />



### 混合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"

网站公告

今日签到

点亮在社区的每一天
去签到