为什么我的android progressBar从不显示?

时间:2018-07-06 16:36:43

标签: android android-volley

我有一个recyclerView,它正在加载并显示我想要的项目,但是有一个延迟(当应用程序从网络获取xml新闻并对其进行解析时),并且我希望有一个progressBar来显示指示用户。进度条在那里,但从未显示。

这是我的代码:

    public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, SearchView.OnQueryTextListener {

    private ArrayList<NewsItem> newsItems;
    private ProgressBar updateProgressBar;
    private RecyclerView newsItemsRecyclerView;
    private NewsAdapter newsAdapter;
    private SearchView searchView;
    private NewsAddressProvider newsAddressProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hookUpScreenViews();

        //making the list ready
        newsItems = new ArrayList<>();

        //getting the news
        newsAddressProvider = new NewsAddressProvider();
        getTheNews(newsAddressProvider.getGeneral());

        //setting up the search view
        searchView.setOnQueryTextListener(this);
    }

    private void getTheNews(ArrayList<String> addresses) {

        for (String address : addresses) {
            StringRequest stringRequest = new StringRequest(address, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    NewsXMLParser newsXMLParser = new NewsXMLParser(response);
                    newsXMLParser.parseNewsXml();
                    newsItems.addAll(newsXMLParser.getNewsItems());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            Volley.newRequestQueue(this).add(stringRequest);
        }
        updateRecyclerView(newsItems);
    }

    private void hookUpScreenViews() {
        //setting the progressBar
        updateProgressBar = findViewById(R.id.updateProgressBar);
        newsItemsRecyclerView = findViewById(R.id.newsItemsRecyclerView);
        searchView = findViewById(R.id.main_news_search_view);
    }

    private void updateRecyclerView(ArrayList<NewsItem> newsList) {

        if (newsList != null) {
            updateProgressBar.setVisibility(View.INVISIBLE);
        }

        newsAdapter = new NewsAdapter(newsList, this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        newsItemsRecyclerView.setLayoutManager(layoutManager);
        newsItemsRecyclerView.setAdapter(newsAdapter);
        newsAdapter.notifyDataSetChanged();
    }


    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (newsAdapter != null) {
            newsAdapter.getFilter().filter(newText);
        }
        return true;
    }
}

我也尝试过

 if (newsList != null) {
            updateProgressBar.setVisibility(View.GONE);
        }

以及此代码:

if (newsList != null && newsList.size()>0) {
        updateProgressBar.setVisibility(View.INVISIBLE);
    }

我也将.GONE与第二种方法一起使用,但是这次ProgreesBar永远不会消失,它一直停留在那儿并且到处旋转,当我尝试Log.d时,它说newsList是空的!,虽然其中显然有值,并且recyclerView会显示它们。 我也用.size()进行了检查,而不是.isEmpty(),但结果仍然相同!

这是进度条的xml:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activities.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:background="@color/transparent"
        android:padding="5dp"
        android:id="@+id/newsItemsRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/mobileBanner"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:divider="@color/transparent"/>

    <ProgressBar
        android:id="@+id/updateProgressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:layout_gravity="top|center_horizontal"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您的updateRecyclerView方法在MainThead中被调用,并立即执行。 将您的updateRecyclerView方法放入onResponseonErrorResponse

答案 1 :(得分:1)

问题是您基本上是从updateProgressBar.setVisibility(View.INVISIBLE);间接调用onCreate()

Volley请求是异步的,因此在查询完成后将调用onResponse()

基本上,您的代码是这样运行的:首先从onCreate()调用getTheNews(),该代码几乎立即通过for循环运行(Volley是异步的)。然后依次是updateRecyclerView()updateProgressBar.setVisibility(View.INVISIBLE);

由于此操作非常快,因此您永远不会看到ProggessBar

编辑:

为计数器添加一个类变量:

private int addressCounter = 0;

现在像这样更改您的getTheNews()方法:

private void getTheNews(ArrayList<String> addresses) {

    for (String address : addresses) {
        StringRequest stringRequest = new StringRequest(address, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                NewsXMLParser newsXMLParser = new NewsXMLParser(response);
                newsXMLParser.parseNewsXml();
                newsItems.addAll(newsXMLParser.getNewsItems());

                newsAdapter.notifyDataSetChanged();
                if(addessCounter >= address.size()){
                    updateProgressBar.setVisibility(View.INVISIBLE);                        
                }
                //Increment the counter
                addressCounter++;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if(addessCounter >= address.size()){
                    updateProgressBar.setVisibility(View.INVISIBLE);                        
                }
                //increment the counter here, too!
                addressCounter++;
            }
        });
        Volley.newRequestQueue(this).add(stringRequest);
    }
}

您可能还需要考虑: 如果要使用hookUpScreenViews()初始化视图,为什么不继续添加searchView.setOnQueryTextListener(this);?您还可以添加用于设置RecyclerView的代码,因为它只需调用一次即可。

private void hookUpScreenViews() {
    //setting the progressBar
    updateProgressBar = findViewById(R.id.updateProgressBar);
    newsItemsRecyclerView = findViewById(R.id.newsItemsRecyclerView);
    searchView = findViewById(R.id.main_news_search_view);
    searchView.setOnQueryTextListener(this);

    newsAdapter = new NewsAdapter(newsList, this);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    newsItemsRecyclerView.setLayoutManager(layoutManager);
    newsItemsRecyclerView.setAdapter(newsAdapter);
}

免责声明:我没有尝试过这段代码,因此您可能需要对其进行一些调整。

编辑: 这是工作代码:

public void onResponse(String response) {
                addressCounter++;
                NewsXMLParser newsXMLParser = new NewsXMLParser(response);
                newsXMLParser.parseNewsXml();
                newsItems.addAll(newsXMLParser.getNewsItems());

                if (addressCounter>=addresses.size()){
                    updateRecyclerView(newsItems);
                }
            }