澳门新蒲京娱乐

新蒲京官方下载 2
美团渠道打包简单粗暴快速版,批量自动重签名脚本
新蒲京官方下载 1
图片大小不可控问题,迷上旅游

【干货】Android真正的【新蒲京官方下载】“万能”Adapter

近年看来数不完封装Adapter的库,不过都认为相当不足方便。前几天给大家介绍叁个当真的“万能”艾达pter,精髓就在于三个Adapter同临时间适用于RecyclerView、ListView、GridView、Spinner等。

RecylerView介绍

RecylerView是support-v7包中的新组件,是二个精锐的滑行组件,与杰出的ListView比较,同样具有item回收复用的机能,那一点从它的名字recylerview即回收view也能够见见。官方对此它的牵线则是:RecyclerView
是 ListView
的进级版本,越发进取和灵活。RecyclerView通过设置LayoutManager,ItemDecoration,ItemAnimator达成您想要的机能。

  • 运用LayoutManager来显著每四个item的排列格局。
  • 使用ItemDecoration本人绘制分水线,更加灵敏
  • 选择ItemAnimator为增添或删除一行设置动画效果。

新蒲京官方下载 1源码结构图.png

注意

新建完项目,供给在app/build.gradle增添RecylerView依赖,不然找不到RecyclerView类

compile 'com.android.support:recyclerview-v7:23.1.0'
  • 减去大气代码!降低大气代码!减弱大气代码!
  • 支持header和footer。
  • 支撑RecyclerView的item点击事件。
  • 隐蔽ViewHolder相关的富有代码。
  • 二个SuperAdapter同不经常间补助BaseAdapterRecyclerView.Adapter
  • 封装Adapter数据源变动操作。
  • item加载动画。

RecylerView简单的Demo

大家来看activity代码,跟ListView写法大概,只是那边多设置了布局管理器。

public class LinearLayoutActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerViewAdapter adapter;
    private List<String> datas;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_main);

        initData();

        recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局管理器
        recyclerView.addItemDecoration(new DividerItemDecoration(this));
        recyclerView.setAdapter(adapter=new RecyclerViewAdapter(this,datas));
    }

    private void initData(){
        datas=new ArrayList<>();
        for(int i=0;i<100;i++){
            datas.add("item:"+i);
        }
    }
}

activity对应的布局文件:recycler_main.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="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

adapter相对ListView来讲变化十分大的。把ViewHolder逻辑封装起来了,代码相对简单一些。

  • 亟需持续RecyclerView.Adapter,重写多个情势
  • MyViewHolder要求一连RecyclerView.ViewHolder

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
    private List<String> datas;
    private LayoutInflater inflater;

    public  RecyclerViewAdapter(Context context,List<String> datas){
        inflater=LayoutInflater.from(context);
        this.datas=datas;
    }

    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=inflater.inflate(R.layout.recycler_item,null);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, int position) {
        holder.textview.setText(datas.get(position));
    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView textview;

        public MyViewHolder(View itemView) {
            super(itemView);
            textview= (TextView) itemView.findViewById(R.id.textview);
        }
    }
}

咱俩来会见效果图:

新蒲京官方下载 2

RecylerView基本使用

如果是个轻易布局的Adapter,能够简写为如下示例代码:

RecyclerView扩展分隔线

RecyclerView是未曾android:divider跟android:dividerHeight属性的,假使大家供给分水线,就只能协和下手去贯彻了。

  • 急需连续ItemDecoration类,达成onDraw跟getItemOffsets方法。
  • 调用RecyclerView的addItemDecoration方法。

咱俩先写一个DividerItemDecoration类,承接RecyclerView.ItemDecoration,在getItemOffsets留出item之间的区间,然后就能调用onDraw方法绘制(onDraw的绘图优先于每一行的绘图)

public class DividerItemDecoration extends RecyclerView.ItemDecoration{
    /*
    * RecyclerView的布局方向,默认先赋值 为纵向布局
    * RecyclerView 布局可横向,也可纵向
    * 横向和纵向对应的分割线画法不一样
    * */
    private int mOrientation = LinearLayoutManager.VERTICAL;

    private int mItemSize = 1;//item之间分割线的size,默认为1

