我正在开发我的第一个Android应用程序,并实现了RecyclerView来滚动某些项目。 我遵循了以下Youtube Tutorial。
我正在使用华为P20 Pro(物理设备)测试程序,但我注意到在滚动浏览项目时出现闪烁的线条。 (在模拟器上,使用Google Pixel 3,没有明显的闪烁)
White flickering line that appears during scrolling
我将适配器实现为:
package com.example.recyclerviewexample
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.test_3.R
import kotlinx.android.synthetic.main.example_item.view.*
class ExampleAdapter(private val exampleList: List<ExampleItem>) : RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.example_item,
parent,
false)
return ExampleViewHolder(itemView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textView1.text = currentItem.text1
holder.textView2.text = currentItem.text2
}
override fun getItemCount() = exampleList.size
class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.image_view //image_view is the id of the image! (same: itemView.findViewById(R.id.image_view))
val textView1: TextView = itemView.text_view_1
val textView2: TextView = itemView.text_view_2
}
}
此外,我将我的代码上传到了Github,因此您可以自己尝试一下。 由于我是android studio的新手,所以我真的不知道发生了什么,非常感谢您的帮助。 谢谢:)
答案 0 :(得分:1)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".MaliActivity">
<include
android:id="@+id/toolbar_main"
layout="@layout/toolbar_mali"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_below="@+id/toolbar_main"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/search_list"
android:layout_width="399dp"
android:layout_height="633dp"
android:layout_below="@+id/searchView"
android:layout_margin="5dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="331dp"
android:layout_height="53dp"
android:layout_alignParentRight="true"
android:padding="10dp"
app:queryHint="Search for a resource ">
</androidx.appcompat.widget.SearchView>
</RelativeLayout>
答案 1 :(得分:0)
检出布局文件。布局高度属性上的match_parent
。将其更改为wrap_content
。
就我而言,问题在于recyclerview
中的每个项目都占据了整个屏幕,这很有帮助。对你来说可能一样。