Skip to content

Commit 33b7e46

Browse files
authored
Merge pull request #109 from priyanka0906/sample_for_coil-kt
RecyclerView
2 parents 27f8e8e + de8daff commit 33b7e46

File tree

13 files changed

+222
-5
lines changed

13 files changed

+222
-5
lines changed

android/src/main/AndroidManifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="playground.android">
3+
package="playground.android">
44

55
<application
66
android:allowBackup="true"
@@ -9,11 +9,12 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/Theme.AppCompat.NoActionBar">
12-
<activity android:name="playground.android.MainActivity">
12+
<activity android:name=".MainActivity">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
1515

1616
<category android:name="android.intent.category.LAUNCHER" />
17+
1718
</intent-filter>
1819
</activity>
1920
</application>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package playground.android
2+
3+
import android.os.Bundle
4+
import androidx.fragment.app.Fragment
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
9+
10+
class CoilFragment : Fragment() {
11+
12+
13+
override fun onCreateView(
14+
inflater: LayoutInflater, container: ViewGroup?,
15+
savedInstanceState: Bundle?
16+
): View? {
17+
// Inflate the layout for this fragment
18+
return inflater.inflate(R.layout.fragment_coil, container, false)
19+
}
20+
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package playground.android
2+
3+
import android.os.Bundle
4+
import androidx.fragment.app.Fragment
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
9+
10+
class ContourFragment : Fragment() {
11+
12+
override fun onCreateView(
13+
inflater: LayoutInflater, container: ViewGroup?,
14+
savedInstanceState: Bundle?
15+
): View? {
16+
17+
return MainView(requireContext())
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package playground.android
2+
3+
import android.content.Context
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.widget.TextView
8+
import androidx.appcompat.app.AppCompatActivity
9+
import androidx.fragment.app.Fragment
10+
import androidx.fragment.app.FragmentTransaction
11+
import androidx.recyclerview.widget.RecyclerView
12+
13+
14+
15+
class LibraryAdapter(private val context: Context?, private val libraries: List<LibraryName>):RecyclerView.Adapter<LibraryAdapter.MyViewHolder>() {
16+
inner class MyViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
17+
val name:TextView = itemView.findViewById(R.id.name_txt)
18+
fun setData(library: LibraryName, position: Int) {
19+
name.text = library.name
20+
}
21+
}
22+
23+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
24+
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item, parent, false)
25+
return MyViewHolder(view)
26+
}
27+
28+
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
29+
val library = libraries[position]
30+
holder.setData(library, position)
31+
holder.itemView.setOnClickListener {
32+
33+
val activity = context as AppCompatActivity
34+
activity.supportFragmentManager.beginTransaction().replace(R.id.main_fragment, library.fragment).addToBackStack(null).commit()
35+
}
36+
}
37+
38+
39+
40+
41+
42+
43+
override fun getItemCount(): Int {
44+
return libraries.size
45+
}
46+
}
47+

android/src/main/java/playground/android/MainActivity.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ package playground.android
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5-
5+
import androidx.fragment.app.Fragment
6+
import androidx.fragment.app.FragmentTransaction
7+
import androidx.recyclerview.widget.RecyclerView
68

79
class MainActivity : AppCompatActivity() {
810
override fun onCreate(savedInstanceState: Bundle?) {
911
super.onCreate(savedInstanceState)
10-
setContentView(MainView(this))
11-
12+
setContentView(R.layout.activity_main)
13+
val ft: FragmentTransaction =supportFragmentManager.beginTransaction()
14+
val recyclerViewFragment = RecyclerViewFragment(this)
15+
ft.replace(R.id.main_fragment,recyclerViewFragment)
16+
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
17+
ft.addToBackStack(null)
18+
ft.commit()
1219
}
20+
1321
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package playground.android
2+
3+
import android.content.Context
4+
import android.os.Bundle
5+
import androidx.fragment.app.Fragment
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import android.view.ViewGroup
9+
import androidx.fragment.app.FragmentTransaction
10+
import androidx.recyclerview.widget.RecyclerView
11+
12+
13+
class RecyclerViewFragment(context:Context) : Fragment() {
14+
override fun onCreateView(
15+
inflater: LayoutInflater, container: ViewGroup?,
16+
savedInstanceState: Bundle?
17+
): View? {
18+
return inflater.inflate(R.layout.fragment_recycler_view, container, false)
19+
}
20+
21+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
22+
super.onViewCreated(view, savedInstanceState)
23+
val recyclerView : RecyclerView = view.findViewById(R.id.recyclerView)
24+
val adapter = LibraryAdapter(context,Suppliers.libraries)
25+
recyclerView.adapter = adapter
26+
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package playground.android
2+
3+
import android.content.Context
4+
import android.content.Intent
5+
import androidx.fragment.app.Fragment
6+
7+
data class LibraryName(var name:String, var fragment:Fragment)
8+
9+
10+
object Suppliers{
11+
val libraries = listOf(LibraryName("Contour",ContourFragment()),
12+
LibraryName("Coil" , CoilFragment())
13+
14+
15+
)
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.fragment.app.FragmentContainerView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:id="@+id/main_fragment"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
8+
/>
9+
10+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
tools:context=".CoilFragment">
7+
8+
9+
<TextView
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:text="@string/hello_blank_fragment" />
13+
14+
</FrameLayout>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
tools:context=".ContourFragment">
7+
8+
<!-- TODO: Update blank fragment layout -->
9+
<TextView
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:text="@string/hello_blank_fragment" />
13+
14+
</FrameLayout>

0 commit comments

Comments
 (0)