    private Paint mPaint;//绘制item分割线的画笔,和设置其属性

    public DividerItemDecoration(Context context) {
        this(context,LinearLayoutManager.VERTICAL,R.color.colorAccent);
    }

    public DividerItemDecoration(Context context, int orientation) {
        this(context,orientation, R.color.colorAccent);
    }

    public DividerItemDecoration(Context context, int orientation, int dividerColor){
        this(context,orientation,dividerColor,1);
    }

    /**
     * @param context
     * @param orientation 绘制方向
     * @param dividerColor 分割线颜色 颜色资源id
     * @param mItemSize 分割线宽度 传入dp值就行
     */
    public DividerItemDecoration(Context context, int orientation, int dividerColor, int mItemSize){
        this.mOrientation = orientation;
        if(orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL){
            throw new IllegalArgumentException("请传入正确的参数") ;
        }
        //把dp值换算成px
        this.mItemSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,mItemSize,context.getResources().getDisplayMetrics());
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mPaint.setColor(context.getResources().getColor(dividerColor));
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if(mOrientation == LinearLayoutManager.VERTICAL){
            drawVertical(c,parent) ;
        }else {
            drawHorizontal(c,parent) ;
        }
    }

    /**
     * 绘制纵向 item 分割线
     * @param canvas
     * @param parent
     */
    private void drawVertical(Canvas canvas,RecyclerView parent){
        final int left = parent.getPaddingLeft() ;
        final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        final int childSize = parent.getChildCount() ;
        for(int i = 0 ; i < childSize ; i ++){
            final View child = parent.getChildAt( i ) ;
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + layoutParams.bottomMargin ;
            final int bottom = top + mItemSize ;
            canvas.drawRect(left,top,right,bottom,mPaint);
        }
    }

    /**
     * 绘制横向 item 分割线
     * @param canvas
     * @param parent
     */
    private void drawHorizontal(Canvas canvas,RecyclerView parent){
        final int top = parent.getPaddingTop() ;
        final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom() ;
        final int childSize = parent.getChildCount() ;
        for(int i = 0 ; i < childSize ; i ++){
            final View child = parent.getChildAt( i ) ;
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + layoutParams.rightMargin ;
            final int right = left + mItemSize ;
            canvas.drawRect(left,top,right,bottom,mPaint);
        }
    }

    /**
     * 设置item分割线的size
     * @param outRect
     * @param view
     * @param parent
     * @param state
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if(mOrientation == LinearLayoutManager.VERTICAL){
            outRect.set(0,0,0,mItemSize);//垂直排列 底部偏移
        }else {
            outRect.set(0,0,mItemSize,0);//水平排列 右边偏移
        }
    }
}

并不是遗忘调用addItemDecoration方法哦

recyclerView.addItemDecoration(new DividerItemDecoration(this));//添加分割线

再度运维,效果图:

新蒲京官方下载 3

丰盛分水岭

世家读到这里断定会有一个疑问,那货比ListView麻烦多了啊,不过google官方为什么要说是ListView的进级版啊?接下去初叶放大招。。。

public class SingleAdapter extends SuperAdapter<MockModel> { public SingleAdapter(Context context, List<MockModel> list, int layoutResId) { super(context, list, layoutResId); } @Override public void onBind(SuperViewHolder holder, int viewType, int position, MockModel item) { holder.setText(R.id.tv_name, item); }}

GridLayoutManager

在RecyclerView中落实不一致的列表,只必要切换分化的LayoutManager就可以。RecyclerView.LayoutManager跟RecyclerView.ItemDecoration同样,都是RecyclerView静态抽象内部类,可是LayoutManager有四个官方写好的兑现类。

  • LinearLayoutManager 线性布局管理器 跟ListView功用相似
  • GridLayoutManager 网格布局管理器 跟GridView功能相似
  • StaggeredGridLayoutManager 瀑布流布局管理器

正好我们用的是LinearLayoutManager,今后大家切换来GridLayoutManager,看到下边那句代码,有未有痛感分秒钟切换不一样列表呈现。

recyclerView.setLayoutManager(new GridLayoutManager(this,2));

一旦要出示多列也许要纵向显示就new差别的构造方法,以下代码纵向展现4列。当前一旦您还索要反方向显示,把false改成true就足以。

recyclerView.setLayoutManager(new GridLayoutManager(this,4,GridLayoutManager.HORIZONTAL,false));

因为用的是网格布局,所以呢绘制分界线的代码供给再行修改一下。网格布局一行能够有多列,何况最后一列跟最终一行没有要求绘制,所以我们得重复创立二个类。
DividerGridItemDecoration.java

public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {
    /*
    * RecyclerView的布局方向,默认先赋值 为纵向布局
    * RecyclerView 布局可横向,也可纵向
    * 横向和纵向对应的分割线画法不一样
    * */
    private int mOrientation = LinearLayoutManager.VERTICAL;

    private int mItemSize = 1;//item之间分割线的size,默认为1

    private Paint mPaint;//绘制item分割线的画笔,和设置其属性

    public DividerGridItemDecoration(Context context) {
        this(context,LinearLayoutManager.VERTICAL,R.color.colorAccent);
    }

    public DividerGridItemDecoration(Context context, int orientation) {
        this(context,orientation, R.color.colorAccent);
    }

    public DividerGridItemDecoration(Context context, int orientation, int dividerColor){
        this(context,orientation,dividerColor,1);
    }

    /**
     * @param context
     * @param orientation 绘制方向
     * @param dividerColor 分割线颜色 颜色资源id
     * @param mItemSize 分割线宽度 传入dp值就行
     */
    public DividerGridItemDecoration(Context context, int orientation, int dividerColor, int mItemSize){
        this.mOrientation = orientation;
        if(orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL){
            throw new IllegalArgumentException("请传入正确的参数") ;
        }
        //把dp值换算成px
        this.mItemSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,mItemSize,context.getResources().getDisplayMetrics());
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mPaint.setColor(context.getResources().getColor(dividerColor));
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawHorizontal(c, parent);
        drawVertical(c, parent);
    }

    private int getSpanCount(RecyclerView parent) {
        // 列数
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return spanCount;
    }

    public void drawHorizontal(Canvas canvas, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin + mItemSize;
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mItemSize;
            canvas.drawRect(left,top,right,bottom,mPaint);
        }
    }

    public void drawVertical(Canvas canvas, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mItemSize;
            canvas.drawRect(left,top,right,bottom,mPaint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition,RecyclerView parent) {
        int spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();

        if (isLastRow(parent, itemPosition, spanCount, childCount)){//如果是最后一行,不需要绘制底部
            outRect.set(0, 0, mItemSize, 0);
        } else if (isLastColum(parent, itemPosition, spanCount, childCount)){// 如果是最后一列,不需要绘制右边
            outRect.set(0, 0, 0, mItemSize);
        } else {
            outRect.set(0, 0, mItemSize,mItemSize);
        }
    }

    private boolean isLastColum(RecyclerView parent, int pos, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if ((pos + 1) % spanCount == 0){// 如果是最后一列,则不需要绘制右边
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                if ((pos + 1) % spanCount == 0){// 如果是最后一列,则不需要绘制右边
                    return true;
                }
            } else {
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount)// 如果是最后一列,则不需要绘制右边
                    return true;
            }
        }
        return false;
    }

    private boolean isLastRow(RecyclerView parent, int pos, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount)//最后一行
                return true;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL){//纵向
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount)//最后一行
                    return true;
            } else{ //横向
                if ((pos + 1) % spanCount == 0) {//是最后一行
                    return true;
                }
            }
        }
        return false;
    }
}

写了那八个画分界线的类,主流的布局:线性列表跟网格列表都能彰显了。。。赶紧运营代码看看结果:

新蒲京官方下载 4

网格列表

能够看到除了构造方法,只须要重写三个onBind()办法就够了。然后在Activity(or
Fragment)中调用:

StaggeredGridLayoutManager

actviity中期维修改下布局处理器,大家应该感觉很熟识了啊~~~

recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

瀑布流列表一般列的冲天是不等同的,为了参照他事他说加以考察差异的宽高,数据源作者把String类型改成了对象.然后最早化的时候随机了三个中度.

public class ItemData {
    private String content;//item内容
    private int height;//item高度

    public ItemData() {
    }

    public ItemData(String content, int height) {
        this.content = content;
        this.height = height;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

瀑布流列表没有拉长分界线,给item布局设置了android:padding属性。recycler_staggered_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dp"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textview"
        android:background="@color/colorAccent"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="122"
        android:textSize="20sp"/>
</FrameLayout>

末段大家在适配器的onBindViewHolder方法中给itemd中的TextView设置三个惊人

@Override
public void onBindViewHolder(StaggeredGridAdapter.MyViewHolder holder, int position) {
    ItemData itemData=datas.get(position);
    holder.textview.setText(itemData.getContent());
    //手动更改高度,不同位置的高度有所不同
    holder.textview.setHeight(itemData.getHeight());
}

是否以为so easy,赶紧运营看看效果:

新蒲京官方下载 5

瀑布流效果

mSingleAdapter = new RecyclerSingleAdapter(context, list, R.layout.your_item); recyclerView.setAdapter(mSingleAdapter);

添加header跟footer

RecyclerView增多头顶跟尾巴部分是一贯不对号入座的api的,可是大家非常多的急需都会用到,于是只可以和煦想方法落到实处了。大家得以透过适配器的getItemViewType方法来贯彻这几个职能。

修改后的适配器代码:RecyclerHeadFootViewAdapter.java

public class RecyclerHeadFootViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<String> datas;
    private LayoutInflater inflater;

    public static final int TYPE_HEADER=1;//header类型
    public static final int TYPE_FOOTER=2;//footer类型
    private View header=null;//头View
    private View footer=null;//脚View

    public  RecyclerHeadFootViewAdapter(Context context, List<String> datas){
        inflater=LayoutInflater.from(context);
        this.datas=datas;
    }

    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPE_HEADER){
            return new RecyclerView.ViewHolder(header){};
        }else if(viewType==TYPE_FOOTER){
            return new RecyclerView.ViewHolder(footer){};
        }
        View itemView=inflater.inflate(R.layout.recycler_item,null);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position){
        if(getItemViewType(position)==TYPE_HEADER||getItemViewType(position)==TYPE_FOOTER){
            return;
        }
        MyViewHolder myholder= (MyViewHolder) holder;
        myholder.textview.setText(datas.get(getRealPosition(position)));
    }

    //如果有头部 position的位置是从1开始的  所以需要-1
    public int getRealPosition(int position){
        return header==null?position:position-1;
    }

    //数据源的数量
    @Override
    public int getItemCount() {
        if(header == null && footer == null){//没有head跟foot
            return datas.size();
        }else if(header == null && footer != null){//head为空&&foot不为空
            return datas.size() + 1;
        }else if (header != null && footer == null){//head不为空&&foot为空
            return datas.size() + 1;
        }else {
            return datas.size() + 2;//head不为空&&foot不为空
        }
    }

    @Override
    public int getItemViewType(int position){
        //如果头布局不为空&&位置是第一个那就是head类型
        if(header!=null&&position==0){
            return TYPE_HEADER;
        }else if(footer!=null&&position==getItemCount()-1){//如果footer不为空&&最后一个
            return TYPE_FOOTER;
        }
        return super.getItemViewType(position);
    }

    public void setHeader(View header) {
        this.header = header;

        notifyItemInserted(0);//在位置0插入一条数据,然后刷新
    }

    public void setFooter(View footer) {
        this.footer = footer;
        notifyItemInserted(datas.size()-1);//在尾部插入一条数据,然后刷新
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView textview;

        public MyViewHolder(View itemView) {
            super(itemView);
            textview= (TextView) itemView.findViewById(R.id.textview);
        }
    }
}
  • getItemCount

    有header跟footer的时候要求在源数据长度基础上开展充实。

  • getItemViewType

    通过getItemViewType剖断不一致的连串

  • onCreateViewHolder

    经过不一致的类别成立item的View

  • onBindViewHolder

