Android: Collapsing Linearlayout instead of Collapsing Toolbar

前端 未结 4 723
生来不讨喜
生来不讨喜 2020-12-30 10:52

I\'m trying to create a Master/Detail transaction in a single fragment. I thought of using LinearLayout as the container of my edittext for my header. Then a RecyclerView fo

4条回答
  •  星月不相逢
    2020-12-30 11:27

    Improved my answer using David's comment.

    I would animate "topMargin" of your header:

    LinearLayout _headerLayout; // expected to be set in "onCreateView"
    int _headerHeight; // expected to be set in "onCreateView" as _headerHeight = getHeaderHeight();
    
    Animation _hideAnimation = new Animation() {
      @Override
      protected void applyTransformation(float interpolatedTime, Transformation t) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) _headerLayout.getLayoutParams();
        params.topMargin = -(int) (_headerHeight * interpolatedTime);
        _headerLayout.setLayoutParams(params);
      }
    };
    
    Animation _showAnimation = new Animation() {
      @Override
      protected void applyTransformation(float interpolatedTime, Transformation t) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) _headerLayout.getLayoutParams();
        params.topMargin = (int) (_headerHeight * (interpolatedTime - 1));
        _headerLayout.setLayoutParams(params);
      }
    };
    
    private int getHeaderHeight()
    {
      _headerLayout.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
      return _headerLayout.getMeasuredHeight();
    }
    

    Hiding header:

    _headerLayout.clearAnimation();
    _headerLayout.startAnimation(_hideAnimation);
    

    Showing header:

    _headerLayout.clearAnimation();
    _headerLayout.startAnimation(_showAnimation);
    

    You can also easily set duration for your animations:

    _hideAnimation.setDuration(2000) // will hide in 2 seconds
    _showAnimation.setDuration(2000) // will show in 2 seconds
    

提交回复
热议问题