作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用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.
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()
}
}
}
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()
}
}
package com.masreta87.hussain
class DataImg(var image:Int) {
}
<?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"?>
<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>
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!!) {
}
}
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/
我是一名优秀的程序员,十分优秀!