2016-02-14 200 views
0

我正在關注YouTube上的教程,但我被困在(+/- 41分鐘)(https://www.youtube.com/watch?v=Zuo3n-DjF7w)。我希望看到一個指示,當我點擊一個菜單項,但是這不會發生。下圖顯示了它是如何應該看看: Picture錯誤導航抽屜

怎樣看:抽屜式導航教程主要代碼:

package com.example.flori.mylordring; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Adapter; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ImageButton; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{ 


    private static ImageButton imgView; 
    private int test; 
    int [] images = {R.drawable.im1,R.drawable.im2,R.drawable.im3,R.drawable.im4,R.drawable.im5,R.drawable.im6,R.drawable.im7}; 
    private ActionBarDrawerToggle actionBarDrawerToggle; 
    private DrawerLayout drawerLayout; 
    private ListView navList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout); 
     navList = (ListView)findViewById(R.id.navlist); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     ArrayList<String> navArray = new ArrayList<String>(); 
     navArray.add("Home"); 
     navArray.add("Fragment 1"); 
     navArray.add("Fragment 2"); 
     navArray.add("Fragment 3"); 
     navArray.add("Fragment 4"); 
     navArray.add("Fragment 5"); 
     navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,navArray); 
     navList.setAdapter(adapter); 

     actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer); 
     drawerLayout.setDrawerListener(actionBarDrawerToggle); 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayShowHomeEnabled(true); 
     actionBar.setDisplayHomeAsUpEnabled(true); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Rolololo", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 

    @Override 
    protected void onPostCreate(Bundle saveInstancesState){ 
     super.onPostCreate(saveInstancesState); 
     actionBarDrawerToggle.syncState(); 
    } 


    public void image(View view){ 
     imgView=(ImageButton)findViewById(R.id.imageButton); 
     imgView.setOnClickListener(new View.OnClickListener() { 
             @Override 
             public void onClick(View v) { 
              test++; 
              test = test % images.length; 
              imgView.setImageResource(images[test]); 
             } 
            } 
     ); 
    } 

    @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_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) { 
      return true; 
     }else if(id == android.R.id.home){ 
      if(drawerLayout.isDrawerOpen(navList)){ 
       drawerLayout.closeDrawer(navList); 
      }else{ 
       drawerLayout.openDrawer(navList); 
      } 
     } 

     return super.onOptionsItemSelected(item); 
    } 

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

       break; 
      case 1: 

       break; 
      case 2: 

       break; 
      case 3: 

       break; 
      case 4: 

       break; 
      case 5: 

       break; 
     } 
    } 
} 

預先感謝您;)

回答

0

在你onItemClick方法,你沒有任何事情就會突破每一個案例,並且你實現了OnItemClickListener接口,如果你沒有指定在這些情況下會發生什麼,那什麼都不會發生。請在這些情況下指定您想要的,而不是突破。您可以添加一個敬酒,看看發生了什麼事情,

樣的情況下0:Toast.makeText(yourContext, "first item", Toast.LENGTH SHORT).show(); break;

+0

是的,我aggree你,但是,教程,代碼是我的一樣,但是他,當他點擊它喜歡上了做了圖片。 –