Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const val IMAGES_KEY = "imageList"

class ImageDisplayFragment : Fragment() {

lateinit var someVar : Array<Int>

private lateinit var images: IntArray

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -40,17 +42,17 @@ class ImageDisplayFragment : Fragment() {
// The recycler view is the root element of the Fragment's layout
// as such the view argument passed to onViewCreated() is the RecyclerView
with (view as RecyclerView) {
adapter = CustomRecyclerAdapter(images)
if (::someVar.isInitialized) adapter = CustomRecyclerAdapter(images)
layoutManager = GridLayoutManager(requireContext(), 2)
}
}

companion object {
fun newInstance(images: IntArray) =
ImageDisplayFragment().apply {
arguments = Bundle().apply {
putIntArray(IMAGES_KEY, images)
}
fun setImages(newImages : IntArray) {
images = newImages

view?.run {
(this as RecyclerView).adapter = CustomRecyclerAdapter(newImages)
}
}

}
}
16 changes: 16 additions & 0 deletions app/src/main/java/edu/temple/inclassactivity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@ package edu.temple.inclassactivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {

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



// Fetch images into IntArray called imageArray
val typedArray = resources.obtainTypedArray(R.array.image_ids)
val imageArray = IntArray(typedArray.length()) {typedArray.getResourceId(it, 0)}
typedArray.recycle()

findViewById<Button>(R.id.button).setOnClickListener {
(supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as ImageDisplayFragment).setImages(
imageArray)
}


// Attach an instance of ImageDisplayFragment using factory method
//val imageDisplayFragment = ImageDisplayFragment.newInstance(imageArray)

/*supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainerView, imageDisplayFragment)
.commit()
*/

}
}
20 changes: 20 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,24 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="edu.temple.inclassactivity.ImageDisplayFragment"
android:layout_width="363dp"
android:layout_height="457dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="158dp"
tools:layout_editor_absoluteY="592dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_image_display.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImageDisplayFragment" />