点击按钮加载另一个片段的片段



我是一个全新的移动应用程序开发和磕磕绊绊的方式通过这个应用程序,但我一直在努力尝试和解决这个问题。我想要无数的教程,追捕开发人员的网站和搜索堆栈溢出,但没有我做的似乎工作,请你能帮助我吗?我使用Android Studio //Dolphin | 2021.3.1 Patch 1并在Kotlin中编码。

所有我想做的是打开fragment_sign当我点击按钮android:id="@+id/button_customer_sign"fragment_delivery,然后一旦客户已经签署返回到fragment_delivery当我点击按钮android:id="@+id/button_load_sign"fragment_sign

我有以下build.gradle

dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.annotation:annotation:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.kyanogen.signatureview:signature-view:1.2'
implementation 'androidx.fragment:fragment-ktx:1.6.0-alpha03'
implementation 'androidx.fragment:fragment:1.6.0-alpha03'
debugImplementation 'androidx.fragment:fragment-testing:1.6.0-alpha03'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
// google support library ---------------------------------------------------------------------
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.browser:browser:1.4.0'
}

目前这是在我的DeliveryFragment.kt

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.example.delivery.R
import com.example.delivery.databinding.FragmentDeliveryBinding
import com.example.delivery.ui.sign.Sign
import android.support.v4.app.Fragment //This import is an error
import android.support.v4.app.FragmentManager //This import is an error
import android.support.v4.app.FragmentTransaction //This import is an error
class DeliveryFragment : Fragment() {
private var _binding: FragmentDeliveryBinding? = null
private var customerSign: Button? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val deliveryViewModel =
ViewModelProvider(this).get(DeliveryViewModel::class.java)
_binding = FragmentDeliveryBinding.inflate(inflater, container, false)
val root: View = binding.root
val customer = resources.getStringArray(R.array.customer_list)
val arrayAdapter = ArrayAdapter(requireContext(), R.layout.dropdown_item, customer)
binding.autoCompleteTextView.setAdapter(arrayAdapter)
return root
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContectView(com.example.delivery.R.layout.content_main)
customerSign = findViewById(com.example.delivery.R.id.button_customer_sign) as Button
customerSign.setOnClickListener(View.OnClickListener { openSignFragment() })
} //This entire method is an error
fun openSignFragment() {
val intent = Intent(this, SignFragment::class.java)
startActivity(intent)
} //This entire method is an error
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}

listeneropen arguments是一个接一个的错误,.kt文件的最后3个导入也是如此。我需要找出这个问题,因为我还有许多其他按钮需要尝试和解决。

请有人帮我一下,提前谢谢你。

intent需要activity实例和类名,fragments需要宿主容器活动正在运行。如果你去另一个活动,你将不得不再次使用这个来显示你的片段在活动容器。

请从onCreate中删除setContentView(),因为你在onCreateView中返回了片段的视图。

setContentView()用布局填充当前窗口。

片段视图显示在活动窗口内的容器中。

只返回你的视图一次在onCreateView你的片段,在你的情况下,它是return root

如果你正在使用导航组件->


/** Please Execute this Code in onViewCreated method of your fragment **/
val navController = Navigation.findNavController(view)
navController.navigate(R.id.action_deliveryFragment_to_signFragment) 
//make sure you have defined the action in your navigation graph.

如果你不使用导航组件,也可以使用->

val fragManager = requireActivity().supportFragmentManager
val transaction = fragManager.beginTransaction()
transaction.replace(
R.id.container,
RecordFragment()
)
transaction.addToBackStack(null) // if u want this fragment to stay in stack specify it
transaction.commit()

最新更新