fix: working friends master/details

parent d73ffede
...@@ -32,9 +32,9 @@ class FriendDetailActivity : AppCompatActivity() { ...@@ -32,9 +32,9 @@ class FriendDetailActivity : AppCompatActivity() {
supportFragmentManager.beginTransaction() supportFragmentManager.beginTransaction()
.add(R.id.friend_detail_container, FriendDetailFragment().apply { .add(R.id.friend_detail_container, FriendDetailFragment().apply {
arguments = Bundle().apply { arguments = Bundle().apply {
putString( putInt(
FriendDetailFragment.ARG_ITEM_ID, FriendDetailFragment.ARG_FRIEND_ID,
intent.getStringExtra(FriendDetailFragment.ARG_ITEM_ID) intent.getIntExtra(FriendDetailFragment.ARG_FRIEND_ID, -1)
) )
} }
}) })
......
...@@ -28,11 +28,13 @@ class FriendDetailFragment : Fragment() { ...@@ -28,11 +28,13 @@ class FriendDetailFragment : Fragment() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
arguments?.let { arguments?.let {
if (it.containsKey(ARG_ITEM_ID)) { println("Got args!")
if (it.containsKey(ARG_FRIEND_ID)) {
// Load the dummy content specified by the fragment // Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader // arguments. In a real-world scenario, use a Loader
// to load content from a content provider. // to load content from a content provider.
item = DummyContent.ITEM_MAP[it.getInt(ARG_ITEM_ID)] item = DummyContent.ITEM_MAP[it.getInt(ARG_FRIEND_ID)]
println("item: $item")
activity?.toolbar_layout?.title = item?.name activity?.toolbar_layout?.title = item?.name
} }
} }
...@@ -43,11 +45,12 @@ class FriendDetailFragment : Fragment() { ...@@ -43,11 +45,12 @@ class FriendDetailFragment : Fragment() {
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
val rootView = inflater.inflate(R.layout.friend_detail, container, false) val rootView = inflater.inflate(R.layout.friend_detail, container, false)
// Show the dummy content as text in a TextView.
item?.let { item?.let {
rootView.friend_detail.text = it.mapString() println("Got friend: ${it.name}")
activity?.title = it.name activity?.title = it.name
rootView.apply {
friendData.text = it.mapString()
}
} }
return rootView return rootView
...@@ -58,6 +61,6 @@ class FriendDetailFragment : Fragment() { ...@@ -58,6 +61,6 @@ class FriendDetailFragment : Fragment() {
* The fragment argument representing the item ID that this fragment * The fragment argument representing the item ID that this fragment
* represents. * represents.
*/ */
const val ARG_ITEM_ID = "item_id" const val ARG_FRIEND_ID = "item_id"
} }
} }
...@@ -83,7 +83,8 @@ class FriendListActivity : AppCompatActivity() { ...@@ -83,7 +83,8 @@ class FriendListActivity : AppCompatActivity() {
contactsList.layoutManager = LinearLayoutManager(this) contactsList.layoutManager = LinearLayoutManager(this)
contactsList.setHasFixedSize(true) contactsList.setHasFixedSize(true)
contactsList.adapter = adapter contactsList.adapter = adapter
`@+id/contactsTitle`.text = getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends)) friends_title.text =
getString(R.string.text_welcome).format(nbFriends, "friend".plural(nbFriends))
} }
...@@ -106,11 +107,12 @@ class FriendListActivity : AppCompatActivity() { ...@@ -106,11 +107,12 @@ class FriendListActivity : AppCompatActivity() {
init { init {
onClickListener = View.OnClickListener { v -> onClickListener = View.OnClickListener { v ->
val item = v.tag as Friend val friend = v.tag as Friend
if (twoPane) { if (twoPane) {
val fragment = FriendDetailFragment().apply { val fragment = FriendDetailFragment().apply {
arguments = Bundle().apply { arguments = Bundle().apply {
putString(FriendDetailFragment.ARG_ITEM_ID, "friend_${item.id}") putInt(FriendDetailFragment.ARG_FRIEND_ID, friend.id)
println("Put arg: ${friend.id}")
} }
} }
parentActivity.supportFragmentManager parentActivity.supportFragmentManager
...@@ -119,7 +121,8 @@ class FriendListActivity : AppCompatActivity() { ...@@ -119,7 +121,8 @@ class FriendListActivity : AppCompatActivity() {
.commit() .commit()
} else { } else {
val intent = Intent(v.context, FriendDetailActivity::class.java).apply { val intent = Intent(v.context, FriendDetailActivity::class.java).apply {
putExtra(FriendDetailFragment.ARG_ITEM_ID, "friend_${item.id}") putExtra(FriendDetailFragment.ARG_FRIEND_ID, friend.id)
println("Put arg: ${friend.id}")
} }
v.context.startActivity(intent) v.context.startActivity(intent)
} }
......
...@@ -8,7 +8,7 @@ import android.widget.Toast ...@@ -8,7 +8,7 @@ import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import fr.plnech.dunbar.R import fr.plnech.dunbar.R
import fr.plnech.dunbar.model.Friend import fr.plnech.dunbar.model.Friend
import kotlinx.android.synthetic.main.contact.view.* import kotlinx.android.synthetic.main.contact_listitem.view.*
class FriendsAdapter( class FriendsAdapter(
...@@ -22,7 +22,7 @@ class FriendsAdapter( ...@@ -22,7 +22,7 @@ class FriendsAdapter(
override fun getItemCount(): Int = friends.size override fun getItemCount(): Int = friends.size
override fun getItemViewType(position: Int): Int = R.layout.contact override fun getItemViewType(position: Int): Int = R.layout.contact_listitem
override fun onBindViewHolder(holder: FriendsViewHolder, position: Int): Unit = override fun onBindViewHolder(holder: FriendsViewHolder, position: Int): Unit =
holder.bind(friends[position]) holder.bind(friends[position])
...@@ -85,5 +85,4 @@ class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view) ...@@ -85,5 +85,4 @@ class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view)
} }
} }
} }
} }
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content">
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ui.FriendListActivity">
<!-- <TextView
This layout is a two-pane layout for the Friends android:id="@+id/friends_title"
master/detail flow. android:layout_width="wrap_content"
android:layout_height="wrap_content"
--> android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@id/panes"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:text="N friends on Dunbar" />
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/panes"
android:id="@+id/friend_list" app:layout_constraintTop_toBottomOf="@id/friends_title"
android:name="fr.plnech.dunbar.FriendListFragment" app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="@dimen/item_width" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginLeft="16dp" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager" android:baselineAligned="false"
tools:context="fr.plnech.dunbar.ui.FriendListActivity" android:divider="?android:attr/dividerHorizontal"
tools:listitem="@layout/friend_list_content" /> android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ui.FriendListActivity">
<FrameLayout <!--
android:id="@+id/friend_detail_container" This layout is a two-pane layout for the Friends
android:layout_width="0dp" master/detail flow.
android:layout_height="match_parent"
android:layout_weight="3" /> -->
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/friend_list"
android:name="fr.plnech.dunbar.FriendListFragment"
android:layout_width="@dimen/item_width"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="fr.plnech.dunbar.ui.FriendListActivity"
tools:listitem="@layout/friend_list_content" />
<FrameLayout
android:id="@+id/friend_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout> </LinearLayout>
\ No newline at end of file </androidx.constraintlayout.widget.ConstraintLayout>
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
<ListView xmlns:android="http://schemas.android.com/apk/res/android" <ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contact_list_item" android:id="@+id/contact_list_item"
tools:listitem="@layout/contact" tools:listitem="@layout/contact_listitem"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/contactsTitle" app:layout_constraintTop_toBottomOf="@id/contactsTitle"
tools:listitem="@layout/contact" tools:listitem="@layout/contact_listitem"
tools:text="@tools:sample/last_names" /> tools:text="@tools:sample/last_names" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <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" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/friend_detail"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:padding="16dp" android:layout_marginTop="24dp">
android:textIsSelectable="true"
tools:context=".ui.FriendDetailFragment" /> <TextView
\ No newline at end of file android:id="@+id/friendData"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="15dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="end"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="2 interactions, last 1 hour ago" />
</androidx.constraintlayout.widget.ConstraintLayout>
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ui.ContactsActivity" tools:context=".ui.ContactsActivity"
tools:showIn="@layout/activity_friends"> tools:showIn="@layout/activity_friends_list">
<TextView <TextView
android:id="@+id/contactsTitle" android:id="@+id/friends_title"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
...@@ -31,5 +31,5 @@ ...@@ -31,5 +31,5 @@
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager" app:layoutManager="LinearLayoutManager"
tools:context=".ui.FriendListActivity" tools:context=".ui.FriendListActivity"
tools:listitem="@layout/contact" /> tools:listitem="@layout/contact_listitem" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment