NavigationDrawer 的 Android BaseAdapter 的随机行为

Random behavior with Android BaseAdapter for NavigationDrawer

我实现了 NavigationDrawer。它使用从 BaseAdapter 扩展而来的 NavigationDrawerListAdapter。我在 NavigationDrawer 中有 10 个列表项。我想用绿色字体设置 3 个项目,其余为黑色字体。它 "disables" configureEnabledDisabledNavDrawItems() 中的列表项。然后,在 NavDrawerListAdapter 的 renderNavDrawerItemByPos() 中,如果列表项是 "disabled".

,它将字体颜色设置为绿色

但是,我的问题是导航抽屉中的每个列表项似乎都是随机绘制的。例如,当我上下滑动导航抽屉时,随机列表项变成绿色。我完全不知道为什么。有什么想法吗?

NavigationDrawerActivity

public class NavigationDrawerActivity extends ActionBarActivity {
    public DrawerLayout mDrawerLayout;
    public ListView mDrawerList;
    public TypedArray navMenuIcons; // menu item icons
    public String[] navMenuTitles; // menu item titles
    public ArrayList<NavDrawerItem> navDrawerItems; // each drawer item
    public NavDrawerListAdapter adapter; // nav drawer adapter

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
        navDrawerItems = new ArrayList<NavDrawerItem>();
        addNavDrawerItems(); 
        adapter = new NavDrawerListAdapter(this.getApplicationContext(), navDrawerItems);
        mDrawerList.setAdapter(adapter);

        configureEnabledDisabledNavDrawItems()
    }

    private void configureEnabledDisabledNavDrawItems() {
        for(int i=1; i < navDrawerItems.size(); i++){
            adapter.setNavigationDrawerItem(i, true);
        }

        adapter.setNavigationDrawerItem(2, false); 
        adapter.setNavigationDrawerItem(3, false); 
        adapter.setNavigationDrawerItem(6, false);
    }
}

NavigationDrawerListAdapter

public class NavDrawerListAdapter extends BaseAdapter {
    private ArrayList<NavDrawerItem> navDrawerItems;
    private List<Boolean> enabledDrawerItems;
    private LayoutInflater mInflater;

    public NavDrawerListAdapter(Context context,
            ArrayList<NavDrawerItem> navDrawerItems)    {
        super();
        this.context = context;
        this.navDrawerItems = navDrawerItems;
        this.mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        this.headerView = null;

        // Toggle enabled/disabled navigation drawer items
        enabledDrawerItems = new ArrayList<Boolean>(Arrays.asList(new Boolean[navDrawerItems.size()]));
        Collections.fill(enabledDrawerItems, new Boolean(true));
    }

    @Override
    public int getCount() {
        return navDrawerItems.size();
    }

    @Override
    public Object getItem(int position) {
        return navDrawerItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)   {
        if(position == 0) {
            if(convertView == null){
                convertView = mInflater.inflate(R.layout.header_view, null);
                headerView = convertView;
            }
            return convertView;
        }

        if(convertView == null) { // Use the drawer list item view
            convertView = mInflater.inflate(R.layout.drawer_list_item, null);
        }

        renderNavDrawerItemByPos(position, convertView);

        return convertView;
    }

    private void renderNavDrawerItemByPos(int position, View convertView)   {
        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
        txtTitle.setText("test");

        if(enabledDrawerItems.get(position) == false) {
            txtTitle.setTextColor(ColorStateList.valueOf(Color.GREEN));
            convertView.setEnabled(enabledDrawerItems.get(position));
        }
    }

    @Override
    public boolean areAllItemsEnabled()     {
        return false;
    }

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

    public void setNavigationDrawerItem(int pos, boolean enabled)   {
        enabledDrawerItems.set(pos, enabled);
    }

    @Override
    public int getItemViewType (int position) {
        if(position == 0)
            return 0;
        else
            return 1;
    }

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

如果向 getView() 传递一个回收的 convertView,该回收 convertView 之前的标题为绿色,即使当前位置已启用,它仍将是绿色。这是因为 renderNavDrawerItemByPos() 只设置禁用位置的颜色。

一般来说,每当您拿到回收的 View 时,您需要确保其整个状态都针对新位置进行了正确设置。

如果默认的标题颜色是黑色,这样的方法可能有效:

private void renderNavDrawerItemByPos(int position, View convertView)   {
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    txtTitle.setText("test");

    boolean enabled = enabledDrawerItems.get(position);
    txtTitle.setTextColor(ColorStateList.valueOf(
      enabled ? Color.BLACK : Color.GREEN));
    convertView.setEnabled(enabled);
}