Implement image main grid prototype

This commit is contained in:
Dusk 2022-02-08 23:38:09 +01:00
parent 3fb4990709
commit 1b64e7a043
7 changed files with 90 additions and 51 deletions

View File

@ -5,6 +5,7 @@
<map>
<entry key="app/src/main/res/layout/activity_main.xml" value="0.33" />
<entry key="app/src/main/res/layout/fragment_image_grid.xml" value="0.2515625" />
<entry key="app/src/main/res/layout/image_grid_item.xml" value="0.35052083333333334" />
</map>
</option>
</component>

View File

@ -0,0 +1,35 @@
package com.example.testes
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class ImageGridAdapter(val imageList: List<BooruImage>) :
RecyclerView.Adapter<ImageGridAdapter.ViewHolder>() {
/* Creates and inflates view and return FlowerViewHolder. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.image_grid_item, parent, false)
return ViewHolder(view)
}
/* Gets current flower and uses it to bind view. */
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(imageList[position])
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val text: TextView = itemView.findViewById(R.id.text)
//private val image: ImageView = itemView.findViewById(R.id.image)
fun bind(image: BooruImage) {
text.text = image.url
}
}
override fun getItemCount(): Int = imageList.size
}

View File

@ -5,28 +5,13 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [ImageGridFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ImageGridFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
class ImageGridFragment(val images: List<BooruImage>) : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
@ -34,26 +19,12 @@ class ImageGridFragment : Fragment() {
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_image_grid, container, false)
}
val layoutView = inflater.inflate(R.layout.fragment_image_grid, container, false)
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ImageGridFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
ImageGridFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
val recyclerView : RecyclerView = layoutView.findViewById(R.id.imageGridRecV)
recyclerView.adapter = ImageGridAdapter(images)
recyclerView.layoutManager = GridLayoutManager(layoutView.context, 3)
return layoutView
}
}

View File

@ -4,6 +4,7 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.testes.databinding.ActivityMainBinding
import org.json.JSONArray
import org.json.JSONException
@ -17,7 +18,9 @@ class MainActivity : AppCompatActivity() {
try {
val array: JSONArray = json.getJSONArray("post")
val list = BooruImage.listFromJsonArray(array)
Log.i("Image", list[5].toString())
loadGrid(list)
//Log.i("Image", list.toString())
}
catch (e: JSONException) {
e.printStackTrace()
@ -36,4 +39,11 @@ class MainActivity : AppCompatActivity() {
::imageListFromJson
)
}
private fun loadGrid(images: List<BooruImage>) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.add(R.id.imageGridRecV, ImageGridFragment(images))
fragmentTransaction.commit()
}
}

View File

@ -6,14 +6,9 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/imageGridRecV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -3,12 +3,13 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ImageGridFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/imageGridRecV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
/>
</FrameLayout>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher_round"
android:contentDescription="Image"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HOLAAA"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textAlignment="center"
/>
</androidx.constraintlayout.widget.ConstraintLayout>