![](/img/trans.png)
[英]How to pass a button click from Fragment that is in a BottomSheet Toolbar to the Main Activity?
[英]How can I pass data from Activity to BottomSheet Fragment?
我對在BottomSheet中使用片段感到困惑。 我使用了本教程: https : //blog.mindorks.com/android-bottomsheet-in-kotlin
這東西本身基本上是有效的——BottomSheet 隨時顯示和隱藏,但我想根據我從列表中單擊的項目動態傳遞一些數據。
這是根據教程在代碼中的工作方式:
// Fragment creation
var bottomSheetFragment : Fragment? = null
bottomSheetFragment = supportFragmentManager.findFragmentById(R.id.filter_fragment)
// Behavior configuration
private var mBottomSheetBehavior: BottomSheetBehavior<View?>? = null
bottomSheetFragment?.let {
BottomSheetBehavior.from(it.view)?.let { bsb ->
bsb.state = BottomSheetBehavior.STATE_HIDDEN
mBottomSheetBehavior = bsb
}
}
}
// How we show and hide it
fun show(){
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
}
fun hide(){
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
因此,它可以工作並且沒有片段對象,我可以像往常一樣使用 NewInstance 傳遞數據。 在這種情況下我該怎么做?
謝謝!
您可以像這樣使用 bundle 將數據從活動傳遞到片段
Bundle bundle = new Bundle();
String myMessage = "Stack Overflow is cool!";
bundle.putString("message", myMessage );
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
在片段中,您可以訪問此包
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
String myValue = this.getArguments().getString("message");
...
}
您可以在您的nav_graph
添加BottomSheetFragment
並將數據作為action
的參數傳遞。
findNavController().navigate(
MyFragmentDirections.actionMyFragmentToBottomSheetDialog(myData)
)
最后,得到文檔中提到的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.