查看/地产转换

时间:2016-05-23 13:11:05

标签: android animation android-transitions

enter image description here 当用户向下滚动recyclerview时,我需要进行以下转换,如附图所示。 “标题A”和“描述”位于RelativeLayout内部,将折叠并更改其属性:

  1. “标题A”,来自layout_centerHorizo​​ntal = true,layout_centerVertical = true
  2. “描述”从layout_centerHorizo​​ntal = true到layout_centerVertical = true,文本化从30sp到16sp
  3. 最小化relativelayout,高度从70dp到wrap_content
  4. 当用户向上滚动并点击顶部时,更改将恢复为原始展开形式。 (这类似于折叠工具栏的概念)

    我已成功实现,当我点击一个按钮时,布局和视图将使用view.requestlayout()正确更改。但我不确定如何将转换链接到onscrolled函数的deltaY。

    这是我尝试过的:

        changeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animateChanges();
            }
        });
    
     private void animateChanges() {
    
        ViewGroup.LayoutParams layout = relativeLayout.getLayoutParams();
        RelativeLayout.LayoutParams header = (RelativeLayout.LayoutParams) titleA.getLayoutParams();
        RelativeLayout.LayoutParams value = (RelativeLayout.LayoutParams) description.getLayoutParams();
    
        // if current is not expanded, we change to expand view
        if (!isExpanded) {
            relativeLayout.setGravity(Gravity.CENTER);
            layout.height = (int) getResources().getDimension(R.dimen.height_60);
            header.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
            header.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
            header.addRule(RelativeLayout.CENTER_VERTICAL, 0);
            value.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
            value.addRule(RelativeLayout.BELOW, R.id.title_a);
            value.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
            value.addRule(RelativeLayout.CENTER_VERTICAL, 0);
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                value.addRule(RelativeLayout.ALIGN_PARENT_END, 0);
                header.addRule(RelativeLayout.ALIGN_PARENT_START, 0);
            }
            relativeLayout.setLayoutParams(layout);
            titleA.setLayoutParams(header);
            description.setLayoutParams(value);
            description.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_30));
    
            titleA.requestLayout();
            description.requestLayout();
            relativeLayout.requestLayout();
        } else {
    
            relativeLayout.setGravity(Gravity.CENTER_VERTICAL);
            layout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            header.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);
            header.addRule(RelativeLayout.BELOW, 0);
            header.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            header.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            value.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);
            value.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            value.addRule(RelativeLayout.BELOW, 0);
            value.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                value.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
                header.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
            }
            titleA.setLayoutParams(header);
            description.setLayoutParams(value);
            relativeLayout.setLayoutParams(layout);
            description.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_16));
    
            relativeLayout.requestLayout();
            titleA.requestLayout();
            description.requestLayout();
        }
        isExpanded = !isExpanded;
    }
    

    提前致谢!

0 个答案:

没有答案