Marcos de la Cruz Sippel

Softwareentwicklung – Web, Facebook, Android

Marcos de la Cruz Sippel

Als Freelancer habe ich mich auf den Bereich Webentwicklung  spezialisiert. Falls sie einen Programmierer mit fundierten Kenntnissen in PHP, JavaScript, CSS, XML und JQuery zur Entwicklung ihrer eigenen Webpräsenz benötigen, können sie mich gerne kontaktieren. Auch für die Integration von Facebook in ihrer Webseite oder der Entwicklung einer Facebook App kann ich ihnen behilflich sein.

Neben der Webentwicklung bin ich auch, dank meiner Kenntnisse in Java, in der Entwicklung Mobiler Applikationen tätig. Zur Zeit beschränkt sich mein Angebot auf Applikationen für das Mobile Betriebssystem Android.

Falls sie selbst noch kein Konzept haben, übernehme ich auch die Erstellung des Konzepts anhand ihrer Vorstellungen und berate sie gerne über unterschiedliche Möglichkeiten ihre Ideen zu realisieren.

Android ExpandableListAdapter für eine ExpandableListView mit einem footer in jeder Gruppe


Vor kurzem saß ich vor dem Problem das ich als letzte View in einer Gruppe einer ExpandableListView ein Button brauchte.  Zwar kann man der ExpandableListView Header und Footer hinzufügen, doch diese sind nicht für jede Gruppe sondern für die gesamte Liste gedacht. Die folgende Klasse soll als ein Beispiel gelten wie man jeder Gruppe einen footer zuordnen kann.

Was im ListAdapter zu tun ist:

1. Der Rückgabewert der Methode getChildrenCount muss um 1 erhöht werden, da ja nun in jeder Gruppe der Footer mit drin ist

2. In der Methode getChildView muss untersucht werden ob es sich um das letzte Element der Gruppe handelt. Falls dies der Fall ist wird eine anderes Layout geladen, bspw. das von einem Button. Handelt es sich nicht um das letzte Element wird ganz normal vorgegangen wie man es sonst bei eine ExpandableListView machen würde.

Der untenstehende Code veranschaulicht nochmal etwas besser wie es funktioniert:

package mseo.utils;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ExpandableListAdapter {

	/*
	 * The id of the footer layout
	 */
	private int footer;

	/*
	 * The application context
	 */
	private Context context;

	/*
	 * The data for the list
	 */
	private ArrayList<?> data;

	public ExpandableListAdapter(Context context, ArrayList<?> data, int footer) {
		this.footer = footer;
		this.context = context;
		this.data = data;
	}

	/*
	 * Get the children count
	 *
	 * @param groupPosition Group-position to get the children count
	 */
	public int getChildrenCount(int groupPosition) {
		int childrenCount;
		// For example:
		// childrenCount = data.get(groupPosition).getChildCount();
		return childrenCount + 1;
	}

	/*
	 * Get a specific child of a specific group
	 *
	 * @param groupPosition The group position
	 *
	 * @param childPosition The child position
	 *
	 * @return The child object
	 */
	public Object getChild(int groupPosition, int childPosition) {
		Object child;
		// For example
		// child = data.get(groupPosition).getChildAt(childPosition);
		return child;
	}

	/*
	 * Get the id of a specific child of a specific group
	 *
	 * @param groupPosition The group position
	 *
	 * @param childPosition The child position
	 *
	 * @return The child id
	 */
	public long getChildId(int groupPosition, int childPosition) {
		// Change if unique id needed
		return childPosition;
	}

	/*
	 * Get a specific group
	 *
	 * @param groupPosition The group position
	 *
	 * @return The group object
	 */
	public Object getGroup(int groupPosition) {
		return this.data.get(groupPosition);
	}

	/*
	 * Get the group count
	 *
	 * @return The group count
	 */
	public int getGroupCount() {
		return this.data.size();
	}

	/*
	 * Get the id of a specific group
	 *
	 * @param groupPosition The group position
	 *
	 * @return The id of the group
	 */
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	/*
	 * Get a specific child View
	 *
	 * @param groupPosition The group position
	 *
	 * @param childPosition The child position
	 *
	 * @param isLastChild Boolean to indicate if the child is the last child of
	 * the group
	 *
	 * @param convertView The view of the child - if not set -> null
	 *
	 * @param parentView The view of the parent - if not set -> null
	 *
	 * @return The child view
	 */
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View rowView = convertView;
		LayoutInflater infl = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (isLastChild) {
			// Child is last Child ->
			if (rowView == null || (String) rowView.getTag() != "footer") {
				rowView = infl.inflate(this.footer, null);
				rowView.setTag("footer");
			}
			// Example of a button as footer
			Button button = (Button) rowView.findViewById(R.id.button);
			button.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// Things to do on button click
				}
			});
			return rowView;
		}

		// Child is not last child -> normal list item
		if (rowView == null || rowView.getTag() != "normal_item") {
			rowView = infl.inflate(R.layout.expandable_list_child_item, null);
			rowView.setTag("normal_item");
		}

		// Set data of normal child item -> for example src of an imageview, etc

		return rowView;
	}

	/*
	 * Get a specific child View
	 *
	 * @param groupPosition The group position
	 *
	 * @param isExpanded Boolean to indicate if the group is expanded
	 *
	 * @param view The view of the group - if not set -> null
	 *
	 * @param parent The view of the parent - if not set -> null
	 *
	 * @return The group view
	 */
	public View getGroupView(int groupPosition, boolean isExpanded, View view,
			ViewGroup parent) {
		final int pos = groupPosition;
		LayoutInflater infl = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = infl.inflate(R.layout.expandable_list_group_item, null);

		// Set the data for group item -> for example set text of a textview
		// etc.

		return view;
	}

}