美文网首页
Androidx 折叠标题栏

Androidx 折叠标题栏

作者: 折剑游侠 | 来源:发表于2020-04-27 15:00 被阅读0次

Androix库迁移

依赖项

    implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"
    implementation "androidx.recyclerview:recyclerview:1.1.0"
    implementation 'com.google.android.material:material:1.2.0-alpha06'

activity_behavior.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="标题"
            android:textColor="@android:color/holo_orange_light"
            android:textSize="24sp"
            app:layout_scrollFlags="scroll|enterAlways" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvBehavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

BehaviorActivity

class BehaviorActivity : AppCompatActivity() {
    private lateinit var adapter: BehaviorAdapter
    private val data = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_behavior)


        for (i in 0..100) {
            data.add("第${i}项")
        }
        adapter = BehaviorAdapter(data)
        rvBehavior.layoutManager = LinearLayoutManager(this)
        rvBehavior.adapter = adapter
    }
}

BehaviorAdapter

class BehaviorAdapter(private val data: ArrayList<String>) : RecyclerView.Adapter<BehaviorAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_rv, parent, false)
        return ViewHolder(view)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.tvItem.text = data[position]
    }


    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvItem = itemView.findViewById<TextView>(R.id.tvRvItem)
    }
}

item_rv

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvRvItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="20sp" />
</LinearLayout>

相关文章

网友评论

      本文标题:Androidx 折叠标题栏

      本文链接:https://www.haomeiwen.com/subject/vxgywhtx.html