Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android DroperLayout在替换片段之前不会关闭 我有一个问题,DrawerLayout不顺利关闭,它停在中间一段时间,它完全关闭后,完成更换片段。在编码中,我在替换片段(productList类)之前关闭抽屉布局。在productList类中,它调用asynctask从服务器获取所有产品列表,同时运行进度条。理想情况下,当用户单击菜单时,抽屉布局将立即关闭,productList片段将加载所有产品列表。在我看来,抽屉布局关闭事件似乎在等待productList的异步任务。非常感谢您的任何建议 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener { public String tag = "MainActivity"; Context mContext; //User Object User user; //Control TextView txtName, txtPosition, txtLogout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; user = Constants.getUser(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); //NavigationView Header View headerView = navigationView.getHeaderView(0); txtName = (TextView) headerView.findViewById(R.id.nav_header_main_name) ; txtName.setText(user.getName().toString()); txtLogout = (TextView) headerView.findViewById(R.id.nav_header_main_logout); txtLogout.setOnClickListener(this); navigationView.setNavigationItemSelectedListener(this); //Add Menu in navigationView Menu myMenu = navigationView.getMenu(); myMenu.clear(); myMenu.add(0,Integer.parseInt("01"),0,"Product List"); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { //close drawer DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); // replace fragment int id = item.getItemId(); FragmentManager fragmentManager = getSupportFragmentManager(); if(Constants.DEBUG) { Toast.makeText(mContext, " Menu Id " + String.valueOf(id), Toast.LENGTH_LONG).show(); } fragmentManager.beginTransaction() .replace(R.id.container,new productList()) .commit(); return true; } @Override public void onClick(View v) { //Log out onclick if(v.getId() == R.id.nav_header_main_logout){ this.finish(); } } }_Android_Drawerlayout - Fatal编程技术网

Android DroperLayout在替换片段之前不会关闭 我有一个问题,DrawerLayout不顺利关闭,它停在中间一段时间,它完全关闭后,完成更换片段。在编码中,我在替换片段(productList类)之前关闭抽屉布局。在productList类中,它调用asynctask从服务器获取所有产品列表,同时运行进度条。理想情况下,当用户单击菜单时,抽屉布局将立即关闭,productList片段将加载所有产品列表。在我看来,抽屉布局关闭事件似乎在等待productList的异步任务。非常感谢您的任何建议 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener { public String tag = "MainActivity"; Context mContext; //User Object User user; //Control TextView txtName, txtPosition, txtLogout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; user = Constants.getUser(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); //NavigationView Header View headerView = navigationView.getHeaderView(0); txtName = (TextView) headerView.findViewById(R.id.nav_header_main_name) ; txtName.setText(user.getName().toString()); txtLogout = (TextView) headerView.findViewById(R.id.nav_header_main_logout); txtLogout.setOnClickListener(this); navigationView.setNavigationItemSelectedListener(this); //Add Menu in navigationView Menu myMenu = navigationView.getMenu(); myMenu.clear(); myMenu.add(0,Integer.parseInt("01"),0,"Product List"); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { //close drawer DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); // replace fragment int id = item.getItemId(); FragmentManager fragmentManager = getSupportFragmentManager(); if(Constants.DEBUG) { Toast.makeText(mContext, " Menu Id " + String.valueOf(id), Toast.LENGTH_LONG).show(); } fragmentManager.beginTransaction() .replace(R.id.container,new productList()) .commit(); return true; } @Override public void onClick(View v) { //Log out onclick if(v.getId() == R.id.nav_header_main_logout){ this.finish(); } } }

Android DroperLayout在替换片段之前不会关闭 我有一个问题,DrawerLayout不顺利关闭,它停在中间一段时间,它完全关闭后,完成更换片段。在编码中,我在替换片段(productList类)之前关闭抽屉布局。在productList类中,它调用asynctask从服务器获取所有产品列表,同时运行进度条。理想情况下,当用户单击菜单时,抽屉布局将立即关闭,productList片段将加载所有产品列表。在我看来,抽屉布局关闭事件似乎在等待productList的异步任务。非常感谢您的任何建议 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener { public String tag = "MainActivity"; Context mContext; //User Object User user; //Control TextView txtName, txtPosition, txtLogout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; user = Constants.getUser(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); //NavigationView Header View headerView = navigationView.getHeaderView(0); txtName = (TextView) headerView.findViewById(R.id.nav_header_main_name) ; txtName.setText(user.getName().toString()); txtLogout = (TextView) headerView.findViewById(R.id.nav_header_main_logout); txtLogout.setOnClickListener(this); navigationView.setNavigationItemSelectedListener(this); //Add Menu in navigationView Menu myMenu = navigationView.getMenu(); myMenu.clear(); myMenu.add(0,Integer.parseInt("01"),0,"Product List"); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { //close drawer DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); // replace fragment int id = item.getItemId(); FragmentManager fragmentManager = getSupportFragmentManager(); if(Constants.DEBUG) { Toast.makeText(mContext, " Menu Id " + String.valueOf(id), Toast.LENGTH_LONG).show(); } fragmentManager.beginTransaction() .replace(R.id.container,new productList()) .commit(); return true; } @Override public void onClick(View v) { //Log out onclick if(v.getId() == R.id.nav_header_main_logout){ this.finish(); } } },android,drawerlayout,Android,Drawerlayout,这可能是因为导航抽屉的动画和片段的创建同时发生。 您可以通过稍微延迟启动片段来解决此问题- handler.postDelayed(new Runnable() { @Override public void run() { // replace fragment int id = item.getItemId(); FragmentManager fragmentManager = getSupportFragmentManage

这可能是因为导航抽屉的动画和片段的创建同时发生。 您可以通过稍微延迟启动片段来解决此问题-

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // replace fragment
        int id = item.getItemId();
        FragmentManager fragmentManager = getSupportFragmentManager();
        if(Constants.DEBUG) {
            Toast.makeText(mContext, " Menu Id " + String.valueOf(id), Toast.LENGTH_LONG).show();
        }

        fragmentManager.beginTransaction()
                .replace(R.id.container,new productList())
                .commit();
    }
}, 300);