我对 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
}
Run Code Online (Sandbox Code Playgroud)
因此,它可以正常工作,并且没有片段对象可以让我像平常一样使用 NewInstance 传递数据。在这种情况下我该怎么办?
谢谢!
小智 6
您可以使用捆绑包将数据从活动传递到片段,如下所示
Bundle bundle = new Bundle();
String myMessage = "Stack Overflow is cool!";
bundle.putString("message", myMessage );
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
Run Code Online (Sandbox Code Playgroud)
在片段中您可以访问此捆绑包
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
String myValue = this.getArguments().getString("message");
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7101 次 |
| 最近记录: |