Android RecyclerView measureTimeMillis Glide GridLayoutManager,kotlin

Android RecyclerView measureTimeMillis Glide GridLayoutManager,kotlin

implementation 'com.github.bumptech.glide:glide:4.15.1'

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {
    companion object {
        val TAG = "fly"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView: RecyclerView = findViewById(R.id.recyclerview)
        val layoutManager = GridLayoutManager(this, 5)
        recyclerView.setLayoutManager(layoutManager)
        var photoAdapter = PhotoAdapter(this)
        recyclerView.setAdapter(photoAdapter)

        //recyclerView.setHasFixedSize(true)
        //recyclerView.setItemViewCacheSize(100)
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlin.system.measureTimeMillis

class PhotoAdapter(private val context: Context) : RecyclerView.Adapter<PhotoHolder>() {

    private fun createView(parent: ViewGroup): View {
        var view: View?
        val time = measureTimeMillis {
            view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
        }
        Log.d(MainActivity.TAG, "$time @createView")
        return view!!
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoHolder {
        var holder: PhotoHolder?

        val time = measureTimeMillis {
            holder = PhotoHolder(createView(parent))
        }
        Log.d(MainActivity.TAG, "$time @onCreateViewHolder")

        return holder!!
    }

    override fun onBindViewHolder(holder: PhotoHolder, pos: Int) {
        val time = measureTimeMillis {
            bindHolder(holder, pos)
        }
        Log.d(MainActivity.TAG, "$time @onBindViewHolder")
    }

    private fun bindHolder(holder: PhotoHolder, pos: Int) {
        var resId: Int?
        when (pos % 2) {
            0 -> {
                resId = R.drawable.ic_launcher_foreground
            }

            else -> {
                resId = R.drawable.ic_launcher_background
            }
        }

        Glide.with(context).load(resId).into(holder.image)
        //holder.image.setImageResource(resId)
        holder.pos.setText("$pos")
        holder.text.setText("-$pos-")
    }

    override fun getItemCount(): Int {
        return Int.MAX_VALUE
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="1dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:id="@+id/pos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="10dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="10dp" />

    <View
        android:layout_width="20dp"
        android:layout_height="1px"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10dp"
        android:background="@color/cardview_dark_background" />
</LinearLayout>

Android RecyclerView paging/pager/page implements based on AsyncListUtil,kotlin_zhangphil的博客-CSDN博客基于Android官方Paging Library的RecyclerView分页加载框架我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。基于Android官方Paging Library的RecyclerView分页加载框架我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。【代码】Android Paging 3,kotlin(1)https://blog.csdn.net/zhangphil/article/details/130874429

Android官方ORM数据库Room技术解决方案简介(一)_zhangphil的博客-CSDN博客是时候该忘记Android SQLite了!Android官方ORM数据库Room技术解决方案简介(一)Android的Romm是Android官方整合出来的一揽子ORM数据库解决方案。Android Room和历史上的ORM数据库如Android ORMLite(见附录文章1,2),Android greenDao等等,有诸多相同的设计思想和理念,但Android Room同时吸收众家ORM数据https://blog.csdn.net/zhangphil/article/details/78611632

Android官方ORM数据库Room技术解决方案:@Embedded内嵌对象(二)_room 嵌套对象_zhangphil的博客-CSDN博客Android官方ORM数据库Room技术解决方案:@Embedded内嵌对象(二)(一)附录1简介了Android Room的基本使用。在附录1例子中,User对象元素均为普通的Java基本数据类型,但是实际的开发中,通常建立的持久化存储对象复杂,且通常是结构化的Java对象,互相之间存在引用或者内嵌关系。Android Room支持数据库表Java对象通过注解符@Embedded内嵌一个Javhttps://blog.csdn.net/zhangphil/article/details/78621009

Android Room联合AsyncListUtil实现RecyclerView分页加载ORM数据_zhangphil的博客-CSDN博客Android Room联合AsyncListUtil实现RecyclerView分页加载ORM数据我之前写了一系列关于AsyncListUtil实现RecyclerView和ListView的分页加载机制和技术路线,见附录文章4,5。同时也写了一些列文章介绍Android官方推出的ORM数据库:Room技术,见附录文章1,2。现在结合Android分页加载框架AsyncListUtil,以及Anhttps://blog.csdn.net/zhangphil/article/details/78661838

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/131051138