Android 中RecyclerView多种item布局的写法(头布局+脚布局)

Android 中RecyclerView多种item布局的写法(头布局+脚布局),第1张

概述RecyclerView多个item布局写法(头布局+脚布局)上图github下载源码Initialcommit第一次提交的代码,为本文内容

RecyclerVIEw多个item布局的写法(头布局+脚布局)

上图

github

下载源码

Initial commit第一次提交的代码,为本文内容

以下的为主要代码,看注释即可,比较简单

MainActivity

含上拉加载更多

package com.anew.recyclervIEwall;import androID.os.Bundle;import androID.os.Handler;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.DefaultItemAnimator;import androID.support.v7.Widget.divIDerItemdecoration;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.support.v7.Widget.Toolbar;import androID.util.Log;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {  private RecyclerVIEw mRecyclerVIEw;  private List<String> mheadList;  private List<String> mBodyList;  private List<String> mFootList;  private MyAdapter mAdapter;  private linearlayoutmanager mLayoutManager;  Handler mHandler = new Handler();  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar);    setSupportActionbar(toolbar);    mheadList = new ArrayList<>();    for (int i = 0; i < 1; i++) {      mheadList.add("head" + i);    }    mBodyList = new ArrayList<>();    for (int i = 'A'; i < 'L'; i++) {      mBodyList.add("" + (char) i);    }    mFootList = new ArrayList<>();    for (int i = 0; i < 1; i++) {      mFootList.add("foot" + i);    }    mRecyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.rv);//    使用默认的API绘制分割线    mRecyclerVIEw.addItemdecoration        (new divIDerItemdecoration(this,divIDerItemdecoration.HORIZONTAL));//    设置增加删除item的动画效果    mRecyclerVIEw.setItemAnimator(new DefaultItemAnimator());//    瀑布流//    mRecyclerVIEw.setLayoutManager(new StaggeredGrIDLayoutManager(4,StaggeredGrIDLayoutManager.VERTICAL));    mLayoutManager = new linearlayoutmanager(this);    mRecyclerVIEw.setLayoutManager(mLayoutManager);    mAdapter = new MyAdapter(this,mheadList,mBodyList,mFootList);    mRecyclerVIEw.setAdapter(mAdapter);    // 当目前的可见条目是所有数据的最后一个时,开始加载新的数据    mRecyclerVIEw.addOnScrollListener(new RecyclerVIEw.OnScrollListener() {      @OverrIDe      public voID onScrolled(RecyclerVIEw recyclerVIEw,int dx,int dy) {        super.onScrolled(recyclerVIEw,dx,dy);        int lastCompletelyVisibleItemposition = mLayoutManager.findLastCompletelyVisibleItemposition();        if (lastCompletelyVisibleItemposition + 1 == mAdapter.getItemCount()) {          mHandler.postDelayed(new Runnable() {            @OverrIDe            public voID run() {              List<String> mMoreList = new ArrayList<>();              for (int i = 0; i < 3; i++) {                mMoreList.add("more" + i);              }              mBodyList.addAll(mMoreList);              mAdapter.notifyItemInserted(mAdapter.getItemCount() - 2);            }          },1500);          Log.e("qqq","到底了" + lastCompletelyVisibleItemposition);        }        int firstCompletelyVisibleItemposition = mLayoutManager.findFirstCompletelyVisibleItemposition();        if (firstCompletelyVisibleItemposition == 0&&dy<0) {          Log.e("eeeee","开始刷新呀呀呀");        }      }    });  }}

MyAdapter

对外部:头布局List、身体布局List、脚布局List,所有的List单独计数

内部:对position进行调整,这里需要耐心调整position

