Skip to content

Instantly share code, notes, and snippets.

@bphermansson
Created February 9, 2023 14:13
Show Gist options
  • Save bphermansson/fa12159f0c9da732bbd78111bdc003be to your computer and use it in GitHub Desktop.
Save bphermansson/fa12159f0c9da732bbd78111bdc003be to your computer and use it in GitHub Desktop.
Code for my take on Exercise2
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
##############
class PersonAdapter(
val onItemClicked : (Boolean)-> Unit)
: RecyclerView.Adapter<PersonAdapter.ViewHolder>(){
class ViewHolder(view : View) : RecyclerView.ViewHolder(view) {
val personName : TextView
init {
personName = view.findViewById(R.id.personNameTV)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.personname, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.personName.text = "Bertil " + position.toString()
holder.itemView.setOnClickListener {
onItemClicked(true)
}
}
override fun getItemCount(): Int {
return 5
}
}
##############
class StartFragment : Fragment() {
lateinit var listadapter: PersonAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_start, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
listadapter = PersonAdapter{
requireActivity().supportFragmentManager.beginTransaction().replace(R.id.fragmentContainerView, FragmentDetail()).addToBackStack("mystack").commit()
}
val personRecview = requireActivity().findViewById<RecyclerView>(R.id.personsRV)
personRecview.adapter = listadapter
personRecview.layoutManager = LinearLayoutManager(requireContext())
}
}
##############
class FragmentDetail : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_detail, container, false)
}
}
##############
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:visibility="visible"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="com.paheco.exercise2_take2.StartFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/fragment_start" />
</androidx.constraintlayout.widget.ConstraintLayout>
##############
fragment_start.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/personsRV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
##############
fragment_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentDetail">
<TextView
android:id="@+id/detailTV"
android:layout_width="147dp"
android:layout_height="90dp"
android:layout_marginTop="44dp"
android:text="DETAIL"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
##############
personname.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp">
<TextView
android:id="@+id/personNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment