我有一个持有两种不同观点的RecyclerView
with an 。Adapter
第一个视图的宽度和高度比后面的视图稍大。
启动包含 的活动时RecyclerView
,我想滚动到Adapter
. 我通过调用scrollToPositionWithOffset(position, offset)
连接LinearLayoutManager
到RecyclerView
.
以下代码大致描述了设置(没有什么异常)。包含RecyclerView
调用的活动Timeline.scrollTo(position)
。请耐心等待我发布 Scala 代码。
举办活动RecyclerView
class Activity extends [...] {
lazy val timeline = findView( TR.layout_screen_transaction_timeline )
override def onCreate( state: Option[Bundle] ) = {
super.onCreate( state )
setContentView( R.layout.screen_transaction_timeline )
[...]
timeline.setAdapter( adapter )
if ( state.isEmpty ) {
focus.foreach { position ⇒
new Handler().post { () ⇒
timeline.scrollTo( position )
}
}
}
}
带有 LinearLayoutManager 的 RecyclerView
class Timeline( attributes: AttributeSet = null, style: Int = 0 )( implicit context: Context )
extends RecyclerView( context, attributes, style ) {
[...]
setHasFixedSize( true )
setLayoutManager( layoutManager )
def scrollTo( position: Int ) = {
layoutManager.scrollToPositionWithOffset( position, 0 )
}
object layoutManager extends LinearLayoutManager( context, VERTICAL, false )
}
适配器
object adapter extends Adapter[Holder] {
override def getItemCount = tour.shipments.length + 1
override def onBindViewHolder( holder: Holder, position: Int ) = holder.view match {
case collection: widget.transaction.timeline.Header ⇒
collection.set( tour.collection, tour, tour.state == Tour.State.New )
case waypoint: widget.transaction.timeline.Waypoint ⇒
waypoint.set( position, tour.shipments( position - 1 ), focus.contains( position ) )
}
override def onCreateViewHolder( parent: ViewGroup, kind: Int ) = Holder {
val view = kind match {
case 0 ⇒ new widget.transaction.timeline.Header()
case 1 ⇒ new widget.transaction.timeline.Waypoint()
}
view.setLayoutParams( new RecyclerView.LayoutParams( MATCH_PARENT, WRAP_CONTENT ) )
view
}
override def getItemViewType( position: Int ) = position match {
case 0 ⇒ 0
case _ ⇒ 1
}
}
一切正常,除非我想滚动到最后一个位置。在这种情况下,LinearLayoutManager
有时会正确滚动到该位置,有时会以稍微偏离的方式滚动。
更新:我刚刚发现删除包含ActionBar
的 Activity 可以RecyclerView
解决此问题。不过我需要一个ActionBar
...