在Flow中使用Dispatcher的正确方法?

时间:2016-03-04 19:15:14

标签: android square-flow

我有一个简单的双屏应用程序。第一个屏幕向用户显示登录选项,第二个屏幕是他们在登录时看到内容的位置。

用户登录后,我将密钥设置为Flow.get( view ).set( Screens.CHAT )

一切都很好,花花公子但我遇到的问题让我的观点看起来像这样:

enter image description here

当用户通过按下主页按钮离开应用程序然后返回到它(基本上在onRestart( )之后)时,会重新绘制视图。未填充的RecyclerView将再次添加到视图层次结构中。

我的Dispatcher逻辑是:

public void dispatch(Traversal traversal, TraversalCallback callback) {
    Object dest = traversal.destination.top();
    Object source = traversal.origin == null ? null : traversal.origin.top();

    ViewGroup root = (ViewGroup) activity.findViewById(R.id.root);

    if (traversal.origin != null) {
        int childCount = root.getChildCount();
        // Our container has a root view with an ImageView in it.
        // If child count > 1, we need to remove the child at position = 1
        // because ImageView is at position = 0
        if (childCount > 1) {
            // save state
            traversal.getState(traversal.origin.top()).save(root.getChildAt(1));
            // remove the added views
            removeAllViewsAfter(root,0);
        }
    }

    @LayoutRes int layout;
    if(dest.equals(Screens.WELCOME)){
        layout = R.layout.view_welcome;
    }else if(dest.equals(Screens.CHAT)){
        layout = R.layout.view_chat;
    }else{
        throw new IllegalArgumentException("Unrecognized Screen");
    }

    View incomingView = LayoutInflater
            .from(traversal.createContext(dest, activity))
            .inflate(layout, root, false);
    traversal.getState( traversal.destination.top() ).restore(incomingView);
    root.addView( incomingView );
    callback.onTraversalCompleted();
}
//----------------------------------------------------------------------------------------------
private void removeAllViewsAfter(ViewGroup root, int index){
    for( int i = root.getChildCount() - 1; i > index; i-- ){
        root.removeViewAt( i );
    }
}
//----------------------------------------------------------------------------------------------  

我在Dispatcher逻辑中做了哪些更改,以避免相同的布局被夸大并再次添加?

1 个答案:

答案 0 :(得分:0)

问题是您的root ViewGroup 未被清除。

我相信这会解决您的问题(<删除,>添加)。

12c12
<         if (childCount > 1) {
---
>         if (childCount > 0) {
14c14
<             traversal.getState(traversal.origin.top()).save(root.getChildAt(1));
---
>             traversal.getState(traversal.origin.top()).save(root.getChildAt(0));
16c16
<             removeAllViewsAfter(root,0);
---
>             frame.removeAllViews();
36,42d35
< //----------------------------------------------------------------------------------------------
< private void removeAllViewsAfter(ViewGroup root, int index){
<     for( int i = root.getChildCount() - 1; i > index; i-- ){
<         root.removeViewAt( i );
<     }
< }
< //----------------------------------------------------------------------------------------------  

Flow repo对您已有的内容有example with something pretty close

@Override
public void dispatch(@NonNull Traversal traversal, @NonNull TraversalCallback callback) {
  Log.d("BasicDispatcher", "dispatching " + traversal);
  Object destKey = traversal.destination.top();

  ViewGroup frame = (ViewGroup) activity.findViewById(R.id.basic_activity_frame);

  // We're already showing something, clean it up.
  if (frame.getChildCount() > 0) {
    final View currentView = frame.getChildAt(0);

    // Save the outgoing view state.
    if (traversal.origin != null) {
      traversal.getState(traversal.origin.top()).save(currentView);
    }

    // Short circuit if we would just be showing the same view again.
    final Object currentKey = Flow.getKey(currentView);
    if (destKey.equals(currentKey)) {
      callback.onTraversalCompleted();
      return;
    }

    frame.removeAllViews();
  }

  @LayoutRes final int layout;
  if (destKey instanceof HelloScreen) {
    layout = R.layout.hello_screen;
  } else if (destKey instanceof WelcomeScreen) {
    layout = R.layout.welcome_screen;
  } else {
    throw new AssertionError("Unrecognized screen " + destKey);
  }

  View incomingView = LayoutInflater.from(traversal.createContext(destKey, activity)) //
      .inflate(layout, frame, false);

  frame.addView(incomingView);
  traversal.getState(traversal.destination.top()).restore(incomingView);

  callback.onTraversalCompleted();
}