我想通过订阅从数据库获取数据的Observable
来填充导航抽屉,完成后通知订阅者(使用 RxJava
(。我已经做了类似的事情,Subscriber
是一个扩展Fragment
的类,但不同的是,导航抽屉不是一个单独的片段,而是在 MainActivity 中定义的。
如何订阅Navigation Drawer
Observable
?
主要活动.java
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@Override
public void onStart() { mLoadAndStoreDataObservable = Observable.create(
super.onStart(); new Observable.OnSubscribe<String>() {
// fragment creation code was here
// populates a String[] myStringArray for Navigation Drawer permitsSQLiteManager.addLogLine(mActivity, logString);
if (!skipRestfulServiceCall) {
getDataFromRestfulService(); }
} catch (Exception e) {
else { // get data from SQLite Log.e("loadAndStoreData ", "Exception: " + e.getMessage());
getDataFromSQLite(); mProgressDialog.dismiss();
} }
}
mTitle = mDrawerTitle = getTitle(); }
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); .subscribeOn(Schedulers.io())
mDrawerList = (ListView) findViewById(R.id.left_drawer); .observeOn(AndroidSchedulers.mainThread())
.subscribe(mDrawerLayout); // parameter was a Fragment
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, myStringArray));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void getDataFromRestfulService() {
<get data>
// implement RxJava-Observable/Subscriber
mLoadAndStoreDataObservable = Observable.create(
new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
Utilities.loadAndStoreData(mActivity);
subscriber.onNext("Utilities.loadAndStoreData Done");
//subscriber.onCompleted();
Log.e("MainActivity.onCreate()", "subscriber.onNext("Utilities.loadAndStoreData Done")");
String logString = "MainActivity.onCreate() - subscriber.onNext(Utilities.loadAndStoreData Done)";
Log.e(TAG, logString);
PermitsSQLiteManager permitsSQLiteManager = PermitsSQLiteManager.getInstance();
permitsSQLiteManager.addLogLine(mActivity, logString);
mProgressDialog.dismiss();
}
catch (Exception e) {
Log.e("loadAndStoreData ", "Exception: " + e.getMessage());
subscriber.onError(e);
mProgressDialog.dismiss();
}
}
}
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mDrawerLayout); // compile error // suscriber was a Fragment, but no longer
}
如果mDrawerLayout是一个实现Observer
的类的实例,那么你可以把它投射到Observer
NavigationDrawer mDrawerLayout = ...
yourObservable.subscribe((Observer)mDrawerLayout);
如果它没有实现Observer
接口,则可以创建一个将操作导航抽屉的新Observer
。(您可能需要声明mDrawerLayout
为最终决定(
final NavigationDrawer mDrawerLayout = ...
yourObservable.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
// on completed
}
@Override
public void onError(Throwable throwable) {
// on error
}
@Override
public void onNext(String people) {
mDrawerLayout.doSomething();
}
});
假设您要接收来自 News.java 类的更新。
因此,您可以从新闻类实现观察器接口
News implements Observable{
public void publishNews(){
String newNews = "newNews";
//Now notify Users that new news is available by calling the following
// two methods
setChanged();
notifyObservers(); // This will call the subscribers and notify them
// that there is a change.
}
}
现在,您希望用户接收更新。
public class User implements Observer(){
}
将其添加到要创建用户类变量的任何类中。此代码确保用户 1、用户 2 对象"订阅"到新闻类
News news = new News();
User user1 = new User();
User user2 = new User();
news.add(user1); // You have to instantiate News object to user the add method
news.add(user2); // from Observable interface.
update(( 方法由 Observer 提供接口。
@Overrride
public void update(Observable, Object){
// This gets called after notifyObservers() in the Observable class
// Whatever you want to do after Observable calls notifyObservers, goes here
}