gpt4 book ai didi

android - 将可隐藏的标题 View 添加到 recyclerview

转载 作者:太空狗 更新时间:2023-10-29 12:59:00 27 4
gpt4 key购买 nike

我想添加一个 headerview,当用户向下滚动时隐藏,当用户向上滚动时再次显示。

示例:https://imgur.com/a/tTq70B0

正如您在链接“您正在写为...”中看到的那样,短语仅在用户滚动到顶部时显示。 Android sdk中有类似的东西吗?

我怎样才能达到同样的目的?

最佳答案

获取滚动事件只是实现这一目标的第一步。需要动画才能达到效果。我重新创建了您发布的 gif 示例的简单版本。

Example

主要 Activity 的布局,activity_main.xml

<?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="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"> <!-- Note the last line-->

<TextView
android:id="@+id/textview_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:text="Hello Stack Overflow!"/>


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

下面是主要 Activity 的代码,我们在其中填充 RecyclerView 并使用 addOnScrollListener 为 TextView 提供动画。请注意注释掉的行,由于上面 xml 布局中的 noted 行,这些行将提供默认的淡出或淡入动画。方法 slideAnimation() 是创建自定义动画的示例。此链接证明useful用于创建动画。

class MainActivity : AppCompatActivity() {
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: LinearLayoutManager

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

// Some data for the RecyclerView
val data: List<String> = (1..100).toList().map { it.toString() }

viewManager = LinearLayoutManager(this)
viewAdapter = TextAdapter(data)

findViewById<RecyclerView>(R.id.recyclerview_main).apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter

addOnScrollListener(
object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

val pastVisibleItems = viewManager.findFirstCompletelyVisibleItemPosition()
if (pastVisibleItems == 0) {
slideAnimation(0f, 1f, View.VISIBLE)
//textview_hello.visibility = View.VISIBLE
} else if (textview_hello.visibility != View.GONE) {
slideAnimation(-150f, 0f, View.GONE)
//textview_hello.visibility = View.GONE
}
}
}
)
}

... // SlideAnimation function

}

}

slideAnimation函数

private fun slideAnimation(translationY: Float, alpha: Float, viewVisibility: Int) {
textview_hello.visibility = View.VISIBLE
textview_hello.clearAnimation()
textview_hello
.animate()
.translationY(translationY)
.alpha(alpha)
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
super.onAnimationEnd(animation)
textview_hello.clearAnimation()
textview_hello.visibility = viewVisibility
}
})
.duration = 500
}

RecycleView 的适配器:

class TextAdapter(private val textList: List<String>) :
RecyclerView.Adapter<TextAdapter.TextViewHolder>() {

class TextViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): TextViewHolder {
val textView = LayoutInflater.from(parent.context)
.inflate(R.layout.item_text_view, parent, false) as TextView
return TextViewHolder(textView)
}

override fun onBindViewHolder(holder: TextViewHolder, position: Int) {
holder.textView.text = textList[position]
}

override fun getItemCount() = textList.size
}

要在 RecyclerView 中显示的项目,item_text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="16dp"
android:textAlignment="center"
android:background="@android:color/darker_gray">

</TextView>

关于android - 将可隐藏的标题 View 添加到 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57943309/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com