Advertisement
ipdan4ik

[lb8] MainActivity.kt

Apr 18th, 2021
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.37 KB | None | 0 0
  1. package com.example.lb8
  2.  
  3. import android.annotation.SuppressLint
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.view.View
  7. import android.widget.ArrayAdapter
  8. import android.widget.ListView
  9. import android.widget.TextView
  10. import android.widget.Toast
  11. import androidx.core.os.bundleOf
  12. import androidx.fragment.app.FragmentTransaction
  13.  
  14. interface OnDataListener {
  15.     fun onData(Data: Int)
  16.     fun onAnswer(right: Int?, summary: Int?, difficulty: Int?)
  17. }
  18.  
  19. class MainActivity : AppCompatActivity(), OnDataListener {
  20.     private var rightAnswers: Int= 0
  21.     private var countAnswers: Int = 0
  22.     private var taskDifficulty: Int = 10
  23.  
  24.     private var isTwoPane = false
  25.     override fun onCreate(savedInstanceState: Bundle?) {
  26.         super.onCreate(savedInstanceState)
  27.         setContentView(R.layout.activity_main)
  28.         isTwoPane = findViewById<View>(R.id.frame_left) != null
  29.         val bundle = bundleOf("Data" to 0, "right" to rightAnswers, "all" to countAnswers, "diff" to taskDifficulty)
  30.         val frag2 = RightFragment()
  31.         frag2.arguments = bundle
  32.         if (isTwoPane) {
  33.             supportFragmentManager.beginTransaction()
  34.                 .add(R.id.frame_left, LeftFragment())
  35.                 .add(R.id.frame_right, frag2)
  36.                 .commit()
  37.         }
  38.         else {
  39.             supportFragmentManager.beginTransaction()
  40.                 .add(R.id.container, LeftFragment())
  41.                 .commit()
  42.         }
  43.     }
  44.  
  45.  
  46.     override fun onData(Data: Int) {
  47.         val bundle = bundleOf("Data" to 0, "right" to rightAnswers, "all" to countAnswers, "diff" to taskDifficulty)
  48.         val frag2 = RightFragment()
  49.         frag2.arguments = bundle
  50.  
  51.         supportFragmentManager.beginTransaction()
  52.             .replace(
  53.                 if (isTwoPane)
  54.                     R.id.frame_right
  55.                 else
  56.                     R.id.container,
  57.                 frag2)
  58.             .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
  59.             .addToBackStack(null)
  60.             .commit()
  61.  
  62.     }
  63.  
  64.     override fun onAnswer(right: Int?, summary: Int?, diff: Int?) {
  65.         if (right != null) {
  66.             rightAnswers = right
  67.         }
  68.         if (summary != null) {
  69.             countAnswers = summary
  70.         }
  71.         if (diff != null) {
  72.             taskDifficulty = diff
  73.         }
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement