iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0

今天我們要做的是邀約詳細資訊! 繼昨天我們完成Recyclerview之後,我們今天要讓我們使用者可以點進去,因為我們每筆邀約的資料都很多,如果要塞在RecyclerView的話,這樣會太醜!

1.得到點擊對應的Invitation

我們現在可以透過Adapter的onBindViewHolder來拿到我們現在點擊的position,進而得到我們的Invitation,所以我們現在要做的就是直接把我們得到的Invitationt傳進ViewModel!

在onBindViewHolder新增

holder.itemView.setOnClickListener{
fragment.addSelectedInvitationToViewModel(model)
}

我們用itemView的話,就是代表整個Item,如果想要item裡面的某個view,則可以再點下去,如上次的垃圾桶

holder.binding.ivHomeInvitationItemListDelete.setOnClickListener{
            fragment.setAndShowDeleteDialog(model.id)
        }

2.傳入LiveData

來到HomeFragment

fun addSelectedInvitationToViewModel(invitation: Invitation){
		//傳入viewModel
        matchingViewModel.addSelectedInvitationToLiveData(invitation)
		//導航至Detail頁面
        nav.navigate(R.id.action_navigation_home_to_invitationDetailFragment)
    }

我們會看到一些紅字,但是沒關係! 我們先解決viewModel的部分!
來到viewModel,新增以下

//新增livedata,讓我們在xml可以觀察
private val _selectedInvitation = MutableLiveData<Invitation>()
val selectedInvitation: LiveData<Invitation>
get() = _selectedInvitation



//把從fragment來的資料傳入livedata
fun addSelectedInvitationToLiveData(invitation: Invitation){
        _selectedInvitation.postValue(invitation)
    }

3.建立InvitationDetailFragment

好的! 我們首先就是一樣需要把 databinding設定好
再過來設定觀察

//再上面宣告
private val matchingViewModel: MatchingViewModel by sharedViewModel()


//onCreateView 新增觀察(我們這邊只觀察圖片,其他的資料我們在xml綁定)
matchingViewModel.selectedInvitation.observe(viewLifecycleOwner, Observer {
     Constant.loadPetImage(it.pet_image,binding.ivInvitationDetailImage)
        })

★ 圖片綁定跟Recyclerview的綁定除了在UI寫觀測之外,也可以透過BindingAdapter來
實現,有興趣的小夥伴可以去查一下關鍵字
來建立layou

dimen

<dimen name="detail_tv_margin_top">8dp</dimen>

string

    <string name="toolbar_title_invitation_detail">邀約內容</string>
    <string name="chat">聊天</string>
    <string name="pet_type">寵物種類</string>
    <string name="pet_type_description">寵物品種</string>
    <string name="date_area">邀約地區</string>
    <string name="date_place">邀約地點</string>
    <string name="date_time">邀約時間</string>
    <string name="date_note">注意事項</string>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">


		//在這邊設定databinding
    <data>
        <variable
            name="viewModel"
            type="com.example.petsmatchingapp.viewmodel.MatchingViewModel" />


    </data>


