RecyclerView 暂无数据 点击重新加载 (二)

1,079 阅读2分钟

上回初步弄了下RecyclerView 暂无数据 点击重新加载 但那个效果不之适用于点击全屏 RecyclerView 暂无数据 点击重新加载 (一)

这回讲下局部刷新 实现下面这样的效果的大致思路 上篇文章实现的方式只是单纯的重写onDraw和dispatchTouchEvent方法 本次实现的思路图片和文字还是和上回一样在onDraw绘制 主要的处理在于局部刷新按钮 废话不多说 上个图 image.png ps.没啥图片整的随便搞了图将就下 第一步 绘制出 重新加载按钮 image.png 通过addview 把按钮绘制到屏幕中间 由于RecyclerView的item是属于RecyclerView.ViewHolder 直接view 不处理的话会报这样的错

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference

so 就要去第二个步骤了 重写 onMeasure onLayout方法 image.png onMeasure没啥好说的就是把childview绘制下宽高

onLayout的话就要注意那个if判断以及v.layout这俩个地方

super.onLayout(changed,l,t,r,b); 是处理item的绘制 也就是真实数据的item v.layout是单独处理addview添加的按钮 把那个报错信息处理掉 按钮不是 ViewHolder绘制出来 如果不加判断 会异常 而 真实数据不返回 super.onLayout则item不会绘制

最后一步onDraw 绘制图片和描述文字 image.png

到此就这么结束了 上完整代码

public class My2RecyclerView extends RecyclerView {
    Shuaxin shuaxin; //这个是自己瞎写的接口  
    boolean complete;
    Bitmap bitmap;
    Paint paint;
    public My2RecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setTextSize(DisplayUtils.sp2px(context,14));
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setColor(Color.BLACK);
    }

    public void setShuaxin(Shuaxin shuaxin) {
        this.shuaxin = shuaxin;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }


    public void noData(){
        if(getChildCount() == 0){
            LayoutParams params =
                    new RecyclerView.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            TextView textView =new  TextView(getContext());
            textView.setPadding(60,30,60,30);
            textView.setLayoutParams(params);
            textView.setText("重新加载");
            textView.setGravity(Gravity.CENTER);
            textView.setTag("加载");
            textView.setBackgroundResource(R.drawable.click);
            textView.setTextColor(Color.WHITE);
            textView.setOnClickListener(v -> {
                if(this.shuaxin!=null){
                    this.shuaxin.shuaxin();
                }
            });
            addView(textView);
        }
        requestLayout();
        //由于是addview 实现 需要刷新view的位置 故不采用 invalidate 回调onDraw方法
        //requestLayout 回调 onMeasure    onLayout 如果有视图改变 才会 onDraw
    }

    public void complete(){
        complete =true;
        bitmap = null;
        removeAllViews();
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        super.onMeasure(widthSpec, heightSpec);
        Log.d("RecyclerView","onMeasure---"+getChildCount());
        for (int i = 0; i < getChildCount(); i++) {
            View v = getChildAt(i);
            measureChild(v,widthSpec,heightSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.d("RecyclerView","onLayout---"+getChildCount());
        if (getChildCount()>0) {
            for (int i = 0; i < getChildCount(); i++) {
                View v = getChildAt(i);
                if(v.getTag()!=null &&v.getTag().equals("加载")) {
                    int left  =(getWidth()-v.getMeasuredWidth())/2;
                    int top  =(getHeight()-v.getMeasuredHeight())/2;
                    v.layout(left,top+bitmap.getHeight(),
                            v.getMeasuredWidth()+left,
                            v.getMeasuredHeight()+top+bitmap.getHeight());
                }
            }
        }
        if(complete){
            super.onLayout(changed,l,t,r,b);
        }
    }

    @Override
    public void onDraw(Canvas c) {
        Log.d("RecyclerView", "onDraw");
        if (getChildCount() > 0) {
            View v = getChildAt(0);
           if(v.getTag()!=null &&v.getTag().equals("加载")) {
               c.drawColor(Color.GREEN);
               int x = (getWidth() - bitmap.getWidth()) / 2 ;
               int y = (getHeight() - bitmap.getHeight()) / 2;
               c.drawBitmap(bitmap, x, y-200, paint);
                 c.drawText("暂无数据",getWidth()/2,
                                   (getHeight()/2)+bitmap.getHeight()-200,paint);
           }
        }
    }
}

activity使用 image.png

大概思路就是这样 到此结束