androidlayoutexpandablelistadapter

Redrawing of SlidingMenu programmatically


I am using SlidingMenu from Jeremy Feinstein with success. In the slide menu I create a list of items as can be seen in the next image.

enter image description here

i've managed to make it work properly until now that I've decided to add an indicator (a rectangle coloured) at the left of the selected item which is giving me some troubles.

I've tried some solutions, like destroying the RelativeLayout that represents the SlideMenu but in some cases there are more than 200 items, and re-create it again is so much time consuming.

Other solution that didn't work was to get the View that is associated with the item, and change the background color programatically. The problem here is when there are more than 12 items (the maximum displayed in my screen). In that case if I try to change the colour of the 15th item (i.e.) I get a NullPointerException.

Eventually I managed to do it elegantly:

private void updateSlideViewIndicator(int id_from, int id_to) {
    SectionItem si = (SectionItem) slidePreguntas.getSectionListAdapter().getChild(0, id_from);
    si.setActualItem(false);
    si = (SectionItem) slidePreguntas.getSectionListAdapter().getChild(0, id_to);
    si.setActualItem(true);
}

With this code, I change a private field in the Item object that help me clear the item "from" I come, and highlight the item "to" I am going.

Moreover, I look at that field in the getChildView in the SectionListAdapter.java:

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.pregunta_slide_menu_sectionitem,
                parent, false);
    }

    SectionItem oSectionItem = this.sections.get(groupPosition).getSectionItems().get(childPosition);

    TextView titleView = (TextView) convertView
            .findViewById(R.id.slidingmenu_sectionitem_label);
    titleView.setText(oSectionItem.getTitle());

    TextView textView = (TextView) convertView
            .findViewById(R.id.slidingmenu_sectionitem_text);
    textView.setText(oSectionItem.getText());

    View indicator = (View) convertView
            .findViewById(R.id.slidingmenu_sectionitem_indicator);

    if (oSectionItem.isActualItem()) {
        indicator.setBackgroundColor(this.context.getResources()
                .getColor(R.color.marca));
    } else {
        indicator.setBackgroundColor(this.context.getResources()
                .getColor(R.color.blanco));
    }

    return convertView;
}

The problem with this solution is that the green indicator is only updated when I scroll the slideMenu beyond the visibility of the item selected and returned it back. As long as I hide the slideMenu and show it again without scrolling, the higlighted item remains wrong. I've tried also to setVisibility(Invisible) and Visible again but it didn't work either. I thought that maybe that way the getChildView function would be called but it wasn´t.

I would like that the selected item will be highlited as soos as it is selected, and that there wasn't necessary to scroll the list and come back again to see it correctly.

I am sure there has to be a solution for this, does somebody have a clue about this?


Solution

  • Why don't you try using

    invalidateViews() method on your View?