举个例子是header跟footer类型是无需绑定数据的,header跟footer的View一般在actvity中创建,无需那边做拍卖,所以这两种档案的次序大家就不往下举行,假如有头布局,position==0的职位被header占用了,但是我们的数据源也正是相会的下标是从0初始的,所以这里需求-1。

  • setHeader

    安装头布局,在第一行插入一条数据,然后刷新。注意那些方式调用后会有插入的动画片,这些动画能够应用暗中认可的,也得以团结定义

  • setFooter

    设置背后部遍布局,在后面部分插入一条数据,然后刷新。

增多header跟footer的秘技终于封装好了,在activity中只须求两行代码就能够增添header,跟ListView调用addHeader方法同样轻易,又足以happy的娱乐了。这里需求留意的是大家开首化View的时候,inflate方法供给五个参数。

  • resource 资源id
  • root 父View
  • attachToRoot true:重回父View false:重临能源id生成的View

//添加header
View header=LayoutInflater.from(this).inflate(R.layout.recycler_header,recyclerView,false);
adapter.setHeader(header);

//添加footer
View footer=LayoutInflater.from(this).inflate(R.layout.recycler_footer,recyclerView,false);
adapter.setFooter(footer);

recycler_header跟recycler_footer布局文件小编就不贴出来了,就一个TextView,我们直接看功能图:

