【问题标题】:Cannot hide ProgressBar programmatically in Android无法在 Android 中以编程方式隐藏 ProgressBar
【发布时间】:2015-10-09 03:15:40
【问题描述】:

在将数据加载到ListView 后,我试图隐藏ProgressBar。但是,ProgressBar 似乎不受视图更改的影响。我没有收到任何错误(activityIndicator 似乎不是NULL)。我的处理方式有问题吗?

<ProgressBar
    android:id="@+id/activityIndicator"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:indeterminateOnly="true"
    android:keepScreenOn="true"
    android:layout_gravity="center" />

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    theInflater = inflater;
    theContainer = container;

    currentView = inflater.inflate(R.layout.fragment_server_list, container, false);
    activityIndicator = (ProgressBar) currentView.findViewById(R.id.activityIndicator);

    // check if you are connected or not
    if(isConnected()){
        new Thread(new Runnable() {
            public void run() {
                HashMap<String, String> data = new HashMap<String, String>();
                data.put("action", "getServerList");
                data.put("phone_id", "857a95839d7d6c270e97ff6a3a64008ccf96074ebffa6e8e82db932a6c635644");
                data.put("username", "atomic");
                data.put("password", "Pa55word1");
                AsyncHttpPost asyncHttpPost = new AsyncHttpPost(data);

                try {
                    String jsonString = asyncHttpPost.execute("http://website.com/app-files/API.php").get();

                    Log.i("JSON Result", jsonString);

                    try {
                        JSONObject json = new JSONObject(jsonString);

                        if (json.optString("result").equalsIgnoreCase("true")) {
                            JSONArray articles = json.getJSONArray("data");

                            for (int i = 0; i < articles.length(); i++) {
                                JSONObject row = articles.getJSONObject(i);
                                myServers.add(new Server(Integer.parseInt(row.getString("id")), 
                                        row.getString("host"), 
                                        0, //Integer.parseInt(row.getString("port")), 
                                        "", //row.getString("hostname"), 
                                        "", //row.getString("ipAddress"), 
                                        "", //row.getString("checkDate"), 
                                        "", //row.getString("serverInfo"), 
                                        "", //row.getString("lastModified"), 
                                        "", //row.getString("contentType"), 
                                        "", //row.getString("city"), 
                                        "", //row.getString("state"), 
                                        "", //row.getString("zip"), 
                                        "", //row.getString("country"), 
                                        "", //row.getString("latitude"), 
                                        "", //row.getString("longitude"), 
                                        "", //row.getString("timeZone"), 
                                        0, //Integer.parseInt(row.getString("consecutiveFails")), 
                                        Float.parseFloat(row.getString("latency")), 
                                        Float.parseFloat(row.getString("latency")) > 300 ? 
                                                R.drawable.btn_yellow : (Float.parseFloat(row.getString("latency")) > 1 ? 
                                                        R.drawable.btn_green : R.drawable.btn_red)));

                                //Log.i("Current Server's Host", row.getString("host"));
                            }

                            populateListView();

                            registerClickCallback();

                            currentView.post(new Runnable() {
                                public void run() {
                                    activityIndicator.setVisibility(View.INVISIBLE);
                                }
                            });
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

        activityIndicator.setVisibility(View.GONE);
    }else{
        Log.i("Connection", "You are NOT connected");
    }

    return currentView;
}

编辑:似乎如果我离开选项卡并返回此视图的选项卡,进度条就消失了。所以这就好像它正在移除进度条,但视图没有更新以显示它已被审核。

【问题讨论】:

  • 您的应用程序是否关闭?
  • 您在启动线程后立即隐藏 ProgressBar,也在线程内部。您确定这种行为吗?
  • @Rod_Algonquin 不,应用程序不会崩溃。它很好地显示了我的列表视图,我可以滚动和单击。它唯一没有做的就是隐藏进度条。
  • @AbhishekShukla 是的,我确定。我只是想让它隐藏起来。之后,我只能在需要时让它隐藏起来。但不幸的是,它根本没有隐藏。
  • 在 xml 中放置进度条是个坏主意。最好动态创建一个。

标签: android android-progressbar


【解决方案1】:

好吧,我认为这很荒谬,但这是我修复它的方法。

在我的ProgressBar 的xml 中,我添加了android:visibility="gone" 以默认隐藏它。然后,在我的代码中,我首先告诉它在尝试获取服务器列表之前显示 (View.VISIBLE),然后我告诉它在完成后隐藏 (View.GONE)。这有效(我可以在加载数据时看到进度指示器,然后它就消失了)。所以我想我无法让它隐藏在代码中,因为代码并不是强迫它一开始就可见的原因......这对我来说似乎是一个错误。

【讨论】:

  • 哇,在我为此烧了 2 小时后,你才救了我。只是愚蠢。谢谢!
猜你喜欢
  • 2019-08-02
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 2011-12-03
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多