2016-06-09 68 views
0

當我在抽屜中選擇一個選項並切換到另一個選項時,突出顯示不會改變。我的意思是,第一個選擇的選項將保持突出顯示,第二個選項將不會顯示。我一直在許多博客中尋找答案,但沒有找到任何適合我的工作。導航抽屜突出顯示不能正常工作

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemClickListener{ 

    ListView listView; 
    List<RowItem> rowItems; 
    private int navId; 
    private static final String NAV_ITEM_ID = "navId"; 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

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

     if (null == savedInstanceState){ 
      navId = R.id.nav_home; 
     } else { 
      navId = savedInstanceState.getInt(NAV_ITEM_ID); 
     } 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 

     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(); 

     ImageView profilePic = (ImageView)findViewById(R.id.profileImageView); 


     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     navigationView.getMenu().findItem(navId).setChecked(true); 

     rowItems = new ArrayList<RowItem>(); 
     for (int i = 0; i < DownloadFromServer.titles.length; i++) { 
      RowItem item = new RowItem(DownloadFromServer.images[i], DownloadFromServer.titles[i], 
        DownloadFromServer.descriptions[i], DownloadFromServer.puntos[i], 
        DownloadFromServer.puntuacion[i]); 
      rowItems.add(item); 
     } 

     // Get ListView object from xml 
     listView = (ListView) findViewById(R.id.list); 

     // Define a new Adapter 
     // First parameter - Context 
     // Second parameter - Layout for the row 
     // Third - the Array of data 

     CustomListViewAdapter adapter = new CustomListViewAdapter(this, 
       R.layout.principal_list_item, rowItems) { 
     }; 


     // Assign adapter to ListView 
     listView.setAdapter(adapter); 

     // ListView Item Click Listener 
     listView.setOnItemClickListener(this); 

    } 

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

     Intent intent = new Intent(this, PopupListCard.class); 
     intent.putExtra("Title", DownloadFromServer.titles[position]); 
     intent.putExtra("Desc", DownloadFromServer.descriptions[position]); 
     intent.putExtra("Image",DownloadFromServer.images[position].toString()); 
     intent.putExtra("Puntuacion", String.valueOf(DownloadFromServer.puntuacion[position])); 
     intent.putExtra("Puntos", String.valueOf(DownloadFromServer.puntos[position])); 

     startActivity(intent); 

    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      finish(); 
      BaseActivity.hideProgressDialog(); 
     } 
    } 


    @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(); 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 

     navId = item.getItemId(); 

     if (navId == R.id.nav_home) { 
      // Handle the action 

     } else if (navId == R.id.nav_my_profile){ 

      Intent intent = new Intent(this, MyProfile.class); 
      startActivity(intent); 

     } else if (navId == R.id.nav_buy_puntos) { 

     } else if (navId == R.id.nav_settings){ 

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

     } else if (navId == R.id.nav_feedback) { 

      Intent intent = new Intent(this, FeedbackActivity.class); 
      startActivity(intent); 

     } else if (navId == R.id.nav_help) { 

     } else if (navId == R.id.nav_log_out){ 

      finish(); 
      BaseActivity.hideProgressDialog(); 

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

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.gummyfish.appuntes3/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.gummyfish.appuntes3/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 

    protected void onSaveInstanceState (final Bundle outState){ 
     super.onSaveInstanceState(outState); 
     outState.putInt(NAV_ITEM_ID, navId); 
    } 
} 
+0

你可以做到這一點'navigationView.getMenu()的getItem(indexOfTheItem).setChecked(真);' – Vucko

回答

0
navigationView.setCheckedItem(id); 

這種方法在API 23.0.0

介紹你應該看看performIdentifierAction爲好。它應該選擇指定的項目並執行與之關聯的操作。

0

可能有很多方法可以解決這個問題。

第一次製作navigationView公開。 然後首先清除所有選中的clearChecked(),然後設置選中的navigationView.getMenu().findItem(navId).setChecked(true);

查找以下對代碼的更改。

private NavigationView navigationView; 

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 

    navId = item.getItemId(); 
    clearChecked(); 
    navigationView.getMenu().findItem(navId).setChecked(true); 

    if (navId == R.id.nav_home) { 
     // Handle the action 

    } else if (navId == R.id.nav_my_profile){ 

     Intent intent = new Intent(this, MyProfile.class); 
     startActivity(intent); 

    } else if (navId == R.id.nav_buy_puntos) { 

    } else if (navId == R.id.nav_settings){ 

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

    } else if (navId == R.id.nav_feedback) { 

     Intent intent = new Intent(this, FeedbackActivity.class); 
     startActivity(intent); 

    } else if (navId == R.id.nav_help) { 

    } else if (navId == R.id.nav_log_out){ 

     finish(); 
     BaseActivity.hideProgressDialog(); 

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

private void clearChecked() { 
     int size = navigationView.getMenu().size(); 
     for (int i = 0; i < size; i++) { 
      navigationView.getMenu().getItem(i).setChecked(false); 
     } 
    } 
+0

非常感謝你的幫助。但是,我在clearChecked()上遇到錯誤,無法找出它的根。這是消息:'試圖調用虛擬方法'android.view.Menu android.support.design.widget.NavigationView.getMenu()'在一個空對象引用' – Nkuutz

+0

請保持您的其他代碼,因爲它。我想你並沒有初始化navigationView。 – keshav

+0

它是如何初始化的?抽屜用於工作,它只是沒有突出顯示正確的選項。我的意思是,如果它沒有初始化,它怎麼會起作用?因爲我沒有改變那部分代碼 – Nkuutz