2016-04-01 203 views
0

我在試圖讓我的導航抽屜運行平穩,因爲打開和關閉抽屜時出現斷續/延遲過渡。我希望這能夠完美運行。任何幫助?導航抽屜在打開和關閉時滯後android

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     final MediaPlayer mp = MediaPlayer.create(this, R.raw.firsteps); 
     mp.start(); 


     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.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setItemIconTintList(null); 
     navigationView.setNavigationItemSelectedListener(this); 


     FragmentManager fm = getSupportFragmentManager(); 
     fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit(); 

    } 

    @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) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      startActivity(new Intent(getApplicationContext(),Testing.class)); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 

     //final MediaPlayer mp = MediaPlayer.create(this, R.raw.xlophone); 

     FragmentManager fm = getSupportFragmentManager(); 

     int id = item.getItemId(); 

     if (id == R.id.car_stories) { 

      fm.beginTransaction().replace(R.id.content_frame,new ChildrensList()).commit(); 
      //mp.start(); 
     } else if (id == R.id.car_places) { 

      fm.beginTransaction().replace(R.id.content_frame,new ImportFragment()).commit(); 
      //mp.start(); 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

回答

-1

通話drawerLayout.closeDrawers後()創建一個新的線程

new Thread(new Runnable() { 
public void run() { 
try { 
    TimeUnit.MILLISECONDS.sleep(300); 
    //Here call you fragmentManager 
    } catch (InterruptedException e) { 
    e.printStackTrace(); 
        } 
    } 
}).start(); 

當然,只要你想,你可以改變毫秒。對不起我的英語

1

文檔:Click

試試這個:

@Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
    new Handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      switch (item.getItemId()) { 
       case: R.id.example1: 
         // do something 
         break; 
       case: R.id.example2: 
         // do something 
         break; 
       default: // do something 
     } 
    }, 200); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
    } 

依我拙見,你需要避免調用 '新' 的每一次點擊。

要解決這個問題,您可以使用常數值導航抽屜的屬性即導航抽屜的 android:layout_widthandroid:layout_height屬性。

 android:layout_width="@dimen/navigation_drawer_width" 
     android:layout_height="match_parent" 

您可能還希望能夠在AndroidManifest.xml

  <activity 
       android:name=".ui.SomeActivity" 
       android:hardwareAccelerated="true" /> 
0

上的活動硬件加速滯後是由兩個重操作(片段置換和抽屜動畫)在同一個線程發生引起的(主線程又名UI線程)。

解決此問題的一種方法是在抽屜完全關閉後調用碎片替換。這樣兩種操作不會同時發生。

此外,這是一個很好的設計實現,可以將一個進度條與片段容器淡入淡出。

裏面你Activity,你應該有這樣的事情:

override fun onNavigationItemSelected(item: MenuItem): Boolean { 

    // Show progress bar and hide content container 
    crossfade(progressBar, container, false) 

    drawerLayout?.addDrawerListener(object : DrawerLayout.DrawerListener { 
     override fun onDrawerSlide(drawerView: View, slideOffset: Float) {} 
     override fun onDrawerOpened(drawerView: View) {} 
     override fun onDrawerStateChanged(newState: Int) {} 
     override fun onDrawerClosed(drawerView: View) { 
      // This method will be called after drawer animation finishes 
      // Perform the fragment replacement 
      when (item.itemId) { 
       R.id.drawer_item_1 -> { 
        supportFragmentManager.beginTransaction() 
          .replace(R.id.container, MyFragmentOne.newInstance()) 
          .addToBackStack(null) 
          .commit() 
       } 
       R.id.drawer_item_2 -> { 
        supportFragmentManager.beginTransaction() 
          .replace(R.id.container, MyFragmentTwo.newInstance()) 
          .addToBackStack(null) 
          .commit() 
       } 
      } 

      // Cross fade back the content container and hide progress bar 
      crossfade(container, progressBar, false) 

      // Remove this listener so close by, for example, swiping do not call it again 
      drawerLayout.removeDrawerListener(this) 
     } 
    }) 

    // Closes the drawer, triggering the listener above 
    drawerLayout.closeDrawer(GravityCompat.START) 
    return true 
} 

private fun crossfade(viewIn: View, viewOut: View, animateViewOut: Boolean = true) { 

    val crossfadeDuration = 200L 

    // Set the content view to 0% opacity but visible, so that it is visible 
    // (but fully transparent) during the animation. 
    viewIn.alpha = 0f 
    viewIn.visibility = View.VISIBLE 
    viewIn.bringToFront() 

    // Animate the in view to 100% opacity, and clear any animation 
    // listener set on the view. 
    viewIn.animate() 
      .alpha(1f) 
      .setDuration(crossfadeDuration) 
      .setListener(null) 

    // Animate the out view to 0% opacity. After the animation ends, 
    // set its visibility to GONE as an optimization step (it won't 
    // participate in layout passes, etc.) 
    viewOut.animate() 
      .alpha(0f) 
      .setDuration(if (animateViewOut) crossfadeDuration else 0) 
      .setListener(object : AnimatorListenerAdapter() { 
       override fun onAnimationEnd(animation: Animator) { 
        viewOut.visibility = GONE 
       } 
      }) 

} 

裏面你的XML,你應該有這樣的事情:

... 
<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:openDrawer="start"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <FrameLayout 
      android:id="@+id/container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:visibility="gone" /> 

     <ProgressBar 
      android:id="@+id/progressBar" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:visibility="gone" /> 

    </FrameLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigationView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:menu="@menu/drawer" /> 

</android.support.v4.widget.DrawerLayout> 
....