我有一个带有列表视图的导航抽屉有7个项目,但是当我点击最后一个名为Logout的项目打开一个对话框时,我遇到了一个小问题。
问题是,当我点击任何打开活动的项目时,项目会被正确突出显示,但是当我点击注销时,之前点击的项目和点击的注销项目都会突出显示,我希望注销项目不应该突出显示。
我的代码:
public int selected_Drawer_Item = 0;
public class NavArrayAdapter extends ArrayAdapter {
Context context;
ArrayList<String> navList;
int[] images;
public NavArrayAdapter(Context context, ArrayList<String> navList,
int[] images) {
super(context, R.layout.row_item_nav, R.id.txtRow, navList);
this.context = context;
this.navList = navList;
this.images = images;
}
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row_item_nav, parent,
false);
TextView textView = (TextView) view.findViewById(R.id.txtRow);
ImageView imageView = (ImageView)
view.findViewById(R.id.imgNav);
RelativeLayout relativeLayout = (RelativeLayout)
view.findViewById(R.id.rel_lay_row_item);
textView.setText(navList.get(position));
imageView.setImageResource(images[position]);
if (Constants.selected_Drawer_Item == position) {
relativeLayout.setBackgroundColor
(context.getResources().getColor
(R.color.listviewitemselect));
textView.setTextColor(context.getResources().
getColor(R.color.dialogOKtext));
}
return view;
}
}
我的Listview onitem点击
mDrawerList.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View
view, final int i, long l) {
mDrawerLayout.closeDrawers();
mDrawerLayout.postDelayed(new Runnable() {
@Override
public void run() {
switch (i) {
case 0:
Intent intent = new
Intent(DrawerActivity.this, Home.class);
startActivity(intent);
break;
case 1:
Intent intent1 = new
Intent(DrawerActivity.this,
Add_Account.class);
startActivity(intent1);
break;
...... till case 5 same as above
case 6:
final Dialog dialog = new
Dialog(DrawerActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
RelativeLayout.LayoutParams dialogParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dislogView = inflater.inflate(R.layout.succes_dialog, null);
TextView dialogheader = (TextView) dislogView.findViewById(R.id.textDialogHeader);
TextView dialogmessage = (TextView) dislogView.findViewById(R.id.textDialogMessage);
TextView btnOk = (TextView) dislogView.findViewById(R.id.textDialogOk);
TextView btnNo = (TextView) dislogView.findViewById(R.id.textDialogNo);
dialogheader.setText("Logout");
dialogmessage.setText("Are you sure you want to logout ?");
btnNo.setVisibility(View.VISIBLE);
btnOk.setText("YES");
btnOk.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
dialog.dismiss();
Intent intent = new Intent(DrawerActivity.this, Login.class);
Bundle bndlanimation = ActivityOptions.makeCustomAnimation(
getBaseContext(), R.anim.open_translate,
R.anim.close_translate).toBundle();
startActivity(intent, bndlanimation);
finish();
}
});
btnNo.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.setContentView(dislogView, dialogParams);
dialog.show();
break;
}
}
}, 300);
}
});
答案 0 :(得分:2)
只需更新适配器中的条件,您的问题就会得到解决。
if (Constants.selected_Drawer_Item == position) {
relativeLayout.setBackgroundColor
(context.getResources().getColor
(R.color.listviewitemselect));
textView.setTextColor(context.getResources().
getColor(R.color.dialogOKtext));
}
这里改变如果这样的条件
if (Constants.selected_Drawer_Item == position && position != 6) {
/* Here I assumed that your logout menu option's position is 7th so this will work on position 6th*/
relativeLayout.setBackgroundColor
(context.getResources().getColor
(R.color.listviewitemselect));
textView.setTextColor(context.getResources().
getColor(R.color.dialogOKtext));
}
使用这些行来修改你的onItemClick,
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View
view, final int i, long l) {
Constants.selected_Drawer_Item = i;
.........
mDrawerLayout.closeDrawers();
mDrawerLayout.postDelayed(new Runnable() {
@Override
public void run() {
switch (i) {
case 0:
.............
break;
}
mDrawerListAdapter.notifyDataSetChanged();
}
}, 300);
}