Sunday, 18 August 2013

Overriding ArrayAdapter lifecycle to add separators

Overriding ArrayAdapter lifecycle to add separators

I'm currently making a menu for my app, using a DrawerLayout and an
ArrayAdapter subclass to achieve something looking like Facebook's drawer
menu.
I currently have no problems creating the list, but now that it looks
good, i'd like to add separators between different kind of options (i.e.
user-related and application-related options) and a search bar on top of
the menu.
The code of my current ArrayAdaptor subclass is as following :
public class DrawerMenuAdapter extends ArrayAdapter<String>{
private Context context;
private String[] values;
private int resId;
public DrawerMenuAdapter(Context context, int textViewResourceId,
String[] values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
this.resId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(this.resId, parent, false);
TextView elementText =
(TextView)rowView.findViewById(R.id.element_text);
ImageView elementImage =
(ImageView)rowView.findViewById(R.id.element_icon);
String textValue = values[position];
elementText.setText(textValue);
//This switch adds the icons to the related elements
switch (position){
case 0:
elementImage.setImageResource(R.drawable.search);
break;
case 1:
elementImage.setImageResource(R.drawable.facebook_friends);
break;
case 2:
elementImage.setImageResource(R.drawable.flirts_history);
break;
case 3:
elementImage.setImageResource(R.drawable.premium);
break;
case 4:
elementImage.setImageResource(R.drawable.settings);
break;
case 5:
elementImage.setImageResource(R.drawable.share_app);
break;
case 6:
elementImage.setImageResource(R.drawable.cgu);
break;
}
return rowView;
}
}
I assume that I have to override the function that populates the ListView
by calling the getView function, but I can't find which function it is.

No comments:

Post a Comment