新蒲京官方下载 6

RecyclerView增添头跟尾

其中SuperViewHolder为通用的holder封装类,提供了一各类的方便人民群众格局(ChainSetter),并且可以链式调用,如:

item点击事件&&增添或删除带动画效率

当我们调用RecyclerView的setOnItemClickListener方法的时候,发掘依然未有,用了RecyclerView你要习于旧贯怎么事物都协调包裹。。。

率先大家从adapter开刀,内部写贰个接口,一个实例变量,提供多个国有艺术,设置监听。

private RecyclerViewItemClick recyclerViewItemClick;

public void setRecyclerViewItemClick(RecyclerViewItemClick recyclerViewItemClick) {
    this.recyclerViewItemClick = recyclerViewItemClick;
}

public interface RecyclerViewItemClick{
    /**
     * item点击
     * @param realPosition 数据源position
     * @param position view position
     */
    void onItemClick(int realPosition,int position);
}

在onBindViewHolder方法中给item监听点击事件

if(recyclerViewItemClick!=null) {
    myholder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            recyclerViewItemClick.onItemClick(getRealPosition(position),position);
        }
    });
}

在activity的onCreate方法中张开监听,顺便设置item扩张删除动画。作者用的是sdk自带的暗中认可动画。

adapter.setRecyclerViewItemClick(recyclerViewItemClick);
recyclerView.setItemAnimator(new DefaultItemAnimator());

private RecyclerHeadFootViewAdapter.RecyclerViewItemClick recyclerViewItemClick=new RecyclerHeadFootViewAdapter.RecyclerViewItemClick() {
    @Override
    public void onItemClick(int realPosition, int position) {
        Log.i("ansen","删除数据:"+realPosition+" view位置:"+position);
        Log.i("ansen","当前位置:"+position+" 更新item数量:"+(adapter.getItemCount()-position-1));

        datas.remove(realPosition);//删除数据源
        adapter.notifyItemRemoved(position);//item移除动画
        //更新position至adapter.getItemCount()-1的数据
        adapter.notifyItemRangeChanged(position,adapter.getItemCount()-position-1);
    }
};

新蒲京官方下载 7ChainSetter部分方法.png还足以应用getView(int viewId)来规定一个item的view:

源码下载

RecyclerView

@Overridepublic void onBind(SuperViewHolder holder, int viewType, int position, String item) { holder.setText(R.id.tv_name, item); ImageView img = holder.getView(R.id.iv_portrait); img.setImageResource;}

自然也支撑多item布局,供给提供一个IMulItemViewType<T>接口:

public class MultipleAdapter extends SuperAdapter<MockModel> { public MultipleAdapter(Context context, List<MockModel> list, IMulItemViewType<MockModel> multiItemViewType) { super(context, list, multiItemViewType); } @Override public void onBind(SuperViewHolder holder, int viewType, int position, MockModel item) { switch  { case 0: holder.setText(R.id.tv_name, item.getName; break; case 1: holder.setText(R.id.tv_name, item.getName; holder.setImageResource(R.id.iv_portrait, R.mipmap.ic_launcher); holder.setText(R.id.tv_age, String.valueOf(item.getAge; break; } }}

下一场调用(注意构造方法的参数与单布局的界别):

相关文章

No Comments, Be The First!
近期评论
    功能
    网站地图xml地图