天天看點

AppbarLayout中Toolbar包含了子view時設定标題顔色

public class AppBarTextColorController implements OnOffsetChangedListener {

    private final CollapsingToolbarLayout collapsingLayout;
    private final int expandColor;
    private final int collapseColor;
    private final TextView textView;

    private int textColorType = 0;
    private final int typeExpand = 1;
    private final int typeCollapse = 2;

    public AppBarTextColorController(CollapsingToolbarLayout collapsingLayout, int expandColor, int collapseColor, TextView textView) {
        this.collapsingLayout = collapsingLayout;
        this.expandColor = expandColor;
        this.collapseColor = collapseColor;
        this.textView = textView;
    }


    @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        LogUtil.d(Contant.DEBUG_LOG, new StringBuilder().append("onOffsetChanged: ").append(textColorType)
                .append(",").append(collapsingLayout.getHeight()).append(",")
                .append(collapsingLayout.getScrimVisibleHeightTrigger())
                .append(", appbar").append(appBarLayout.getHeight()).append(",")
                .append(appBarLayout.getTotalScrollRange()).append(",")
                .append(verticalOffset).toString());
//                collapsingLayout.
        int scrimHeight = collapsingLayout.getHeight() + verticalOffset;
        if (textColorType != typeCollapse && scrimHeight < collapsingLayout.getScrimVisibleHeightTrigger()) {
            textColorType = typeCollapse;
            textView.setTextColor(collapseColor);
        } else if (textColorType != typeExpand && scrimHeight > collapsingLayout.getScrimVisibleHeightTrigger()) {
            textColorType = typeExpand;
            textView.setTextColor(expandColor);
        }
    }
}