Android: Writing Custom Views that Support The Message Queue

廉价感情. 提交于 2019-12-12 23:06:52

问题


How do you implement a custom view in Android so that it properly supports, or works with, the messaging queue ?

I'm trying to emulate the behavior of the built-in views so that I can properly / normally update a custom view with data within onCreate.

Currently, my custom view has ad-hoc set/update functions to put data in them. The problem with this is that my view's children views are not initialized until the first time onMeasure is called, which is after onCreate exits (which I think is how the built-ins do it).

Therefore, I want to know what the general strategy is (ie, what methods to override) to update a custom view from onCreate in such a way that the updates go into the message queue and reach the view after they are properly instantiated (just like the built-ins) ?

Thanks.


回答1:


Look at View.post():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView hello = ((TextView) findViewById(R.id.hello));
    hello.post(new Runnable() {
        @Override
        public void run() {
            hello.setText("Hello World!");
        }
    });
}


来源:https://stackoverflow.com/questions/12955899/android-writing-custom-views-that-support-the-message-queue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!