带有ListView的导航抽屉突出显示了对话框盒的两个列表项目



我有一个带有7个项目的导航抽屉,但是当我单击最后一个称为注销的项目时,我遇到了一个小问题,该项目打开对话框。

问题是当我单击任何打开活动的项目时,该项目得到正确突出显示,但是当我单击注销时,先前单击的项目和注销项目点击都会突出显示,我希望注销项目不应该注销项目突出显示。

我的代码:

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

如果适配器中的条件,您的问题将得到解决。

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

用这些行udpate youtemclick,

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

最新更新