Android 如何在Kotlin中将listView转换为RecyclerView

Android 如何在Kotlin中将listView转换为RecyclerView,android,listview,kotlin,android-recyclerview,Android,Listview,Kotlin,Android Recyclerview,我在使用listView时遇到问题,因为我在listView中显示了许多图像(关于内存) 我需要将listview转换为recyclerview 问题: 当我在手机上显示图像时,使用上一个视图,你会进行大量内存阻塞, 它导致应用程序停止,所以我想将列表视图转换为RecycleView 主要活动: package com.masreta87.hussain import android.app.Activity import android.content.pm.PackageManag

我在使用listView时遇到问题,因为我在listView中显示了许多图像(关于内存)

我需要将listview转换为recyclerview

问题:

当我在手机上显示图像时,使用上一个视图,你会进行大量内存阻塞, 它导致应用程序停止,所以我想将列表视图转换为RecycleView

主要活动:

    package com.masreta87.hussain

import android.app.Activity
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.ListView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.get
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
import java.lang.Exception
import java.util.jar.Manifest
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.util.Log
import kotlinx.android.synthetic.*


class MainActivity : AppCompatActivity() {
    lateinit var Img:ListView
    lateinit var ImgArr:ArrayList<DataImg>

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

         Img=findViewById(R.id.listviewB)
         ImgArr=ArrayList()

        ImgArr.add(DataImg(R.drawable.h))
        ImgArr.add(DataImg(R.drawable.hh))
        ImgArr.add(DataImg(R.drawable.hhh))
        ImgArr.add(DataImg(R.drawable.v2))
        ImgArr.add(DataImg(R.drawable.v3))
        ImgArr.add(DataImg(R.drawable.v4))
        ImgArr.add(DataImg(R.drawable.v5))
        ImgArr.add(DataImg(R.drawable.v6))
        //ImgArr.add(DataImg(R.drawable.v7))
        ImgArr.add(DataImg(R.drawable.v8))
        ImgArr.add(DataImg(R.drawable.v9))

        Img.adapter=customAdapter(applicationContext,ImgArr)
        Img.setOnItemClickListener{parent:AdapterView<*>, view:View, position:Int, id:Long ->
         var ImgNa=ImgArr.get(position).image.toString()

            Log.d("this",ImgNa)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE )
                    !=PackageManager.PERMISSION_GRANTED){

                    ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        ,100)

                }else{
                    saveImageToStorage(ImgNa)
                }
            }else{
                saveImageToStorage(ImgNa)
            }

        }


    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (requestCode==100){
            if (grantResults.isNotEmpty() && grantResults[0]==PackageManager.PERMISSION_GRANTED){

               // saveImageToStorage(ImgNa)
            }else{
                Toast.makeText(this,"No Permission for save",Toast.LENGTH_SHORT).show()

            }
        }
    }
    public fun saveImageToStorage(ImgNa:String){

        val externalStorageState=Environment.getExternalStorageState()
        if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
            val storeDirectory=Environment.getExternalStorageDirectory().toString()
            val rnds = (1000..100000000000).random().toString()
            val file=File(storeDirectory,"$rnds.png")//Namephoto
            try {

                Log.d("output",ImgNa)
                val stream:OutputStream =FileOutputStream(file)
                var drawable=ContextCompat.getDrawable(applicationContext,ImgNa.toInt()) //edit
                var bitmap=(drawable as BitmapDrawable).bitmap
                bitmap.compress(Bitmap.CompressFormat.PNG,100,stream)
                stream.flush()
                stream.close()
                Toast.makeText(this,"Image Saved",Toast.LENGTH_LONG).show()
            }catch (e:Exception){
                e.printStackTrace()
            }

        }else{
            Toast.makeText(this,"unable to access the storage",Toast.LENGTH_SHORT).show()
        }
    }
}
activity_Main.xml:

<?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=".MainActivity">


    <ListView
        android:id="@+id/listviewB"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>
<?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=".MainActivity">


    <RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

listview_item.xml

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

    <ImageView
        android:id="@+id/imageViewN"
        android:background="@drawable/image_border"
        android:layout_width="match_parent"
        android:scaleType="fitXY"
        android:layout_height="200dp"

        />
</LinearLayout>

第一次更改您的活动\u Main.xml:

<?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=".MainActivity">


    <ListView
        android:id="@+id/listviewB"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>
<?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=".MainActivity">


    <RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

第一次更改您的活动\u Main.xml:

<?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=".MainActivity">


    <ListView
        android:id="@+id/listviewB"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>
<?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=".MainActivity">


    <RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

您只需要创建recyclerview适配器而不是listview适配器,并使用与本例中相同的视图,并使用activityAlternative方法进行设置……我建议您使用glide图像库,它将缓存内存中的可绘制图像。您不会从内存中得到任何错误您只需要创建recyclerview适配器而不是listview适配器,并使用与本例中相同的视图,并使用activityAlternative方法进行设置……我建议您使用滑动图像库,它将缓存内存中的可绘制图像。您不会从memoryrecyclerView.layoutManager=LinearLayoutManager(上下文,LinearLayoutManager.VERTICAL,false)recyclerViewSearchList.adapter=CustomAdapter(MainActivity.this,ImgArr)中得到任何错误此处出错确保您正在编写CustomAdapter而不是CustomAdapter,使用recyclerViewSearchList=findViewById(R.id.recycler\u视图)recyclerview.layoutManager=LinearLayoutManager(上下文,LinearLayoutManager.VERTICAL,false)初始化recyclerview//此处错误recyclerView元素??recyclerView.layoutManager=LinearLayoutManager(上下文,LinearLayoutManager.VERTICAL,false)recyclerViewSearchList.adapter=CustomAdapter(MainActivity.this,ImgArr)此处出错确保您正在编写CustomAdapter而不是CustomAdapter,使用recyclerViewSearchList=findViewById(R.id.recycler\u视图)recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)初始化recyclerview//此处错误recyclerview元素是什么??