package com.anew.recyclervIEwall;import androID.content.Context;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;import androID.Widget.Toast;import java.util.List;/** * Created by a on 2017/1/7. */public class MyAdapter extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder> {  private Context context;  private List<String> mheadList;  private List<String> mBodyList;  private List<String> mFootList;  private static final int head_TYPE = 1;  private static final int BODY_TYPE = 2;  private static final int FOOT_TYPE = 3;  public MyAdapter(Context context,List<String> headList,List<String> bodyList,List<String> footList) {    this.context = context;    this.mheadList = headList;    this.mBodyList = bodyList;    this.mFootList = footList;  }  //  ★ 1. 定义规则,什么情况,是哪一种VIEwType  @OverrIDe  public int getItemVIEwType(int position) {    int vIEwType = -1;    if (position < getheadCount()) {      vIEwType = head_TYPE;    } else if (position > getheadCount() + getbodyCount() - 1) {      //这里最后要减去1,慢慢地去数      vIEwType = FOOT_TYPE;    } else {      vIEwType = BODY_TYPE;    }    return vIEwType;  }  @OverrIDe  public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {    LayoutInflater inflater = LayoutInflater.from(context);    VIEw vIEw = null;    //  ★ 2. 根据vIEwType来确定加载那个布局,返回哪一个VIEwHolder    switch (vIEwType) {      case head_TYPE:        vIEw = inflater.inflate(R.layout.item_head,parent,false);        return new headHolder(vIEw);      case BODY_TYPE:        vIEw = inflater.inflate(R.layout.item_body,false);        return new BodyHolder(vIEw);      case FOOT_TYPE:        vIEw = inflater.inflate(R.layout.item_foot,false);        return new FootHolder(vIEw);    }    return null;  }  @OverrIDe  public voID onBindVIEwHolder(final RecyclerVIEw.VIEwHolder holder,final int position) {// ★ 3. 根据holder是哪一个VIEwHolder的实例,来确定 *** 作哪一个布局的UI    if (holder instanceof headHolder) {      headHolder headHolder = (headHolder) holder;      headHolder.mTvhead.setText(mheadList.get(position) + "");      headHolder.mTvhead.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {          addData();        }      });    }    if (holder instanceof BodyHolder) {      final BodyHolder bodyHolder = (BodyHolder) holder;      bodyHolder.mTvBody.setText(mBodyList.get(position - getheadCount()) + "");      bodyHolder.mTvBody.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {          removeData(bodyHolder.getAdapterposition());        }      });    }    if (holder instanceof FootHolder) {      final FootHolder footHolder = (FootHolder) holder;      footHolder.mTvFoot.setText(mFootList.get(position - getheadCount() - getbodyCount()) + "");      footHolder.mTvFoot.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {          Toast.makeText(context,footHolder.getAdapterposition() + "",Toast.LENGTH_SHORT).show();        }      });    }  }  @OverrIDe  public int getItemCount() {    return getheadCount() + getbodyCount() + getFootCount();  }  private int getFootCount() {    return mFootList.size();  }  private int getbodyCount() {    return mBodyList.size();  }  private int getheadCount() {    return mheadList.size();  }  public voID addData(int position) {    mBodyList.add(position,"Insert One");//    ★★★★ 使用notifyItemInserted去更新数据,否则没有动画效果    notifyItemInserted(position);  }  /**   * 增加bodyList条目   */  public voID addData() {    mBodyList.add("1");    mBodyList.add("2");    mBodyList.add("3");//    ★★★★ 使用notifyItemInserted去更新数据,否则没有动画效果    notifyItemRangeInserted(0,3);  }  /**   * 删除bodyList条目   */  public voID removeData(int position) {//    经测试疯狂点击删除条目,会产生负数,导致越界异常    if (!(position - getheadCount() < 0)) {//    ★★★★ 防止角标越界异常,要减去头布局数量,总之去数数吧      mBodyList.remove(position - getheadCount());      notifyItemRemoved(position);    }  }  /**   * 头布局Holder   */  static class headHolder extends RecyclerVIEw.VIEwHolder {    private TextVIEw mTvhead;    public headHolder(VIEw itemVIEw) {      super(itemVIEw);      mTvhead = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_head);    }  }  /**   * 身体布局Holder   */  static class BodyHolder extends RecyclerVIEw.VIEwHolder {    private TextVIEw mTvBody;    public BodyHolder(VIEw itemVIEw) {      super(itemVIEw);      mTvBody = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_body);    }  }  /**   * 脚布局Holder   */  static class FootHolder extends RecyclerVIEw.VIEwHolder {    private TextVIEw mTvFoot;    public FootHolder(VIEw itemVIEw) {      super(itemVIEw);      mTvFoot = (TextVIEw) itemVIEw.findVIEwByID(R.ID.tv_foot);    }  }}

item_body

item_head和item_foot类似就不粘贴了

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"       androID:ID="@+ID/ll"       androID:layout_wIDth="match_parent"       androID:layout_height="40dp"       androID:layout_marginRight="11px"       androID:layout_marginBottom="11px"       androID:background="@color/colorPrimary"       androID:orIEntation="vertical">  <TextVIEw    androID:ID="@+ID/tv_body"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:gravity="center"    androID:textSize="17sp"    androID:layout_gravity="center"    androID:text="Hello World!"/></linearLayout>

以上所述是小编给大家介绍的AndroID 中RecyclerVIEw多种item布局的写法(头布局+脚布局),希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

总结

以上是内存溢出为你收集整理的Android 中RecyclerView多种item布局的写法(头布局+脚布局)全部内容,希望文章能够帮你解决Android 中RecyclerView多种item布局的写法(头布局+脚布局)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147300.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存