0

Hiho!Android: меню навигации не работает с кнопкой, но с сенсорным

Я создаю ящик для навигации в своем приложении для Android. Я использовал howto от http://blog.teamtreehouse.com/add-navigation-drawer-android и модифицировал его в одном месте макета. Я могу активировать меню, когда я прикасаюсь и качаю пальцем слева направо. Но если я прикоснусь к меню гамбургера, ничего не скажу. Что не так? Если я нажал в меню, значок изменился на стрелку, так что есть соединение. Надеюсь, что кто-то может мне помочь.

Мой Java Class (Основная деятельность):

public class AmericanFootball extends AppCompatActivity { 
    private MenuItem mi; 
    public static Context context; 
    private ListView mDrawerList; 
    private ArrayAdapter<String> mAdapter; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private DrawerLayout mDrawerLayout; 
    private String mActivityTitle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     context = (Context) this; 
     setContentView(R.layout.activity_american_football); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle(getText(R.string.app_name)); 
     setSupportActionBar(toolbar); 

     mDrawerList = (ListView)findViewById(R.id.navList); 
     mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout); 
     mActivityTitle = getTitle().toString(); 

     addDrawerItems(); 
     setupDrawer(); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 

     checkInternet ci = new checkInternet(); 
     boolean internet=ci.isNetzwerkVerfuegbar((Context) this); 

     TextView tv = (TextView) findViewById(R.id.tv_hello); 
     tv.setTypeface(FontManager.getTypeface((Context) this, FontManager.FONTAWESOME)); 
     tv.setText(R.string.fa_icon_areachart); 
     tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryLight)); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_american_football, menu); 

     MenuItem mi = menu.findItem(R.id.action_refresh); 

     TextDrawable td = new TextDrawable((Context) this); 
     td.setText(getString(R.string.fa_refresh)); 
     td.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryLight)); 
     td.setTextSize(30); 
     mi.setIcon(td); 

     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ImageView iv = (ImageView)inflater.inflate(R.layout.iv_refresh, null); 
     iv.setImageDrawable(td); 

     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) { 
      return true; 
     } 

     if (id == R.id.action_refresh) { 
      doRefreshGames(item); 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    private void addDrawerItems() { 
     String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" }; 
     mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray); 
     mDrawerList.setAdapter(mAdapter); 

     mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(AmericanFootball.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    private void setupDrawer() { 
     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) { 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getSupportActionBar().setTitle("Navigation!"); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       getSupportActionBar().setTitle(mActivityTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 

     mDrawerToggle.setDrawerIndicatorEnabled(true); 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 
    } 
} 

Мой layoutfile:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="k0f.de.americanfootball.AmericanFootball"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_american_football" /> 

<android.support.v4.widget.DrawerLayout 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 
    <ListView 
     android:id="@+id/navList" 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left|start" 
     android:background="#ffeeeeee"/> 

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

ответ

0

Я нашел ошибку. Забыл маленькую вещь:

public boolean onOptionsItemSelected(MenuItem item) { 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^