<androidx.constraintlayout.widget.ConstraintLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.fragment.InvitationDetailFragment">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_invitation_detail_fragment"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/light_pewter_blue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/toolbar_title_invitation_detail"
            android:textColor="@color/white"
            android:textSize="@dimen/toolbar_textSize"
            android:textStyle="bold">

        </TextView>

    </androidx.appcompat.widget.Toolbar>


    <ImageView
        android:id="@+id/iv_invitation_detail_image"
        android:layout_width="match_parent"
        android:scaleType="center"
        android:layout_height="@dimen/image_height"
        app:layout_constraintTop_toBottomOf="@id/toolbar_invitation_detail_fragment"
        app:layout_constraintStart_toStartOf="parent">
    </ImageView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@id/btn_invitation_detail_submit"
        app:layout_constraintTop_toBottomOf="@id/iv_invitation_detail_image"
        app:layout_constraintVertical_bias="1.0"
        tools:layout_editor_absoluteX="0dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/tip_margin_start_end"
            android:layout_marginEnd="@dimen/tip_margin_start_end"
            android:orientation="vertical">

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_title_pet_type"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/pet_type"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold">

            </com.example.petsmatchingapp.utils.JFTextView>

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_pet_type"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/item_value_text_color"
                android:textSize="@dimen/edText_textSize"
                android:text="@{viewModel.selectedInvitation.pet_type}"
                android:textStyle="bold"
                tools:text="狗">

            </com.example.petsmatchingapp.utils.JFTextView>


            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_title_pet_type_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/detail_tv_margin_top"
                android:text="@string/pet_type_description"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                tools:text="寵物品種">

            </com.example.petsmatchingapp.utils.JFTextView>

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_pet_type_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/item_value_text_color"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                android:text="@{viewModel.selectedInvitation.pet_type_description}"
                tools:text="狗">

            </com.example.petsmatchingapp.utils.JFTextView>

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_title_area"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/detail_tv_margin_top"
                android:text="@string/date_area"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                tools:text="邀約地區">

            </com.example.petsmatchingapp.utils.JFTextView>

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_area"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/item_value_text_color"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                android:text="@{viewModel.selectedInvitation.area}"
                tools:text="台南" />


            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_title_date_place"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/detail_tv_margin_top"
                android:text="@string/date_place"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                tools:text="邀約地點">

            </com.example.petsmatchingapp.utils.JFTextView>

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_date_place"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/item_value_text_color"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                android:text="@{viewModel.selectedInvitation.date_place}"
                tools:text="台南國家公園" />

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_title_date_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/detail_tv_margin_top"
                android:text="@string/date_time"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                tools:text="邀約時間">

            </com.example.petsmatchingapp.utils.JFTextView>

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_date_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/item_value_text_color"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                android:text="@{viewModel.selectedInvitation.date_time}"
                tools:text="2021/11/07" />

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_title_note"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/detail_tv_margin_top"
                android:text="@string/date_note"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                tools:text="注意事項">

            </com.example.petsmatchingapp.utils.JFTextView>

            <com.example.petsmatchingapp.utils.JFTextView
                android:id="@+id/tv_invitation_detail_note"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/item_value_text_color"
                android:textSize="@dimen/edText_textSize"
                android:textStyle="bold"
                android:text="@{viewModel.selectedInvitation.note}"
                tools:text="一堆" />


        </LinearLayout>


    </ScrollView>

    <com.example.petsmatchingapp.utils.JFButton
        android:id="@+id/btn_invitation_detail_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="20dp"
        android:layout_marginStart="@dimen/tip_margin_start_end"
        android:layout_marginEnd="@dimen/tip_margin_start_end"
        android:background="@drawable/button_background"
        android:foreground="?attr/selectableItemBackground"
        android:textColor="@color/white"
        android:text="@string/chat"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

設定完之後,我們也要在InvitationDetailFragment綁定viewModel喔,不然觀測不到

//直接在onCreateView新增,其中 viewModel就是我們在xml,data裡面的variable裡面的name
binding.viewModel = matchingViewModel

也要記得在我們的mobile_navigation.xml來新增建立好的InvitationDetailFragment跟連連看喔,這樣我們才可以透過navigation來進行跳轉Fragment!

最後就是來刪除 ActionBar跟BottomNavigation以及新增返回鍵

//新增並在onCreateView呼叫
private fun dismissActivityActionBarAndBottomNavigationView(){
        val activityInstance = this.activity as MatchingActivity
        activityInstance.supportActionBar?.hide()
        activityInstance.findViewById<BottomNavigationView>(R.id.nav_view).visibility = View.GONE

    }

新增返回鍵

binding.toolbarInvitationDetailFragment.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24)
        binding.toolbarInvitationDetailFragment.setNavigationOnClickListener {
            requireActivity().onBackPressed()
        }

大功告成啦!!
成果如下!!!
day12.finish


上一篇
【Day11】HomeFragment X RecyclerView X Firestore取/刪除資料
下一篇
【day13】DashboardFragment X CardView
系列文
30天建立寵物約散App-Android新手篇30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言