How to get soon-to-be-selected page while settling snapping feature on RecyclerView and ViewPager(如何在解决 RecyclerView 和 ViewPager 上的捕捉功能时获得即将被选中的页面)
本文介绍了如何在解决 RecyclerView 和 ViewPager 上的捕捉功能时获得即将被选中的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
背景
ViewPager
在你执行一些滚动后会捕捉到一个视图,如果你使用类似这样的东西,RecyclerView
也可以:
LinearSnapHelper().attachToRecyclerView(recyclerView)
或者通过使用库来捕捉到某个边缘,如
回收站视图:
我尝试过的 POC 代码(ViewPager
和 RecyclerView
):
MainActivity.kt
类 MainActivity : AppCompatActivity() {覆盖 fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val inflater = LayoutInflater.from(this)//viewPager 区域viewPager.adapter = 对象:RecyclerPagerAdapter() {var selectedHolder:RecyclerPagerAdapter.ViewHolder?=空覆盖乐趣 onCreateViewHolder(父:ViewGroup,viewType:Int):ViewHolder {返回对象:RecyclerPagerAdapter.ViewHolder(inflater.inflate(R.layout.cell, parent, false)) {}}覆盖有趣的 getItemCount(): Int = 100覆盖乐趣 onBindViewHolder(holder: ViewHolder, position: Int) {(holder.itemView as TextView).text = position.toString()}覆盖有趣的 setPrimaryItem(container: ViewGroup?, position: Int, obj: Any?) {super.setPrimaryItem(容器,位置,obj)//TODO 尽快获取即将被选中的页面val holder = obj 作为 RecyclerPagerAdapter.ViewHolderif (selectedHolder != null && selectedHolder != 持有人)(selectedHolder!!.itemView as TextView).text = position.toString()(holder.itemView as TextView).text = "selected:${position.toString()}"selectedHolder = 持有者}}viewPager.addOnPageChangeListener(对象:ViewPager.OnPageChangeListener {覆盖乐趣 onPageScrollStateChanged(state: Int) {当(状态){ViewPager.SCROLL_STATE_DRAGGING ->Log.d("AppLog", "onPageScrollStateChanged: SCROLL_STATE_DRAGGING")ViewPager.SCROLL_STATE_IDLE ->Log.d("AppLog", "onPageScrollStateChanged: SCROLL_STATE_IDLE")ViewPager.SCROLL_STATE_SETTLING ->Log.d("AppLog", "onPageScrollStateChanged: SCROLL_STATE_SETTLING")}}覆盖乐趣 onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {Log.d("AppLog", "onPageScrolled: position:$position positionOffset :$positionOffset positionOffsetPixels:$positionOffsetPixels")}覆盖乐趣 onPageSelected(position: Int) {Log.d("AppLog", "onPageSelected:" + 位置)}})//recyclerView区域//不需要,因为我为此使用了一个库:LinearSnapHelper().attachToRecyclerView(recyclerView)recyclerView.setHasFixedSize(true)recyclerView.adapter = 对象:RecyclerView.Adapter() {覆盖有趣的 getItemCount(): Int = 100覆盖乐趣 onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {返回对象:RecyclerView.ViewHolder(inflater.inflate(R.layout.cell, parent, false)) {}}覆盖乐趣 onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {(holder.itemView as TextView).text = position.toString()}}recyclerView.addOnPageChangedListener { oldPosition, newPosition ->Log.d("AppLog", "OnPageChanged:$oldPosition->$newPosition") }recyclerView.addOnScrollListener(对象:RecyclerView.OnScrollListener(){@SuppressLint("ClickableViewAccessibility")覆盖乐趣 onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {super.onScrollStateChanged(recyclerView, newState)当(新状态){RecyclerView.SCROLL_STATE_IDLE ->{Log.d("AppLog", "状态:SCROLL_STATE_IDLE")recyclerViewStateTextView.text = "状态:SCROLL_STATE_IDLE"//setOnTouchListener 并不能很好地工作.它使滚动卡住//recyclerView!!.setOnTouchListener(null)}RecyclerView.SCROLL_STATE_SETTLING ->{//结算时的TODO,阻止touch,更新即将被关注的页面Log.d("AppLog", "状态:SCROLL_STATE_SETTLING")recyclerViewStateTextView.text = "状态:SCROLL_STATE_SETTLING"//recyclerView!!.setOnTouchListener(object : View.OnTouchListener {//覆盖 fun onTouch(v: View?, event: MotionEvent?): Boolean {//返回真/
沃梦达教程
本文标题为:如何在解决 RecyclerView 和 ViewPager 上的捕捉功能时获得即将被选中的页面
猜你喜欢
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- URL编码Swift iOS 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- UITextView 内容插图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01