Mam ten fragment, jego Viewmodel i jego adapter. Może już słuchać kliknięcia, ale wszystko, co wiem, aby to zrobić, to pokazać tosty . Chcę, aby przejdzie do innego fragmentu, który pokaże swoje "szczegóły", które przekazuje dane klikniętego elementu recykleryjnego do tego fragmentu "Szczegóły". Wskazówka: na fragmencie tam jest //TODO i potrzebuję tego kodu.

Oto fragment:

class HomeFragment : Fragment(), RecyclerViewClickListener {

    private lateinit var factory: HomeViewModelFactory
    private lateinit var viewModel: HomeViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?)
    {
        super.onActivityCreated(savedInstanceState)

        val api = DormsAPI()
        val repository = DormRepository(api)

        factory = HomeViewModelFactory(repository)
        viewModel = ViewModelProviders.of(this, factory).get(HomeViewModel::class.java)

        viewModel.getDorms()

        viewModel.dorms.observe(viewLifecycleOwner, Observer { dorms ->
            recyclerViewDorms.also{
                it.layoutManager = LinearLayoutManager(requireContext())
                it.setHasFixedSize(true)
                it.adapter = dormAdapter(dorms, this)
            }
        })
    }
    override fun onRecyclerViewItemClick(view: View, dorms: Dorms) {
        when(view.id){
            R.id.button_reserve -> {
                // TODO: Go to new account if not signed up, etc...
                Toast.makeText(requireContext(), "Reserve button clicked", Toast.LENGTH_LONG).show()
            }
            R.id.layoutBox -> {
                // TODO: Go to Dorm Details
                Toast.makeText(requireContext(), "Go to dorm details", Toast.LENGTH_LONG).show()
            }
        }
    }
}

Jeśli chodzi o klasę adaptera:

class dormAdapter(
    private val dorms: List<Dorms>,
    private val listener: RecyclerViewClickListener
) : RecyclerView.Adapter<dormAdapter.DormViewHolder>() {

    override fun getItemCount() = dorms.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        DormViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.layout_home,
                parent, false
            )
        )

    override fun onBindViewHolder(holder: DormViewHolder, position: Int) {
        holder.recyclerviewDormBinding.dorm = dorms[position]
        holder.recyclerviewDormBinding.buttonReserve.setOnClickListener {
            listener.onRecyclerViewItemClick(holder.recyclerviewDormBinding.buttonReserve, dorms[position])

        }
        holder.recyclerviewDormBinding.layoutBox.setOnClickListener {
            listener.onRecyclerViewItemClick(holder.recyclerviewDormBinding.layoutBox, dorms[position])
        }
    }

    inner class DormViewHolder(
        val recyclerviewDormBinding: LayoutHomeBinding
    ) : RecyclerView.ViewHolder(recyclerviewDormBinding.root)

}

Wreszcie oto WILTMODEL:

class HomeViewModel(private val repository: DormRepository) : ViewModel() {

    private lateinit var job: Job

    private val _dorms = MutableLiveData<List<Dorms>>()
    val dorms: LiveData<List<Dorms>>
        get() = _dorms

    fun getDorms() {
        job = Coroutines.ioThenMain(
            { repository.getDorms() },
            { _dorms.value = it }
        )
    }

    override fun onCleared() {
        super.onCleared()
        if(::job.isInitialized) job.cancel()
    }
}

Edytuj: W razie potrzeby mam ten interfejs:

interface RecyclerViewClickListener {
    fun onRecyclerViewItemClick(view: View, dorms: Dorms)
}
0
kkshinichi 19 styczeń 2020, 20:31

1 odpowiedź

Najlepsza odpowiedź

Ponieważ z aktywności dodamy HomeFragment, to, co spróbujemy zrobić, to utworzyć interfejs do komunikowania między aktywnością a fragmentem.

1. Utwórz interfejs

class HomeFragment : Fragment(), RecyclerViewClickListener {
    ...
    ...

    private var callback : Callback? = null

    ...
    ...

    override fun onAttach(context: Context) {
        ...
        // Callback instance is initialized 
        if(context is Callback) callback = context
        else throw RuntimeException("$context must implement Callback")
    }

    ...
    ...

    override fun onDetach() {
        callback = null
    }

    ...
    ...

    override fun onRecyclerViewItemClick(view: View, dorms: Dorms) {
        when(view.id){
            R.id.button_reserve -> {
                // TODO: Go to new account if not signed up, etc...
                Toast.makeText(requireContext(), "Reserve button clicked", Toast.LENGTH_LONG).show()
            }
            R.id.layoutBox -> {
                // Go to Dorm Details
                callback?.onShowDormDetail(dorm)
            }
        }
    }

    ...
    ...

    // This interface will act as mode to communication between
    // activity and fragment
    interface Callback {
        fun onShowDormDetail(dorm: Dorm)
    }
}

2. Wdrożenie Callback na aktywności wywołania

class HomeActivity : AppCompatActivity(), HomeFragment.Callback {

...
...

    override onShowDormDetail(dorm: Dorm) {
        // Add or replace the detail fragment here
    }
}
1
Hussain 19 styczeń 2020, 18:26