开发者

Android - How do I set View.GONE outside the UI thread?

I want to make a view disappear (to be gone) when the user pushes a button.

I can make it inside the onCreate() method (main UI thread) by doing:

fi开发者_Python百科ndViewById(R.id.llLoadingGallery).setVisibility(View.GONE);

However, I want to be able to do the same thing inside another thread (out of the main UI thread). I tried to put the above live in my thread and it didn't work.

Thank you in advance!

## EDIT ####

To make myself more clear, I want to do something like this:

        @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.teste_aba_3);

    botao_tab_musica.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            ...
        findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
        }
    });

}

However, that DOESN'T work! How can I fix this?


theres a neat method called runOnUiThread

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
            }
        });

edit: with your code

botao_tab_musica.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick(View v) {

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
            }
        });
}

});


I'd suggest either using View.post(Runnable), View.postDelayed(Runnable, long), a Handler, or an AsyncTask to do this for you.

There are some good examples on how to post to the UI thread in these scenarios:

http://developer.android.com/resources/articles/painless-threading.html


I think one possibility is to pass a reference to your activity to that other thread so that your thread can access the findViewById method.


try this

findViewById(R.id.llLoadingGallery).post(new Runnable()
        {

            @Override
            public void run()
            {
               findViewById(R.id.llLoadingGallery).setVisibility(View.GONE);
            }
        });

or create AsyncTask

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