0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【kotlin】超簡単なMVVMを実装してみた、②異なるレイアウト(Fragment)との連携

Last updated at Posted at 2023-09-08

実装内容

・Activity(親View)の下半分にFragment(子View)を重ねる。
・ACTIVITYボタン(紫文字)が押された際に双方ViewのTextViewに1を足し、表示する。
・FRAGEMENTボタン(赤文字)が押された際に双方ViewのTextViewに10を足し、表示する。
Screenshot_20230908_200445.png

下記のURLと動作は同じです
https://qiita.com/Na0yaKub0/items/76854a1dfc79dc978c3e

[コード]
・ViewModelの設定

class MainViewModel : ViewModel(){

    private val _cnt: MutableLiveData<Int> by lazy { MutableLiveData<Int>(0)}
    val cnt: LiveData<Int> = _cnt

    // Activity側のボタンが押された時の処理
    fun onClickActivityButton() {
        _cnt.value = _cnt.value?.plus(1)
    }

    // Fragment側のボタンが押された時の処理
    fun onClickFragmentButton() {
        _cnt.value = _cnt.value?.plus(10)
    }
}

・Fragment(子View)設定

class MainFragment : Fragment() {

    private lateinit var binding: FragmentMainBinding
    private lateinit var viewModel: MainViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // viewBinding初期化
        binding = FragmentMainBinding.inflate(inflater, container, false)
        // 親のviewからViewModel初期化
        activity?.let {
            viewModel = ViewModelProvider(it)[MainViewModel::class.java]
        }

        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.fragmentButton.setOnClickListener {
            viewModel.onClickFragmentButton()
        }

        //  LiveDataを読み込み反映
        viewModel.cnt.observe(viewLifecycleOwner, Observer  {
            binding.fragmentTextView.text = it.toString()
        })

    }
}

・Activity(親View)設定

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private lateinit var viewModel: MainViewModel

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

        // viewBinding初期化
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        // Fragment生成
        val transition = supportFragmentManager.beginTransaction()
        transition.add(R.id.fragmentLayout, MainFragment())
        transition.commit()
        // ViewModel初期化
        viewModel = ViewModelProvider(this)[MainViewModel::class.java]

        binding.activityButton.setOnClickListener {
            viewModel.onClickActivityButton()
        }

        //  LiveDataを読み込み反映
        viewModel.cnt.observe(this, Observer {
            binding.activityTextView.text = it.toString()
        })
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?