一项活动,两种观点。如何和我在哪里吹第二个看法? [英] One activity, two views. How and where do I inflate the second view?

查看:164
本文介绍了一项活动,两种观点。如何和我在哪里吹第二个看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,可以显示两个不同的布局。无论是布局是pre定义(XML)。基本上,如果条件满足,则布局的应显示。如果条件不满足,则布局乙方应显示出来。

布局A是一个简单的线性布局 - 这是我的主要形式,可以这么说。
布局B是一个简单的相对布局 - 这是一个占位符,直到一些数据可以下载。一旦数据被下载(和发送通知),那么我想删除布局B和显示版面A。

我试过调用无效()上的 onResume()方法版面B方法我活动但是,这并不正常工作。

我不知道我应该采取什么办法,(1)其中,为正确切换布局,和(2)我应该如何去显示它。我假设我需要充气版面A时,我的条件得到满足,但我不是100%肯定这一点。

编辑:
剪断我的的onCreate()方法:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.layoutA); //会发生的99%的时间
    ...
    如果(!dbHelper.tableIsPopulated()){
        的setContentView(R.layout.layoutB); //显示占位符BC数据不存在
        的getData();
    }
}


解决方案

除非你有一个理由不使用背景,我建议使用的AsyncTask ,并用进度条。直到你得到的数据会比使用虚拟布局只是作为一个占位符,成本更低。你说不会,但时间的1%被使用。似乎是一种浪费在我看来,

 公共类TalkToServer扩展的AsyncTask<字符串,字符串,字符串> {
        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
        }        @覆盖
        保护无效onProgressUpdate(字符串值...){
            super.onProgressUpdate(值);        }        @覆盖
        保护字符串doInBackground(字符串... PARAMS){
        //做你的工作在这里
            返回的东西;
        }        @覆盖
        保护无效onPostExecute(字符串结果){
            super.onPostExecute(结果);
               //做数据的东西在这里,它显示或发送至mainactivity
    }

您显然知道的AsyncTask 不过的这里是文档以备将来游客的,它有使用的一个例子 ProgressDialog

注意

既然不是你只需要你的上下文传递给你的的AsyncTask 如果你要显示你的 ProgressDialog 从那里。

I have an activity that can show two different layouts. Both of the layouts are pre-defined (XML). Basically, if a condition is met, then layout A should be displayed. If the condition fails, then layout B should be displayed.

Layout A is a simplistic Linear Layout - it's my main "form", so to speak. Layout B is a simplistic Relative Layout - it's a placeholder until some data can be downloaded. Once the data is downloaded (and a notification is sent), then I want to remove Layout B and display Layout A.

I've tried calling the invalidate() method on Layout B in the onResume() method of my Activity but that doesn't work.

I'm not sure what approach I should take, in (1) where to "correctly" switch the layouts, and (2) how I should go about displaying it. I'm assuming I need to inflate Layout A when my condition is met, but I'm not 100% sure about that.

Edit: Snipped of my onCreate() method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutA); // will happen 99% of the time
    ...
    if (!dbHelper.tableIsPopulated()) {
        setContentView(R.layout.layoutB); // show placeholder bc no data exists
        getData();
    }
}

解决方案

Unless you have a reason to not use a background Thread, I suggest using an AsyncTask and using a progress bar. It will be less costly than using a dummy Layout just as a placeholder until you get the data. And you said it won't be used but 1% of the time. Seems like a waste in my opinion

 public class TalkToServer extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);

        }

        @Override
        protected String doInBackground(String... params) {
        //do your work here
            return something;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
               // do something with data here-display it or send to mainactivity
    }

You apparently know about AsyncTask but Here are the Docs for future visitors and it has an example of using the ProgressDialog.

Note

Since it isn't an inner class you will just need to pass your Context to the constructor of your AsyncTask if you are going to show your ProgressDialog from there.

这篇关于一项活动,两种观点。如何和我在哪里吹第二个看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