Created
September 25, 2019 03:10
-
-
Save Draketheb4dass/e34da7692a6cbd6874890d8ff540bf72 to your computer and use it in GitHub Desktop.
Fragment using MVVM, cardview inside recyclerview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.drake.kunuk.ui.buy | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.fragment.app.Fragment | |
import androidx.lifecycle.Observer | |
import androidx.lifecycle.ViewModelProviders | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import com.example.drake.kunuk.R | |
import com.example.drake.kunuk.data.model.Buy | |
import kotlinx.android.synthetic.main.buy_fragment.* | |
import java.util.* | |
class BuyFragment : Fragment() { | |
companion object { | |
fun newInstance() = BuyFragment() | |
} | |
private lateinit var viewModel: BuySharedViewModel | |
private val buyList = ArrayList<Buy>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
viewModel = ViewModelProviders.of(this).get(BuySharedViewModel::class.java) | |
viewModel.getBuys().observe(this, Observer<List<Buy>> { buys -> | |
buyList.addAll(buys) | |
(rvBuy.adapter as BuyAdapter).notifyDataSetChanged() | |
}) | |
} | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
return inflater.inflate(R.layout.buy_fragment, container, false) | |
} | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
rvBuy.layoutManager = LinearLayoutManager(context) | |
rvBuy.adapter = BuyAdapter(activity!!.applicationContext, | |
R.layout.buy_card, buyList) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment