当我在抽屉中选择一个选项并更改为另一个选项时,突出显示不会改变。我的意思是,第一个选定的选项将保持突出显示,第二个选项将不会被突出显示。我一直在寻找许多博客的答案,但没有发现任何对我有用的。
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 :(得分:0)
navigationView.setCheckedItem(id);
此方法在API 23.0.0中引入
您也应该查看performIdentifierAction。它应该选择指定的项目并执行与之关联的操作。
答案 1 :(得分: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);
}
}