繁体   English   中英

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

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

我目前正在使用带有 Kotlin 的 android-studio 开发应用程序。

这个应用程序有

  • 我可以通过片段使用的通用值

  • 主要内容的片段类

  • 底部工作表的片段类

我想在更改底部工作表中 edittext 的公共值后立即在主片段视图上显示一个新值。

但是就我的代码而言,当我从底部工作表返回主片段视图时,公共值不会改变......

我该如何解决这种情况?


以下是代码:

构建.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

package com.sample.bottomsheetvalue

import androidx.lifecycle.ViewModel

class AppState: ViewModel()  {

    var value:String = "initialValue"
}

主活动.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)

    }
}

活动_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>

主片段.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

<?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>

底片.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

<?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>

安卓工作室:3.3.2

Livedata添加到您的视图模型类:

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

并在您的片段中观察:

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

并且还在您的底部工作表中更新数据:

data.postValue("newValue")

暂无
暂无

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

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