kotlin AppBarLayout+CollapsingToolbarLayout+Toolbar+RecyclerView 滾動摺疊

1:沒有滾動前 

2:滾動中

 2:向上滾動完成效果

 

 

<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.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stateListAnimator="@null">

        <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/dsad"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="@color/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <ImageView
                    android:scaleType="centerCrop"
                    android:src="@mipmap/timg"
                    android:layout_width="match_parent"
                    app:layout_collapseMode="parallax"
                    android:layout_height="200dp"/>

            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbarw"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="48dp"
                    app:contentInsetStart="0dp"
                    app:layout_collapseMode="pin"
                    app:navigationIcon="@null">

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

                    <!--<android.support.constraint.ConstraintLayout-->
                            <!--android:id="@+id/rl_tool_bar_container"-->
                            <!--android:layout_width="match_parent"-->
                            <!--android:layout_height="48dp">-->

                        <TextView
                                android:id="@+id/bar_tv"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center"
                                app:layout_constraintStart_toStartOf="parent"
                                app:layout_constraintEnd_toEndOf="parent"
                                app:layout_constraintTop_toTopOf="parent"
                                tools:text="課程"/>
                    <!--</android.support.constraint.ConstraintLayout>-->


                </android.support.constraint.ConstraintLayout>
            </android.support.v7.widget.Toolbar>

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

    <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_rww"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:clipToPadding="false"
            android:overScrollMode="never"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

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

 

class BarLayoutActivity : BaseActivity() {
    private var stringList = ArrayList<String>()
    private lateinit var toobarw: Toolbar;
    private var linearLayoutManager: MeasuringLinearLayoutManager? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.app_bar_layout)
        intiView()
        setSupportActionBar(toolbarw)
    }

    fun intiView() {
        for (i in 0..49) {
            stringList.add(i.toString() + "item")
        }
        linearLayoutManager = MeasuringLinearLayoutManager(mContext, 1)
        linearLayoutManager!!.orientation = LinearLayoutManager.VERTICAL
        rv_rww.layoutManager = linearLayoutManager

        rv_rww.adapter = barLayouAdapter(stringList, mContext)
        appBarLayout.addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener {
            override fun onOffsetChanged(p0: AppBarLayout?, p1: Int) {
                Log.i("retetre", "eeww111=" + p1)
                if (p1 == 0) {
                    setScanToolColor(false)
                    Log.i("ewewewe", "wwww11111=" )
                } else {
                    Log.i("ewewewe", "wwww22222=" )
                    setScanToolColor(true)
                }

            }

        })

    }

    private fun setScanToolColor(isSetColor: Boolean) {
        if (isSetColor) {
            bar_tv.text = "我愛學習"
        }else{
            bar_tv.text = ""
            bar_tv.setTextColor(ContextCompat.getColor(mContext, R.color.black))
        }
    }
}

 適配器

class barLayouAdapter (var list: MutableList<String>, var mContext: Context) :
    RecyclerView.Adapter<barLayouAdapter.ViewHolder>() {
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
        return ViewHolder(TextView(p0.context))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ViewHolder, p1: Int) {
        holder.textView.text = list[p1]
    }


    class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章