哈尔滨模板建站定制网站,ppt做视频的模板下载网站有哪些内容,南宁网站规划与网页设计,net网站是国际域名吗之前一直没注意 SnapHelper 辅助类的功能#xff0c;去年的时候看到项目中仅通过俩行代码设置 RecyclerView 后就提升了用户体验#xff0c;觉得还是很有必要了解一下#xff0c;尝试过后才发现其 PagerSnapHelper、LinearSnapHelper 子类可以作用于不同场景#xff0c;且听… 之前一直没注意 SnapHelper 辅助类的功能去年的时候看到项目中仅通过俩行代码设置 RecyclerView 后就提升了用户体验觉得还是很有必要了解一下尝试过后才发现其 PagerSnapHelper、LinearSnapHelper 子类可以作用于不同场景且听吾言 RecyclerView基础
Android进阶之路 - RecyclerView基础使用17年Android进阶之路 - RecyclerView实现横、纵向滑动列表19年Android基础进阶 - RecyclerView列表加载多类型视图
RecyclerView扩展
Android进阶之路 - RecyclerView加载多类型视图ConcatAdapter到底有没有学习必要Android进阶之路 - RecyclerView停止滑动后Item自动居中SnapHelper辅助类
RecyclerView相关功能
Android进阶之路 - RecyclerView左划删除SwipeRecyclerView的简单使用 17年Android进阶之路 - RecyclerView列表置顶、滑动到指定条目18年Android进阶之路 - RecyclerView列表自动无限水平滚动21年 Android进阶之路 - 双列表联动效果18年 他字字未提喜欢你你句句都是我愿意 基础了解实践检验前置 ItemView前置 Adapter使用方式 你在开发项目中遇到过这样的场景吗 HintRecyclerView 为水平滑动 子ItemView 宽度非 match_parent支持同屏展示多个ItemView 用户滑动列表时产生类似 ViewPager 效果停止滑动后ItemView 自动居中一般正常速度滑动只滑动一条数据但是当滑动速度加快比较费力时可能会滑动多条数据用户正常速度滑动列表时可更轻易的滑动多条数据停止滑动后子ItemView自动居中
Look效果如果以下效果不能完全满足也可以自定义SnapHelper然后参考其子类实现增添部分你需要的业务功能例如修改滑动速度等 Tip核心方法仅有俩行如急于开发亦可直接使用或直接看实践检验等有时间再来一同了解 创建对应的 SnapHelper 后通过 attachToRecyclerView 关联 RecyclerView 即可
PagerSnapHelper val pagerSnapHelper PagerSnapHelper()pagerSnapHelper.attachToRecyclerView(mRvPager)LinearSnapHelper val lineaSnapHelper LinearSnapHelper()lineaSnapHelper.attachToRecyclerView(mRvLinear)基础了解
SnapHelper自身为抽象类同时继承了RecyclerView.OnFlingListener内部实现了一些通用基类方法you俩个实现子类通过重写其中部分方法从而达到对应的需求效果
PagerSnapHelper类似ViewPager滑动效果仅支持单条滑动在 ViewPager控件中也可以看到PagerSnapHelper的身影LinearSnapHelp水平快速滑动列表体验丝滑当滑动停止后ItemView 自动居中 OnFlingListener 仅拥有一个抽象方法 因为我只是通过源码方法命名 参考方法注释 简单理解可能并不是很详细有兴趣的可以前往早期一位前辈写的 让你明明白白的使用RecyclerView——SnapHelper详解 通过查看 SnapHelper 内部方法简单分析一下方法作用范围仅做部分解释并不完全
支持 绑定RecyclerViewcalculateDistanceToFinalSnap 测量移动距离findSnapView 支持 定位移动的ViewfindTargetSnapPosition 支持定位移动后的数据(视图)角标FlingListener、ScrollListener 滑动监听滑动速度监听 PagerSnapHelper、LinearSnapHelper 除基类方法外支持获取居中View、布局方向等
PagerSnapHelper 源码方法 LinearSnapHelper 源码方法 如果要自定义 SnapHelper 的话需要重新以下三个抽象方法
package com.example.recyclerviewsnaphelperimport android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SnapHelperclass OurHelper : SnapHelper() {//计算最终移动距离override fun calculateDistanceToFinalSnap(layoutManager: RecyclerView.LayoutManager, targetView: View): IntArray? {TODO(Not yet implemented)}//获取移动Viewoverride fun findSnapView(layoutManager: RecyclerView.LayoutManager?): View? {TODO(Not yet implemented)}//获取移动View的角标位置override fun findTargetSnapPosition(layoutManager: RecyclerView.LayoutManager?, velocityX: Int, velocityY: Int): Int {TODO(Not yet implemented)}
}实践检验
RecyclerView 常规使用仅加入了SnapHelper.attachToRecyclerView相关绑定
前置 ItemView item_view
?xml version1.0 encodingutf-8?
androidx.appcompat.widget.LinearLayoutCompat xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_width250dpandroid:layout_height100dpandroid:paddingHorizontal5dpTextViewandroid:idid/tv_dataandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:background#f98741android:gravitycenterandroid:textItem Dataandroid:textColor#ffffffandroid:textStylebold /
/androidx.appcompat.widget.LinearLayoutCompat前置 Adapter
package com.example.recyclerviewsnaphelperimport android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerViewclass OurAdapter(private val dataList: MutableListString) : RecyclerView.AdapterOurAdapter.OurViewHolder() {override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OurViewHolder {return OurViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent,false))}override fun getItemCount(): Int {return dataList.size}override fun onBindViewHolder(holder: OurViewHolder, position: Int) {holder.itemView.findViewByIdTextView(R.id.tv_data).textdataList[position]}inner class OurViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}使用方式
package com.example.recyclerviewsnaphelperimport android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.HORIZONTALclass MainActivity : AppCompatActivity() {var dataList mutableListOfString()override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//数据模拟for (i in 0..15) {dataList.add(第${i 1}页)}//RecyclerView基础配置pagerRecyclerSetting()layoutRecyclerSetting()}/*** RecyclerView基础配置PagerSnapHelper示例* */private fun pagerRecyclerSetting() {val mRvPager findViewByIdRecyclerView(R.id.rv_pager)var layoutManager LinearLayoutManager(this)layoutManager.orientation HORIZONTALmRvPager.layoutManager layoutManagerval ourPagerAdapter OurAdapter(dataList)mRvPager.adapter ourPagerAdapter//添加SnapHelper相关辅助类val pagerSnapHelper PagerSnapHelper()pagerSnapHelper.attachToRecyclerView(mRvPager)}/*** RecyclerView基础配置LinearSnapHelper示例* */private fun layoutRecyclerSetting() {val mRvLinear findViewByIdRecyclerView(R.id.rv_linear)var layoutManager LinearLayoutManager(this)layoutManager.orientation HORIZONTALmRvLinear.layoutManager layoutManagerval ourLayoutAdapter OurAdapter(dataList)mRvLinear.adapter ourLayoutAdapter//添加SnapHelper相关辅助类val lineaSnapHelper LinearSnapHelper()lineaSnapHelper.attachToRecyclerView(mRvLinear)}
}activity_main
预览图 layout布局
?xml version1.0 encodingutf-8?
androidx.appcompat.widget.LinearLayoutCompat xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticaltools:context.MainActivityTextViewandroid:layout_widthmatch_parentandroid:layout_height40dpandroid:gravitycenterandroid:textPagerSnapHelper效果android:textStylebold /androidx.recyclerview.widget.RecyclerViewandroid:idid/rv_pagerandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalapp:layoutManagerandroidx.recyclerview.widget.LinearLayoutManagertools:itemCount10tools:listitemlayout/item_view /TextViewandroid:layout_widthmatch_parentandroid:layout_height40dpandroid:layout_marginTop50dpandroid:gravitycenterandroid:textLinearSnapHelperandroid:textStylebold /androidx.recyclerview.widget.RecyclerViewandroid:idid/rv_linearandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalapp:layoutManagerandroidx.recyclerview.widget.LinearLayoutManagertools:itemCount10tools:listitemlayout/item_view //androidx.appcompat.widget.LinearLayoutCompat
文章转载自: http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.gjmll.cn.gov.cn.gjmll.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.jbztm.cn.gov.cn.jbztm.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.kpzbf.cn.gov.cn.kpzbf.cn http://www.morning.xhfky.cn.gov.cn.xhfky.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.rgxn.cn.gov.cn.rgxn.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.rbktw.cn.gov.cn.rbktw.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.jtcq.cn.gov.cn.jtcq.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.wbysj.cn.gov.cn.wbysj.cn http://www.morning.rkdnm.cn.gov.cn.rkdnm.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.rpth.cn.gov.cn.rpth.cn http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.qmzhy.cn.gov.cn.qmzhy.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.xxwhz.cn.gov.cn.xxwhz.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.qwyms.cn.gov.cn.qwyms.cn http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn http://www.morning.dkfrd.cn.gov.cn.dkfrd.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.kcdts.cn.gov.cn.kcdts.cn http://www.morning.cgtrz.cn.gov.cn.cgtrz.cn http://www.morning.cnlmp.cn.gov.cn.cnlmp.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.vibwp.cn.gov.cn.vibwp.cn http://www.morning.lqklf.cn.gov.cn.lqklf.cn http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.slqgl.cn.gov.cn.slqgl.cn