Implement image viewing with static member

This commit is contained in:
Dendy 2022-02-22 22:13:01 +00:00
parent dec94b0011
commit 36af732a9f
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
5 changed files with 23 additions and 0 deletions

View File

@ -12,6 +12,11 @@ data class BooruImage (
var file_url: String,
var preview_url: String,
) {
fun clone(): BooruImage {
return BooruImage(id, rating, tags, file_url, preview_url)
}
companion object {
@Throws(JSONException::class)
fun imageFromJson(json: JSONObject): BooruImage {

View File

@ -54,14 +54,18 @@ class ImageGridFragment(val images: List<BooruImage>) : Fragment() {
.placeholder(ContextCompat.getDrawable(context, R.drawable.ic_launcher_foreground))
.into(holder.imageview)
holder.image = imageList[position]
holder.imageview.setOnClickListener(holder)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
var imageview: ImageView = itemView.findViewById(R.id.image)
lateinit var image: BooruImage
override fun onClick(view: View?) {
val intent = Intent(view?.context, ImageViewerActivity::class.java)
ImageViewerActivity.image = this.image
startActivity(intent)
}
}

View File

@ -3,15 +3,27 @@ package com.example.testes
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.example.testes.databinding.ActivityImageViewerBinding
import com.example.testes.databinding.ActivityMainBinding
class ImageViewerActivity : AppCompatActivity() {
lateinit var binding: ActivityImageViewerBinding
lateinit var local_image: BooruImage
companion object {
lateinit var image: BooruImage
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityImageViewerBinding.inflate(layoutInflater)
setContentView(binding.root)
local_image = image.clone()
Glide.with(this)
.load(local_image.file_url)
.placeholder(ContextCompat.getDrawable(this, R.drawable.ic_launcher_foreground))
.into(binding.image)
}
}

View File

@ -76,6 +76,7 @@ class MainActivity : AppCompatActivity() {
private fun loadGrid(images: List<BooruImage>) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.imageGridRecV, ImageGridFragment(images))
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}

View File

@ -7,6 +7,7 @@
tools:context=".ImageViewerActivity">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"