Coder Social home page Coder Social logo

滑动问题 about appbarlayout-spring-behavior HOT 2 CLOSED

todou avatar todou commented on September 25, 2024
滑动问题

from appbarlayout-spring-behavior.

Comments (2)

vickyleu avatar vickyleu commented on September 25, 2024

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:fitsSystemWindows="true"
app:statusBarBackground="@android:color/transparent">

<!--AppBarLayout必须设置固定高度-->
<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:layout_behavior="@string/appbar_spring_behavior">

    <!--设置可滚动并且折叠在顶部-->
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp"
        app:contentScrim="@color/transparent"
        app:layout_scrollFlags="exitUntilCollapsed|scroll"
        app:statusBarScrim="@color/transparent">
        <ImageView
            android:id="@+id/bg_layer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="fitXY"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
            app:layout_collapseMode="parallax" />


        <!--坑爹的google,pin模式只能在toolbar上生效-->
        <!--如果使用其他viewGroup包裹的情况下,toolbar-->
        <!--会被折叠-->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/pink"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <FrameLayout
                    android:id="@+id/left_component"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:foregroundGravity="center_vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_chainStyle="spread">

                    <ViewStub
                        android:id="@+id/left_stub"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_gravity="center_vertical" />

                    <TextView
                        android:id="@+id/left_txt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:textStyle="bold" />
                </FrameLayout>


                <TextView
                    android:id="@+id/title_component"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:text=""
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#252122"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="@+id/right_component"
                    app:layout_constraintStart_toStartOf="@+id/left_component"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:layout_editor_absoluteY="19dp" />

                <FrameLayout
                    android:id="@+id/right_component"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.5">

                    <TextView
                        android:id="@+id/right_txt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical|right"
                        android:text="" />

                    <ViewStub
                        android:id="@+id/right_stub"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical|right" />
                </FrameLayout>

            </android.support.constraint.ConstraintLayout>

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>


</android.support.design.widget.AppBarLayout>


<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.TabLayout
            android:id="@+id/toolbar_tab"
            android:layout_width="0dp"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="#ffffff"

            app:layout_collapseMode="pin"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabIndicatorColor="#0835f8"
            app:tabIndicatorHeight="2.0dp"
            app:tabSelectedTextColor="#0835f8"
            app:tabTextColor="#ced0d3">


            <android.support.design.widget.TabItem
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="分享" />

            <android.support.design.widget.TabItem
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="收藏" />

            <android.support.design.widget.TabItem
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="关注" />

        </android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/main_vp_container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/pink"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar_tab" />
    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

from appbarlayout-spring-behavior.

vickyleu avatar vickyleu commented on September 25, 2024

package com.aihuawen.erwin.mvp.view.activity

import android.graphics.Color
import android.os.Build
import android.support.design.widget.AppBarLayout
import android.support.v7.widget.Toolbar
import android.util.TypedValue
import android.view.WindowManager
import android.widget.TextView
import com.aihuawen.erwin.R
import com.aihuawen.erwin.base.context.BaseActivity
import com.aihuawen.erwin.mvp.contract.SpecialSubjectContract
import com.aihuawen.erwin.mvp.presenter.SpecialSubjectPresenter
import com.aihuawen.erwin.utils.GlideUtil
import com.aihuawen.erwin.utils.doOnBackPressedEvent
import com.huawen.baselibrary.views.drawableRes
import com.huawen.baselibrary.views.layoutParamsMargin
import org.jetbrains.anko.dip

class SpecialSubjectActivity : BaseActivity<SpecialSubjectContract.View, SpecialSubjectContract.Presenter>(), SpecialSubjectContract.View {
override fun getLayoutId() = R.layout.activity_subject

override fun configView() {
    find<TextView>(R.id.left_txt)?.drawableRes(left = R.drawable.icon_back)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        var statusBarHeight = -1
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = resources.getDimensionPixelSize(resourceId)
        }
        var actionBarHeight = 0

        // Calculate ActionBar height
        try {
            val tv = TypedValue()
            if (theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }


        val total = statusBarHeight + actionBarHeight
        find<Toolbar>(R.id.toolbar)?.setPadding(0, statusBarHeight, 0, 0)
        find<Toolbar>(R.id.toolbar)?.layoutParams?.height = total
    }

    //设置toolbar
    setSupportActionBar(find(R.id.toolbar))
    doOnBackPressedEvent(find<TextView>(R.id.left_txt))
    find<TextView>(R.id.right_txt)!!.layoutParamsMargin(right = dip(10))
    find<TextView>(R.id.left_txt)!!.layoutParamsMargin(dip(10))
    find<TextView>(R.id.right_txt)!!.drawableRes(R.drawable.icon_share)
    GlideUtil.intoView(find(R.id.bg_layer), "http://pic166.nipic.com/file/20180518/8429994_112007730037_2.jpg")
    val appBar = find<AppBarLayout>(R.id.appBar)
    appBar?.addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener {
        override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
            find<Toolbar>(R.id.toolbar)?.setBackgroundColor(changeAlpha(resources.getColor(R.color.white),
                    Math.abs(verticalOffset * 1.0f) / (appBarLayout?.totalScrollRange ?: 1)))
        }
    })

// val tabScrimHelper = TabScrimHelper(find(R.id.toolbar_tab), find(R.id.toolbarLayout)){
// appBarLayout, verticalOffset ->
// find(R.id.toolbar)?.setBackgroundColor(changeAlpha(resources.getColor(R.color.white),
// Math.abs(verticalOffset * 1.0f) / (appBarLayout?.totalScrollRange ?: 1)))
// }
// appBar?.addOnOffsetChangedListener(tabScrimHelper)
}

/** 根据百分比改变颜色透明度  */
fun changeAlpha(color: Int, fraction: Float): Int {
    val red = Color.red(color)
    val green = Color.green(color)
    val blue = Color.blue(color)
    val alpha = (Color.alpha(color) * fraction).toInt()
    return Color.argb(alpha, red, green, blue)
}

override fun initData() {
}

override fun initPresenter() = SpecialSubjectPresenter()
override fun showError(e: Throwable) {
}

override fun complete() {
}

}

from appbarlayout-spring-behavior.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.