gpt4 book ai didi

android - 如何在Kotlin中将listView转换为RecyclerView

转载 作者:行者123 更新时间:2023-12-02 13:44:59 32 4
gpt4 key购买 nike

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

我需要将listview转换为recyclerview

问题:

When I display the images, on my phone, using last view, you do a lot of memory blocking, And it caused the application to stop, so I want to convert List View to RecycleView.



主要 Activity :
    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()
}
}
}

和customAdapter
package com.masreta87.hussain

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView

class customAdapter(var context: Context,var data:ArrayList<DataImg>) :BaseAdapter(){
private class ViewHolder(row:View?){
var ivImage:ImageView
init {
this.ivImage=row?.findViewById(R.id.imageViewN) as ImageView


}
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var view:View?
var viewHolder:ViewHolder
if(convertView==null){
var layout=LayoutInflater.from(context)
view=layout.inflate(R.layout.listview_item,parent,false)
viewHolder=ViewHolder(view)
view.tag=viewHolder

}else{
view=convertView
viewHolder=view.tag as ViewHolder
}
var data:DataImg=getItem(position) as DataImg

viewHolder.ivImage.setImageResource(data.image)
return view as View
}

override fun getItem(position: Int): Any {
return data.get(position)
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

override fun getCount(): Int {
return data.count()
}

}

和DataImg:
package com.masreta87.hussain

class DataImg(var image:Int) {

}

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>

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>

最佳答案

第一个更改您的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">


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

然后,您必须像这样为RecyclerView创建一个适配器:
class CustomAdapter(private val activity: Context?, private val arrData: List<DataImg>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
var itemView: View? = null
itemView = LayoutInflater.from(activity)
.inflate(R.layout.listview_item, parent, false)
return ViewHolder(itemView)
}

override fun onBindViewHolder(holder: ViewHolder, i: Int) {
val model = arrData[i]
}


override fun getItemCount(): Int {
return arrData.size
}

class ViewHolder(view: View?) : RecyclerView.ViewHolder(view!!) {

}

}

现在,在您的主要 Activity 中,您必须为RecyclerView设置Layout Manager,并必须将Adapter与之绑​​定
recyclerViewSearchList = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
recyclerViewSearchList.adapter = CustomAdapter(MainActivity.this, ImgArr)

关于android - 如何在Kotlin中将listView转换为RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59659422/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com