禁用导航抽屉项目,如果它已经被选中,否则应该启用抽屉项目

Disable navigation drawer item, if it is already selected, otherwise drawer item should be enabled

我正在使用导航抽屉。导航抽屉包含 elements/items(例如:反馈、报告、设置、刷新)。当我点击一个项目然后根据 selected 选项 UI 将得到更新。

问题在于:我 select 从导航抽屉中编辑了一个项目,然后 UI 得到更新。如果我再次 selected 相同的选项,那么所有更新 UI 的方法都会被调用。

我想要的是,如果用户再次单击导航抽屉中的同一项目,则不应调用后台方法。

我尝试将 clickable 属性设置为 false,但随着该项目变得不可点击。我无法 select 该项目。

假设我 selected 反馈选项,那么下次我再次打开抽屉时,反馈选项应该不会 selected。意思是,连续的用户不应该select相同的选项。

添加代码::适配器类:

public class NavDrawerAdapter extends ArrayAdapter<NavDrawerItem> {

private LayoutInflater inflater;

public NavDrawerAdapter(Context context, int textViewResourceId,
        NavDrawerItem[] objects) {
    super(context, textViewResourceId, objects);
    this.inflater = LayoutInflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    NavDrawerItem menuItem = this.getItem(position);
    if (menuItem.getType() == NavMenuItem.ITEM_TYPE) {
        view = getItemView(convertView, parent, menuItem);
    } else {
        view = getSectionView(convertView, parent, menuItem);
    }
    return view;
}

public View getItemView(View convertView, ViewGroup parentView,
        NavDrawerItem navDrawerItem) {

    NavMenuItem menuItem = (NavMenuItem) navDrawerItem;
    NavMenuItemHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.navdrawer_item, parentView,
                false);
        TextView labelView = (TextView) convertView
                .findViewById(R.id.navmenuitem_label);
        ImageView iconView = (ImageView) convertView
                .findViewById(R.id.navmenuitem_icon);

        navMenuItemHolder = new NavMenuItemHolder();
        navMenuItemHolder.labelView = labelView;
        navMenuItemHolder.iconView = iconView;

        convertView.setTag(navMenuItemHolder);
    }

    if (navMenuItemHolder == null) {
        navMenuItemHolder = (NavMenuItemHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuItem.getLabel());
    navMenuItemHolder.iconView.setImageResource(menuItem.getIcon());

    return convertView;
}

public View getSectionView(View convertView, ViewGroup parentView,
        NavDrawerItem navDrawerItem) {

    NavMenuSection menuSection = (NavMenuSection) navDrawerItem;
    NavMenuSectionHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.navdrawer_section,
                parentView, false);
        TextView labelView = (TextView) convertView
                .findViewById(R.id.navmenusection_label);

        navMenuItemHolder = new NavMenuSectionHolder();
        navMenuItemHolder.labelView = labelView;
        convertView.setTag(navMenuItemHolder);
    }

    if (navMenuItemHolder == null) {
        navMenuItemHolder = (NavMenuSectionHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuSection.getLabel());

    return convertView;
}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return this.getItem(position).getType();
}

@Override
public boolean isEnabled(int position) {
    return getItem(position).isEnabled();
}

private static class NavMenuItemHolder {
    private TextView labelView;
    private ImageView iconView;
}

private class NavMenuSectionHolder {
    private TextView labelView;
}
}

活动类::

 public class NavDrawerActivity extends AbstractNavDrawerActivity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if ( savedInstanceState == null ) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,  new MainFragment()).commit();
    }
}

@Override
protected NavDrawerActivityConfiguration getNavDrawerConfiguration() {

    NavDrawerItem[] menu = new NavDrawerItem[] {
            NavMenuSection.create( 100, "Logged in user details"),
            //NavMenuItem.create(101,"List/Detail (Fragment)", "navdrawer_friends", true, this),
            //NavMenuItem.create(102, "Airport (AsyncTask)", "navdrawer_airport", false, this),
            //NavMenuSection.create(200, "General"),
            NavMenuItem.create(202, getResources().getString(R.string.nav_drawer_item_reports), (R.drawable.drawer_shadow), false, this),
            NavMenuItem.create(203, getResources().getString(R.string.nav_drawer_item_feedback), (R.drawable.drawer_shadow), false, this),
            NavMenuItem.create(204, getResources().getString(R.string.nav_drawer_item_settings), (R.drawable.drawer_shadow), false, this)};

    NavDrawerActivityConfiguration navDrawerActivityConfiguration = new NavDrawerActivityConfiguration();
    navDrawerActivityConfiguration.setMainLayout(R.layout.activity_nav_drawer);
    navDrawerActivityConfiguration.setDrawerLayoutId(R.id.drawer_layout);
    navDrawerActivityConfiguration.setLeftDrawerId(R.id.left_drawer);
    navDrawerActivityConfiguration.setNavItems(menu);
    navDrawerActivityConfiguration.setDrawerShadow(R.drawable.drawer_shadow);     
    navDrawerActivityConfiguration.setDrawerOpenDesc(R.string.drawer_open);
    navDrawerActivityConfiguration.setDrawerCloseDesc(R.string.drawer_close);
    navDrawerActivityConfiguration.setBaseAdapter(
        new NavDrawerAdapter(this, R.layout.navdrawer_item, menu ));
    return navDrawerActivityConfiguration;
}

@Override
protected void onNavItemSelected(int id) {
    switch ((int)id) {
    case 202:

        getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();

        break;
    case 203:
        //getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new AirportFragment()).commit();
        Toast.makeText(this, "Friend", Toast.LENGTH_SHORT);
        break;
    }
}
}

菜单项界面::

public interface NavDrawerItem {

public int getId();
public String getLabel();
public int getType();
public boolean isEnabled();
public boolean updateActionBarTitle();
}

实现类:

public class NavMenuItem implements NavDrawerItem {

public static final int ITEM_TYPE = 1 ;

private int id ;
private String label ; 
private int icon ;
private boolean updateActionBarTitle ;

private NavMenuItem() {
}

public static NavMenuItem create( int id, String label, int icon, boolean updateActionBarTitle, Context context ) {
    NavMenuItem item = new NavMenuItem();
    item.setId(id);
    item.setLabel(label);
    item.setIcon(icon);
    item.setUpdateActionBarTitle(updateActionBarTitle);
    return item;
}

@Override
public int getType() {
    return ITEM_TYPE;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}

public int getIcon() {
    return icon;
}

public void setIcon(int icon) {
    this.icon = icon;
}

@Override
public boolean isEnabled() {
    return true;
}

@Override
public boolean updateActionBarTitle() {
    return this.updateActionBarTitle;
}

public void setUpdateActionBarTitle(boolean updateActionBarTitle) {
    this.updateActionBarTitle = updateActionBarTitle;
}
}

为抽屉侦听器添加了代码:

 private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        selectItem(position);

    }
}

我得到了答案。需要保持 lastClicked 位置。如果上次点击的元素与当前点击的元素相同,则无需执行任何操作。如果当前位置不同,则用当前位置更新 lastClicked。

private static int lastClicked = 0;

  private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if(lastClicked != position){
            selectItem(position);
        }
        lastClicked = position;
    }
}