Skip to content

Commit de8daff

Browse files
author
priyanka0906
committed
Fragments implemented
1 parent 5c8147c commit de8daff

15 files changed

+159
-88
lines changed

android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/Theme.AppCompat.NoActionBar">
12-
<activity android:name=".CoilSample"></activity>
13-
<activity android:name=".ContourSample" />
1412
<activity android:name=".MainActivity">
1513
<intent-filter>
1614
<action android:name="android.intent.action.MAIN" />
1715

1816
<category android:name="android.intent.category.LAUNCHER" />
17+
1918
</intent-filter>
2019
</activity>
2120
</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+
}

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

Lines changed: 0 additions & 11 deletions
This file was deleted.
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+
}

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

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,47 @@
11
package playground.android
22

33
import android.content.Context
4-
import android.content.Intent
54
import android.view.LayoutInflater
65
import android.view.View
76
import android.view.ViewGroup
87
import android.widget.TextView
9-
import androidx.core.content.ContextCompat.startActivity
8+
import androidx.appcompat.app.AppCompatActivity
9+
import androidx.fragment.app.Fragment
10+
import androidx.fragment.app.FragmentTransaction
1011
import androidx.recyclerview.widget.RecyclerView
1112

12-
class LibraryAdapter(private val context:Context, private val libraries:List<LibraryName>):RecyclerView.Adapter<LibraryAdapter.MyViewHolder>() {
13+
14+
15+
class LibraryAdapter(private val context: Context?, private val libraries: List<LibraryName>):RecyclerView.Adapter<LibraryAdapter.MyViewHolder>() {
1316
inner class MyViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
1417
val name:TextView = itemView.findViewById(R.id.name_txt)
15-
1618
fun setData(library: LibraryName, position: Int) {
1719
name.text = library.name
18-
1920
}
20-
}
21+
}
2122

2223
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
23-
val view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item,parent,false)
24+
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_item, parent, false)
2425
return MyViewHolder(view)
2526
}
2627

2728
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
2829
val library = libraries[position]
29-
holder.setData(library,position)
30-
holder.itemView.setOnClickListener { listener(library) }
31-
}
30+
holder.setData(library, position)
31+
holder.itemView.setOnClickListener {
3232

33-
private fun listener(item: LibraryName) {
33+
val activity = context as AppCompatActivity
34+
activity.supportFragmentManager.beginTransaction().replace(R.id.main_fragment, library.fragment).addToBackStack(null).commit()
35+
}
36+
}
3437

35-
var intent = Intent()
3638

37-
when(item.name)
38-
{
39-
"Contour" -> {
40-
intent = Intent(context,ContourSample::class.java)
41-
}
4239

43-
"Coil" ->{
44-
intent = Intent(context,CoilSample::class.java)
45-
}
46-
}
4740

48-
context.startActivity(intent)
4941

50-
}
5142

5243
override fun getItemCount(): Int {
5344
return libraries.size
5445
}
5546
}
47+

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ package playground.android
22

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

7-
8-
9-
109
class MainActivity : AppCompatActivity() {
1110
override fun onCreate(savedInstanceState: Bundle?) {
1211
super.onCreate(savedInstanceState)
1312
setContentView(R.layout.activity_main)
14-
val recyclerView : RecyclerView = findViewById(R.id.recyclerView)
15-
val adapter = LibraryAdapter(this,Suppliers.libraries)
16-
recyclerView.adapter = adapter
17-
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()
1819
}
20+
1921
}
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+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package playground.android
22

3-
data class LibraryName(var name:String)
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)
48

59

610
object Suppliers{
7-
val libraries = listOf(LibraryName("Contour"),
8-
LibraryName("Coil"),
11+
val libraries = listOf(LibraryName("Contour",ContourFragment()),
12+
LibraryName("Coil" , CoilFragment())
913

1014

1115
)

android/src/main/res/layout/activity_coil_sample.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)