如何控制自动地出现不确定的进度圈 [英] How to control automagically appearing indeterminate progress circle
问题描述
在 MainActivity.onStart()
我的应用程序,它从一个RESTful服务中获取数据,填充与服务数据的本地的SQLite数据库,然后填充与景观该数据。使用RxJava,调用REST式服务并填充的SQLite是由是观察到的,包含该视图的片段使用了RxJava订阅这个功能。安装完成后,查看通知,并从SQLite的数据库获取数据。在此过程中,进度圈出现(自动,我已经添加了没有进度条code我自己)。当查看已被填充,圆不
不再显示。一切都很好。
当设备从纵向转向 - >风景(反之亦然),我的code认识到这一点,并跳过RESTful服务,并使用类似的观察者/订阅机制,从SQLite的DB和填充获取数据风景。进度圈在这里再次显示,但是,这就是问题所在,它使纺纱和永远不会消失,但应用程序将继续正常运行。
是什么导致了这种进步圈出现?如何控制呢?
第一观察者/订阅:
MyListFragment myListFragment =(MyListFragment)getSupportFragmentManager()findFragmentByTag(getResources()的getString(R.string.fragment_main_tag));
mLoadAndStoreDataObservable = Observable.create(
新Observable.OnSubscribe<串GT;(){
@覆盖
公共无效电话(订户LT ;?超级字符串>用户){ 尝试{
Utilities.loadAndStoreData(mActivity); //调用RESTful服务,填充的SQLite
subscriber.onNext(Utilities.loadAndStoreData完成);
}
赶上(例外五){
subscriber.onError(E);
}
}
}
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(myListFragment);
观察/用户,当设备旋转:
MyListFragment myListFragment =(MyListFragment)getSupportFragmentManager()findFragmentByTag(getResources()的getString(R.string.fragment_main_tag));
mLoadAndStoreDataObservable = Observable.create(
新Observable.OnSubscribe<串GT;(){
@覆盖
公共无效电话(订户LT ;?超级字符串>用户){ 尝试{
//不调用REST式服务,只要告诉观测数据是可用的SQLite
subscriber.onNext(Utilities.loadAndStoreData完成);
}
赶上(例外五){
subscriber.onError(E);
}
}
}
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(myListFragment);
MyListFragment
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
查看查看= super.onCreateView(充气器,容器,savedInstanceState); setListAdapter(NULL); //看看这个prevents进展圈......没了
返回视图。
}@覆盖
公共无效onNext(String s)将{
//现在SQLite的填充,所以在列表视图中显示的数据
ArrayList的< MyListData> myListDataList = Utilities.getDataFromSQLite(getActivity());
MyListAdapter适配器=新MyListAdapter(getActivity(),android.R.layout.simple_list_item_1,myListDataList);
setListAdapter(适配器);
}
我用来测试的设备是(第一代)的Nexus 7的Android 4.2.2和(第二代)的Nexus 7的Android 4.4.4
答:结果
LordRaydenMK表明默认ListView的实施示出了喷丝当列表不具有适配器集
这确实看来是如此。设置像下面的适配器造成进度圈不出现
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
查看查看= super.onCreateView(充气器,容器,savedInstanceState); 的String []值=新的String [] {正在等待数据...}; //从来没有看到这一点,但什么
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1,android.R.id.text1,价值观);
setListAdapter(适配器); //看看这个prevents进展圈...是的
返回视图。
}
我不能成为不从 MyListFragment
的code太大帮助。不过,我可以(TRY)到你指出正确的方向。
默认的ListView
的实施显示了微调当列表没有一个转接器。你确定你要设置的适配器?您是否使用 setListAdapter
来设置适配器?
此外,我想用RxJava与多个数据源时,推荐了不同的方法。请查看这个博客帖子丹卢。它使用 CONCAT()
和第()
运营商找到数据的最佳来源。
//我们的消息来源(作为练习留给读者)
观察到与lt;数据>存储器= ...;
观察到与lt;数据>磁盘= ...;
观察到与lt;数据>网络= ...;//检索数据的第一个源
观察到与lt;数据>来源=观测
.concat(内存,磁盘,网络)
。第一();
In MainActivity.onStart()
of my app, it gets data from a RESTful service, populates a local SQLite db with data from the service, then populates a View with that data. Using RxJava, the functionality that calls the RESTful service and populates the SQLite is made to be the observable and the fragment containing the View uses RxJava to subscribe to this. When it is done, the View is notified and gets the data from the SQLite db. During this process, a progress circle appears (automatically, I've added no progress bar code myself). When the View has been populated, the circle is no
longer displayed. All is well.
When the device is turned from Portrait -> Landscape (or vice-versa), my code recognizes this and skips the RESTful service and, using a similar observer/subscriber mechanism, gets data from the SQLite db and populates the View. The progress circle is displayed again here but, this is the problem, it keeps spinning and never goes away, though the app continues to function normally.
What is causing this progress circle to appear? How can I control it?
First 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 when device is rotated:
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);
}
The device I am testing with is a (1st gen) Nexus 7, Android 4.2.2 and a (2nd gen) Nexus 7, Android 4.4.4
Answer:
LordRaydenMK suggests that the default ListView implementation shows a spinner when the list doesn't have an adapter set
This indeed appears to be the case. Setting the adapter like below causes the progress circle not to appear
@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;
}
I can't be of much help without the code from MyListFragment
. However I can (try) to point you in the right direction.
The default ListView
implementation shows a spinner when the list doesn't have an adapter set. Are you sure you are setting the adapter? Are you using setListAdapter
to set the adapter?
Also I would like to recommend a different approach when using RxJava with multiple data sources. Check out this blog post by Dan Lew. It uses the concat()
and first()
operators to find the best source for your data.
// 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();
这篇关于如何控制自动地出现不确定的进度圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!