gpt4 book ai didi

java - 抽屉导航在关闭时抽动

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:49:14 25 4
gpt4 key购买 nike

我尝试在我的应用中实现抽屉导航(Material Design)。我的 Activity 包含带有 fragment 的 FrameLayout。当用户在 Navigation Drawer FrameLayout 中选择项目时重新加载新 fragment :

    listViewDrawer.setOnItemClickListener( new AdapterView.OnItemClickListener() {

@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id) {

drawerLayout.closeDrawer( listViewDrawer); // <<<-------

toolbar.setTitle( getResources().getString( R.string.title));

fragmentMain = new FragmentMain();

android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace( R.id.frameLayoutMain, ApplicationTapaKiosk.getInstance().fragmentMain)
.commit();

}
}

当我点击项目时一切正常。 Navigation Drawer 关闭不顺利,但卡住(抽搐、抽搐),因为 fragment 在后台重新加载。

如何顺利关闭Navigation Drawer?

最佳答案

你必须决定你是否更喜欢:

替换 FrameLayout 中的 Fragment,然后关闭 DrawerLayout

关闭 DrawerLayout,然后替换您的 Fragment

实现第一种方法:

只需在您的代码中进行这些更改:

listViewDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
toolbar.setTitle(getResources().getString(R.string.title));

fragmentMain = new FragmentMain();

android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayoutMain, ApplicationTapaKiosk.getInstance().fragmentMain)
.commit();

fragmentManager.executePendingTransactions(); // wait for the transaction to finish
drawerLayout.closeDrawer(listViewDrawer); // call closeDrawer(...) as a last thing
}
}

实现第二种方法:

在用户点击时设置某种标志:

listViewDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mDrawerItemClicked = true;
drawerLayout.closeDrawer(listViewDrawer);
}
}

并在您的 DrawerLayout 上设置 DrawerLayout.DrawerListener,如下所示:

drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}

@Override
public void onDrawerOpened(View drawerView) {
}

@Override
public void onDrawerClosed(View drawerView) {
if (mDrawerItemClicked){
mDrawerItemClicked = false;

toolbar.setTitle(getResources().getString(R.string.title));
fragmentMain = new FragmentMain();
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayoutMain, ApplicationTapaKiosk.getInstance().fragmentMain)
.commit();
}
}

@Override
public void onDrawerStateChanged(int newState) {
}
});

关于java - 抽屉导航在关闭时抽动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29896748/

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