由于状态改变,如何更新我的视图?

时间:2019-07-11 08:01:20

标签: java android

我是原生Android开发的新手,我不知道如何响应状态更改来更新自己的ui。

在这种情况下,我希望我的View返回一个Is loading == true的TextView,然后当IsLoading设置为false时,我的视图更新返回view

这是我的代码:

public class FlutterPdfView implements PlatformView, MethodCallHandler {
    private PDFView view;
    private String path;
    private File pdf;
    private boolean isLoading;
    private final MethodChannel methodChannel;
    private OnRenderListener onRenderListener;
    private TextView textView;
    private PDFView.OnLayoutChangeListener onLayoutChangeListener;



    FlutterPdfView(final Context context, int id, Object args,  BinaryMessenger messenger ) {
        try {
            methodChannel = new MethodChannel(messenger, "\"plugins.smartwork.flutter_pdfview/pdfview_" + id);
            isLoading = true;
            textView = new TextView(context);
            onRenderListener = new OnRenderListener() {
                @Override
                public void onInitiallyRendered(int nbPages) {
                    isLoading = false;
                }
            };
            view = new PDFView(context, null);
            path = (String) args;
            pdf = new File(path);
            onLayoutChangeListener = new PDFView.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    recycleView();
                }
            };
        }finally {
            initView();
        }
    }



    @Override
    public View getView()  {
        if(isLoading == true) {
            return textView;
        } else {
            return view;
        }
    }


    private void recycleView() {
        view.recycle();
        view.fromFile(pdf).defaultPage(0).load();
    }

    private void initView() {
            view.addOnLayoutChangeListener(onLayoutChangeListener);
            view.fromFile(pdf).defaultPage(0).onRender(onRenderListener).load();
        }

}

是的,它很杂乱,但我是初学者^^

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

设置isLoading后,调用false进行invalidate();它使视图重绘。

 @Override
        public View getView()  {
            if(isLoading == true) {
                return textView;
            } else {
                return view;
                invalidate();        
    }
}