In the development process, there is often a need to refresh the list without data showing no data temporarily

RecyclerView has no data yet click to reload (2)

You might want to use a RelativeLayout to create a ListView and a UI with a RelativeLayout to create a listView and a UI with a RelativeLayout to create a listView and a UI with a RelativeLayout to create a UI with a RelativeLayout to create a UI with a RelativeLayout Directly inherit RecyclerView rewrite onDraw method according to the effect of UI to deal with three things the first load picture drawing and text drawing the first step to draw a picture is the prerequisite to judge the number of getChildCount only meet 0 when the data does not represent Before reloading the style Canvas.drawBitmap method to prepare an image

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

Figure out the coordinates of the plot

Notice that I have the image centered here and you need to get the whole width and height of RecyclerView minus the width and height of the image divided by 2 to get the center point

The first step is to draw the second text, canvas. drawText, below the image

public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
Copy the code

So I can show you what it looks like

public class MyRecyclerView extends RecyclerView { Shuaxin shuaxin; // Write your own interface Boolean complete; Bitmap bitmap; Paint paint; public MyRecyclerView(@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 voidsetShuaxin(Shuaxin shuaxin) {
        this.shuaxin = shuaxin;
    }

    public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; } // Activity tells recycleView that you can use the recycleView data to create your own public voidcompleteLoad(){
        complete =true; invalidate(); } @override public void onDraw(Canvas c) {log.d (Canvas c);"RecyclerView"."onDraw");
        if(getChildCount()==0 && complete){
            c.drawColor(Color.GREEN);
            int x = (getWidth()-bitmap.getWidth()) /2;
            int y = (getHeight()-bitmap.getHeight()) /2;
            c.drawBitmap(bitmap,x,y,paint);
            c.drawText("No data yet. Click reload.",getWidth()/2,
                    (getHeight()/2)+bitmap.getHeight(),paint);


        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()){

            case    MotionEvent.ACTION_UP :
                if(getChildCount()==0&&this.shuaxin! =null){ shuaxin.shuaxin();return true; }}returnsuper.dispatchTouchEvent(ev); }}Copy the code

Use the activity

That’s it