禁用导航抽屉项目(如果已选中),否则应启用抽屉项目

时间:2015-01-05 08:57:03

标签: android navigation-drawer

我使用导航抽屉。导航抽屉中包含元素/项目(如:反馈,报告,设置,刷新)。当我点击一个项目时,根据所选的选项,UI将会更新。

问题是:我从导航抽屉中选择了一个项目,然后更新了UI。如果我再次选择相同的选项,则再次调用所有更新UI的方法。

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

我尝试将可点击属性设置为false,但该项目变为不可点击。我无法选择该项目。

假设我选择了反馈选项,然后下次当我再次打开抽屉时,则不应选择反馈选项。意味着,连续用户不应该选择相同的选项。

添加了Code :: Adapter Class:

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

MenuItem interface ::

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

    }
}

1 个答案:

答案 0 :(得分:0)

我得到了答案。需要保持lastClicked位置。如果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;
    }
}