gpt4 book ai didi

android - bottomappbar android中的抽屉导航

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:11 26 4
gpt4 key购买 nike

enter image description here

class BottomNavigationDrawerFragment: BottomSheetDialogFragment(), 
NavigationView.OnNavigationItemSelectedListener {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_bottomsheet, container, false)
}

override fun onNavigationItemSelected(item : MenuItem): Boolean {
// Bottom Navigation Drawer menu item clicks
when (item.itemId) {
R.id.nav1 -> context!!.toast("oneeeeee")
R.id.nav2 -> context!!.toast("twoooooo")
R.id.nav3 -> context!!.toast("threeeee")

return true
}
// Add code here to update the UI based on the item selected
// For example, swap
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

navigation_view.setNavigationItemSelectedListener(this)

// Add code here to update the UI based on the item selected
// For example, swap
}
}

// This is an extension method for easy Toast call
fun Context.toast(message: CharSequence) {
val toast = Toast.makeText(this, message, Toast.LENGTH_SHORT)
toast.setGravity(Gravity.BOTTOM, 0, 600)
toast.show()
}

我想要实现的是图像中给出的东西。我想在底部应用栏中制作一个 navigation drawer。上面的代码不起作用,它告诉未解析的引用类型 setNavigationItemSelectedListener。我的代码有什么错误?

最佳答案

您应该在 bottomAppbar 中添加抽屉图标,然后为抽屉使用 bottomsheet

对于你的抽屉,你有两个选择:

1- 遵循谷歌标准并在菜单文件夹中添加抽屉项目(看来你不想要这个)

2- 替换底部工作表中的 fragment ,这样您就可以自定义 fragment 并做任何您喜欢的事情

---------------- 替换底页中的 fragment ------------

你的 activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
android:background="@color/white"
android:orientation="vertical">

<FrameLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:elevation="6dp"
android:visibility="visible"
app:layout_behavior="@string/bottom_sheet_behavior">

<FrameLayout
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</FrameLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

你的 Activity.java

public class Activity extends AppCompatActivity implements 
FragmentNavigation.OnFragmentInteractionListener {
private CoordinatorLayout coordinatorLayout;
private View bottomSheet;
private BottomSheetBehavior<View> behavior;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);

FrameLayout bottomSheetLayout = (FrameLayout)
findViewById(R.id.menu);

FragmentNavigation fragmentNavigation = new FragmentNavigation();

androidx.fragment.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();

fragmentTransaction.replace(bottomSheetLayout.getId(),
fragmentNavigation, "k");

fragmentTransaction.commit();


coordinatorLayout = (CoordinatorLayout)
findViewById(R.id.main_content);
bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
behavior = BottomSheetBehavior.from(bottomSheet);
}

@Override
public void onFragmentInteraction(Uri uri) {

}
}

您的 fragment 导航

    public class FragmentNavigation extends androidx.fragment.app.Fragment {
private String descriptions;

public FragmentNavigation () {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

View view = inflater.inflate(R.layout.fragment_navigation, container, false);

return view;
}
}

你的 fragment_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">

<TextView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:elevation="6dp"
android:visibility="visible"
android:text="here is the navigation menu"
app:layout_behavior="@string/bottom_sheet_behavior"/>



</LinearLayout>

关于android - bottomappbar android中的抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52530111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com