我在RecyclerView
内使用ViewPager
,而ViewPager
在NestedScrollView
内,问题是滚动滞后,虽然不大,但并不完美。
我已经阅读了Android Document关于渲染性能的信息,而我的onBind
方法大约花费了1毫秒或更短的时间,而我的customImageView
中的setImageBitmap
舍入位图大约需要2毫秒的时间。
从文档来看,我只需要16毫秒即可绘制框架,而我的时间却远远少于16毫秒,而且我很确定其他部分不会做任何与UI相关的事情。那么我该如何发现问题并解决问题呢?
我的活动布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray_dark"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:minHeight="56dp"
app:contentScrim="@color/colorTransparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:statusBarScrim="@color/colorTransparent">
<ImageView
android:id="@+id/headerImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="@string/header_image"
android:fitsSystemWindows="true"
android:foreground="@color/gray_alpha"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
app:layout_collapseMode="pin"
app:tabIndicatorColor="@color/pink"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/pink"
app:tabTextColor="@color/white" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
和我的片段布局代码:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/recycler"
android:layout_height="match_parent"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:background="@color/white"
android:orientation="vertical">
</android.support.v7.widget.RecyclerView>
我的ViewPager适配器是如此简单,只附加片段:
class MainPagerAdapter(fm: FragmentManager, val listener: FragmentGetter) : FragmentPagerAdapter(fm) {
private val arr = SparseArray<Fragment>()
override fun getItem(position: Int): Fragment {
return when (position) {
0 -> {
if (arr[0] == null) arr.append(0, listener.getBeshnoFragment());arr[0]
}
1 -> {
if (arr[1] == null) arr.append(1, listener.getPlaylistFragment()); arr[1]
}
2 -> {
if (arr[2] == null) arr.append(2, listener.getMusicFragment())
(arr[2] as MusicFragment).load(ALL_MUSICS, null)
arr[2]
}
3 -> {
if (arr[3] == null) arr.append(3, listener.getAlbumFragment()); arr[3]
}
4 -> {
if (arr[4] == null) arr.append(4, listener.getArtistFragment()); arr[4]
}
else -> {
if (arr[5] == null) arr.append(5, listener.getFoldersFragment()); arr[5]
}
}
}
override fun getPageTitle(position: Int): CharSequence? {
return listener.getPageTitle(position)
}
override fun getCount(): Int {
return TabLayoutData.items.size
}
interface FragmentGetter {
fun getBeshnoFragment(): BeshnoFragment
fun getMusicFragment(): MusicFragment
fun getAlbumFragment(): AlbumFragment
fun getArtistFragment(): ArtistFragment
fun getPlaylistFragment(): PlaylistFragment
fun getFoldersFragment(): FoldersFragment
fun getPageTitle(pos: Int): String
}
}
这是我的RecyclerView适配器代码:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val holder = ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.music_list_item, parent, false
)
)
holder.menu.setOnClickListener {
val pos = holder.adapterPosition
if (menu != null) menu!!.dismiss()
mCurrentMusic = musics[pos]
menu = PopupMenu(holder.itemView.context, holder.menu)
buildMenu()
menu!!.show()
}
holder.itemView.setOnClickListener {
val pos = holder.adapterPosition
listener.playMusic(musics[pos])
}
holder.image.radius = UIUtils.convertDpToPixel(8f, parent.context).toInt()
return holder
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val t1 = System.currentTimeMillis()
val music = musics[position]
holder.name.text = music.title
holder.album.text = music.album
val path = music.imagePath
MusicUtils.setMusicImageAsync(path, holder.image, MusicUtils.QUALITY_MED)
Log.d(javaClass.simpleName, "adapter bind time: ${System.currentTimeMillis() - t1}")//this time takes less than 1 miliseconds
}
MusicUtils.setMusicImageAsync
代码是:
fun setMusicImageAsync(album_ART: String?, cover: ImageView, quality: Int) {
if (album_ART == null || album_ART.isEmpty() || !File(album_ART).exists()) {
setPlaceHolder(cover)
return
}
ImageLoaderTask(quality) { // on post execute
if (it == null) {
setPlaceHolder(cover)
} else {
val t1 = System.currentTimeMillis()
cover.setImageBitmap(it)
cover.setPadding(0, 0, 0, 0)
Log.d("MusicImageLoader", "time is: ${System.currentTimeMillis() - t1}") //this time takes about 2 miliseconds
}
}.execute(album_ART)
}
我还为RecyclerView
使用了绘图缓存,但滚动滞后。
答案 0 :(得分:0)
我认为这可以帮助您
recyclerView.setNestedScrollingEnabled(false)