android studio的模拟器录屏会黑屏,也不知道是什么bug
主要用到了PagerSnapHelper
(使RecyclerView滑动表现和viewpager一致)和滑动监听器
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<com.example.gallery.GalleryRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintTop_toTopOf="parent" />
androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
androidx.constraintlayout.widget.ConstraintLayout>
package com.example.gallery
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.gallery.databinding.ActivityMainBinding
import java.util.*
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var timer: Timer = Timer()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 使用viewBinding
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val galleryAdapter = GalleryAdapter(arrayListOf(
"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3801171559,43130079&fm=26&gp=0.jpg",
"https://img.zcool.cn/community/01936a58be7362a801219c77fe8358.jpg@1280w_1l_2o_100sh.jpg",
"https://th.bing.com/th/id/R5d175f44c74fe609a7e7904c1581ba9e?rik=x36FeBN9WfmLcg&riu=http%3a%2f%2fwww.benbenla.cn%2fimages%2f20150626%2fbenbenla-03c.jpg&ehk=HdoV02a0Y%2bAbwlolGa0M%2fdZ073aXMUqidwkvB%2fLo1ZM%3d&risl=&pid=ImgRaw"
))
binding.recyclerView.adapter = galleryAdapter
binding.recyclerView.scrollToPosition(galleryAdapter.start)
val paddingHorizon = 100
val paddingVertical = 50
binding.recyclerView.setPadding(paddingHorizon, paddingVertical, paddingHorizon, paddingVertical)
binding.recyclerView.clipToPadding = false
}
override fun onResume() {
super.onResume()
timer.schedule(object : TimerTask() {
override fun run() {
binding.recyclerView.next()
}
}, 5000, 5000)
}
override fun onPause() {
super.onPause()
timer.cancel()
}
}
package com.example.gallery
import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs
import kotlin.math.min
class GalleryRecyclerView : RecyclerView {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
var minScale: Float = 0.9f
var maxScale: Float = 1.1f
val linearLayoutManager = LinearLayoutManager(context, HORIZONTAL, false)
var position: Int = 0
init {
layoutManager = linearLayoutManager
val pagerSnapHelper = PagerSnapHelper()
pagerSnapHelper.attachToRecyclerView(this)
// 添加滑动监听
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
val first = linearLayoutManager.findFirstVisibleItemPosition()
val last = linearLayoutManager.findLastVisibleItemPosition()
val snapView = pagerSnapHelper.findSnapView(linearLayoutManager) ?: return
val snap = linearLayoutManager.getPosition(snapView)
position = snap
if (snap <= 100 || snap >= Int.MAX_VALUE - 100) {
scrollToPosition(adapter!!.itemCount / 2)
}
// 避免当前展示图被遮挡
snapView.z = Float.MAX_VALUE
// 放大程度
val scaleSpan: Float = maxScale - minScale
// 前一张图片
linearLayoutManager.findViewByPosition(first)?.apply {
pagerSnapHelper
.calculateDistanceToFinalSnap(linearLayoutManager, this)
?.get(0)
?.let {
distance ->
// 滑动时,系数ratio值为[0,1]
val ratio: Float = min((width - abs(distance)).toFloat() / 800, 1f)
scaleX = ratio * scaleSpan + minScale
scaleY = scaleX
}
}
// 后一张图片
linearLayoutManager.findViewByPosition(last)?.apply {
pagerSnapHelper
.calculateDistanceToFinalSnap(linearLayoutManager, this)
?.get(0)
?.let {
distance ->
val ratio: Float = min((width - abs(distance)).toFloat() / 800, 1f)
scaleX = ratio * scaleSpan + minScale
scaleY = scaleX
}
}
}
})
}
fun next() {
smoothScrollToPosition(position + 1)
}
}
package com.example.gallery
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
class GalleryAdapter(var urls: List<String>) : RecyclerView.Adapter<GalleryAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// Create a new view, which defines the UI of the list item
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val index = position % urls.size
Picasso
.get()
.load(urls[index])
.placeholder(R.drawable.place_holder)
.into(holder.imageView)
}
override fun getItemCount(): Int {
return Int.MAX_VALUE
}
val start: Int
get() {
val temp = itemCount / 2
return temp - temp % urls.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.findViewById(R.id.image_view)
}
}