如何控制自动出现不确定进度圈

How to control automagically appearing indeterminate progress circle

在我的应用程序的 MainActivity.onStart() 中,它从 RESTful 服务获取数据,使用服务中的数据填充本地 SQLite 数据库,然后使用该数据填充视图。使用 RxJava,调用 RESTful 服务并填充 SQLite 的功能成为可观察的,包含视图的片段使用 RxJava 订阅它。完成后,View 会收到通知并从 SQLite 数据库中获取数据。在这个过程中,会出现一个进度圈(自动的,我自己没有加进度条码)。当视图已经填充时,圆圈是没有的 显示时间更长。一切顺利。

当设备从纵向 -> 横向(反之亦然)转动时,我的代码识别出这一点并跳过 RESTful 服务,并使用类似的 observer/subscriber 机制从SQLite 数据库并填充视图。进度圈在这里再次显示,但是,这就是问题所在,它一直在旋转并且永远不会消失,尽管应用程序继续正常运行。

出现这个进度圈的原因是什么?我该如何控制它?

第一个observer/subscriber:

MyListFragment myListFragment = (MyListFragment) getSupportFragmentManager().findFragmentByTag(getResources().getString(R.string.fragment_main_tag));
mLoadAndStoreDataObservable = Observable.create(
   new Observable.OnSubscribe<String>() {
         @Override
         public void call(Subscriber<? super String> subscriber) {

             try {
                 Utilities.loadAndStoreData(mActivity); // call RESTful service, populate SQLite
                 subscriber.onNext("Utilities.loadAndStoreData Done");
             }
             catch (Exception e) {
             subscriber.onError(e);
             }
         }
       }
     )
     .subscribeOn(Schedulers.io())
     .observeOn(AndroidSchedulers.mainThread())
     .subscribe(myListFragment);

Observer/subscriber 当设备旋转时:

MyListFragment myListFragment = (MyListFragment) getSupportFragmentManager().findFragmentByTag(getResources().getString(R.string.fragment_main_tag));
mLoadAndStoreDataObservable = Observable.create(
   new Observable.OnSubscribe<String>() {
         @Override
         public void call(Subscriber<? super String> subscriber) {

             try {
                // don't call restful service,  Just tell observer that data is available in SQLite
                subscriber.onNext("Utilities.loadAndStoreData Done");
             }
             catch (Exception e) {
             subscriber.onError(e);
             }
         }
       }
     )
     .subscribeOn(Schedulers.io())
     .observeOn(AndroidSchedulers.mainThread())
     .subscribe(myListFragment);

MyListFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view =  super.onCreateView(inflater, container, savedInstanceState);

    setListAdapter(null); // see if this prevents progress circle ... nope
    return view;
}

@Override
public void onNext(String s) {
    // SQLite is populated now, so display the data in the list view
    ArrayList<MyListData> myListDataList = Utilities.getDataFromSQLite(getActivity());
    MyListAdapter adapter = new MyListAdapter(getActivity(), android.R.layout.simple_list_item_1, myListDataList);
    setListAdapter(adapter);
}

我正在测试的设备是(第一代)Nexus 7,Android 4.2.2 和(第二代)Nexus 7,Android 4.4.4

回答:
LordRaydenMK 建议默认的 ListView 实现在列表没有适配器集时显示微调器 看来确实如此。像下面这样设置适配器会导致进度圈不出现

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view =  super.onCreateView(inflater, container, savedInstanceState);

    String[] values = new String[] { "Waiting for data ..." }; // never see this but whatever
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity() .getApplicationContext(), android.R.layout.simple_list_item_1,  android.R.id.text1, values);
    setListAdapter(adapter); // see if this prevents progress circle ... yep
    return view;
}

如果没有来自 MyListFragment 的代码,我帮不上什么忙。但是我可以(尝试)为您指明正确的方向。

默认 ListView 实现在列表没有适配器集时显示微调器。您确定要设置适配器吗?您是否使用 setListAdapter 设置适配器?

另外,我想推荐一种不同的方法来使用 RxJava 和多个数据源。查看 Dan Lew 的 this blog post。它使用 concat()first() 运算符来为您的数据找到最佳来源。

// Our sources (left as an exercise for the reader)
Observable<Data> memory = ...;  
Observable<Data> disk = ...;  
Observable<Data> network = ...;

// Retrieve the first source with data
Observable<Data> source = Observable  
  .concat(memory, disk, network)
  .first();