[*] Проблема производительности: Начальное создание (настройка, макет и популяция данных) занимает ~ 100 мс, и мне интересно, есть ли оптимизации, которые я могу применить, чтобы улучшить это время.class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: EditTextAdapter
companion object {
init {
System.loadLibrary("recyclerviewtest")
}
external fun nativeGetItemCount(): Int
external fun nativeGetStringProperty(index: Int, propId: Int): String
external fun nativeGetIntProperty(index: Int, propId: Int): Int
external fun nativeGetFloatProperty(index: Int, propId: Int): Float
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val start = System.nanoTime()
recyclerView = RecyclerView(this).apply {
layoutManager = LinearLayoutManager(this@MainActivity)
}
adapter = EditTextAdapter(this)
recyclerView.adapter = adapter
val rootLayout = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
addView(recyclerView, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f))
}
setContentView(rootLayout)
// Initialize data and trigger first data fetch
adapter.setInitialData(nativeGetItemCount())
val durationMs = (System.nanoTime() - start) / 1_000_000.0
Log.d("Performance", "Create ALL duration (including layout): $durationMs ms")
}
< /code>
} < /p>
class EditTextAdapter(private val context: Context) : RecyclerView.Adapter() {
private val items = mutableListOf()
fun setInitialData(count: Int) {
items.clear()
for (i in 0 until count) {
items.add(loadProps(i)) // Load data from JNI
}
notifyItemRangeInserted(0, items.size)
}
private fun loadProps(index: Int): ItemProps {
return ItemProps(
text = MainActivity.nativeGetStringProperty(index, 0),
textColor = MainActivity.nativeGetIntProperty(index, 1),
paddingTop = MainActivity.nativeGetIntProperty(index, 2),
paddingLeft = MainActivity.nativeGetIntProperty(index, 3),
fontSize = MainActivity.nativeGetFloatProperty(index, 4),
backgroundColor = MainActivity.nativeGetIntProperty(index, 5)
)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EditTextViewHolder {
return EditTextViewHolder(context)
}
override fun onBindViewHolder(holder: EditTextViewHolder, position: Int) {
holder.bind(items[position])
}
override fun getItemCount(): Int = items.size
< /code>
} < /p>
Performance Observations:
- The initial creation of the RecyclerView (with all EditText views) takes around 100ms to complete.
- Data is fetched from JNI for each EditText item during the initial population of the RecyclerView.
- Is ~100ms for the initial creation and population of a RecyclerView with hundreds of EditText views considered normal?
- What optimizations can be applied to speed up the initial setup?
Подробнее здесь: https://stackoverflow.com/questions/797 ... -or-can-it