我對RxJava相當陌生,我在想如果我的問題可以用它解決。當X發生時,通過Observable在片段之間傳遞數據
我有一個有兩個片段的活動:一個在CollapsingToolbarLayout內部託管的頭部片段和一個正文片段。標題片段具有包含帖子的回收站視圖,正文片段顯示與標題片段中顯示的當前帖子相關的註釋。
現在,回收站視圖綁定帖子時立即加載註釋。不過,我只想在用戶開始摺疊工具欄時才加載註釋。我有一個監聽器設置爲工具欄,當用戶開始向上拖動工具欄時被調用,但我真的不知道如何使用標題片段中的當前帖子將其掛接到主體片段。
來形容它的僞代碼,它是這樣的:
// Activity has this method invoked when toolbar starts collapsing;
// It's invoked only once per "drag"
public void onToolbarCollapsing() {
// Somehow should pass the Post to the body fragment..?
}
// The header fragment has this method invoked when a post is bound
public void onPostBound(Post post) {
// Create an Observable<Post> and pass it to activity..?
}
// The body fragment could probably observe the observable?
目前,我有Activity
實施ActivityCallback
:
public interface ActivityCallback {
void loadCommentsFor(Post post);
}
public class MainActivity extends AppCompatActivity implements ActivityCallback {
@Override void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create fragments, set activity callback to the header fragment
HeaderFragment header = new HeaderFragment();
header.setActivityCallback(this);
BodyFragment body = new BodyFragment();
// Set fragments to the view etc.
}
// Implementation
@Override
public void loadCommentsFor(Post post) {
// Just pass the post object to the body fragment
bodyFragment.loadCommentsFor(post);
}
}
如何解決這個任何想法?