简体   繁体   English

如何将底部工作表的共同值反映到片段?

[英]How to reflect a common value from a bottom sheet to a fragment?

I'm currently developing an application using android-studio with Kotlin.我目前正在使用带有 Kotlin 的 android-studio 开发应用程序。

This app has这个应用程序有

  • a common value I can use through fragments我可以通过片段使用的通用值

  • a fragment class for main content主要内容的片段类

  • a fragment class for bottom sheet底部工作表的片段类

I want to show a new value on the main fragment view right after I change the common value at edittext in bottom sheet.我想在更改底部工作表中 edittext 的公共值后立即在主片段视图上显示一个新值。

But in the case of my codes, the common value doesn't change when I' back to the main fragment view from the bottom sheet...但是就我的代码而言,当我从底部工作表返回主片段视图时,公共值不会改变......

How can I solve this situation?我该如何解决这种情况?


Here are the codes:以下是代码:

build.gradle构建.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.sample.bottomsheetvalue"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

    implementation "androidx.fragment:fragment-ktx:1.3.0-alpha04"
    implementation "com.google.android.material:material:1.1.0-alpha06"

}

AppState.kt AppState.kt

package com.sample.bottomsheetvalue

import androidx.lifecycle.ViewModel

class AppState: ViewModel()  {

    var value:String = "initialValue"
}

MainActivity.kt主活动.kt

package com.sample.bottomsheetvalue

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }
}

activity_main.xml活动_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
    <fragment
            android:id="@+id/main_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.tomato_love.bottomsheetvalue.MainFragment"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

MainFragment.kt主片段.kt

package com.sample.bottomsheetvalue

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider

class MainFragment : Fragment() {

    lateinit var appState: AppState

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        activity?.run {
            appState = ViewModelProvider(this).get(AppState::class.java)
        }
        val view = inflater.inflate(R.layout.fragment_main, container, false)

        val button = view.findViewById<Button>(R.id.button)
        val textView = view.findViewById<TextView>(R.id.textView)

        textView.text = appState.value

        button.setOnClickListener {

            val fm = activity!!.supportFragmentManager
            val bottomSheet =BottomSheet()
            bottomSheet.show(fm, "navigation_bottom_sheet")
        }
        return view
    }
}

fragment_main.xml fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainFragment"
        android:id="@+id/constraintLayoutFragment">

    <Button
            android:id="@+id/button"
            android:text="show bottom sheet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
    />
    <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginTop="32dp"
            app:layout_constraintTop_toBottomOf="@+id/button"/>
</androidx.constraintlayout.widget.ConstraintLayout>

BottomSheet.kt底片.kt

package com.sample.bottomsheetvalue

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class BottomSheet : BottomSheetDialogFragment() {

    lateinit var appState: AppState

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        activity?.run {
            appState = ViewModelProvider(this).get(AppState::class.java)
        }

        val view = inflater.inflate(R.layout.fragment_bottom_sheet, container, false)

        val editText = view.findViewById<EditText>(R.id.editText)

        editText.setOnEditorActionListener { v, actionId, event ->
            when (actionId) {
                EditorInfo.IME_ACTION_DONE -> {
                    appState.value = editText.text.toString()
                    println(appState.value)
                    true
                }
                else -> false
            }
        }
        return view
    }
}

fragment_bottom_sheet.xml fragment_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".BottomSheet">
    <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:inputType="textPersonName"
            android:ems="10"
            android:imeOptions="actionDone"
    />
</FrameLayout>

Android Studio : 3.3.2安卓工作室:3.3.2

Add a Livedata to your view model class:Livedata添加到您的视图模型类:

val data: MutableLiveData<String> = MutableLiveData("Initial Value")

And observe that in your fragment:并在您的片段中观察:

appState.data.observe(viewLifecycleOwner, Observer {
            // Update your ui here
})

And also in your bottom sheet update the data:并且还在您的底部工作表中更新数据:

data.postValue("newValue")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM