package net.tasksnow.view.slidingmenu;
import android.app.Fragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
import net.tasksnow.R;
import net.tasksnow.util.ColorUtil;
import com.luma.android.andrologger.Logger;
/**
* @author D056943
* @since 20:02:49 - 19.12.2012
* @project cFoldersDemo
*/
public class MenuSectionItem extends AbstractMenuItem {
// ===========================================================
// Interfaces
// ===========================================================
public interface OnExpandCollapseListener {
public void onExpandStateChanged(MenuSectionItem sectionItem, boolean detailsSelected);
}
protected OnExpandCollapseListener expandCollapseListener;
public void setOnExpandCollapseListener(OnExpandCollapseListener expandCollapseListener) {
this.expandCollapseListener = expandCollapseListener;
}
// ===========================================================
// Constants
// ===========================================================
public static final int ITEM_TYPE = 1;
// ===========================================================
// Fields
// ===========================================================
private final String text;
private final int iconRes;
private final Fragment execFragment;
private boolean expanded = false;
private boolean expandable = false;
// ===========================================================
// Constructors
// ===========================================================
public MenuSectionItem(String text, int iconRes, Fragment execFragment) {
this.text = text;
this.iconRes = iconRes;
this.execFragment = execFragment;
this.setOnExpandCollapseListener(expandCollapseListener);
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public int getType() {
return ITEM_TYPE;
}
@Override
public View getListItemView(View convertView, Context context) {
// if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.menu_section_item, null);
// }
ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
icon.setImageResource(this.iconRes);
TextView title = (TextView) convertView.findViewById(R.id.row_title);
title.setText(this.text);
View selector = convertView.findViewById(R.id.selectionHandler);
ToggleButton expandButton = (ToggleButton) convertView.findViewById(R.id.expand_collapse_toggle);
expandButton.setChecked(this.isExpanded());
if (this.isExpandable()) {
expandButton.setVisibility(View.VISIBLE);
expandButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { //TODO Wird Listener nicht neu erzeugt?!?
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Logger.i(text + isChecked); //TODO Wird kurz vor updateList in false umgewandelt -> crash da die Kind Elemente dann nicht geladen werden!
setExpanded(isChecked);
if (getOnListItemStateChangeListener() != null) {
getOnListItemStateChangeListener().onListItemStateChange(MenuSectionItem.this);
}
}
});
}
else
expandButton.setVisibility(View.INVISIBLE);
if (this.isSelected()) {
selector.setVisibility(View.VISIBLE);
convertView.setBackgroundColor(ColorUtil.setAlpha(ColorUtil.darkerColor(R.color.menuselector_background), 95));
}
else {
selector.setVisibility(View.INVISIBLE);
convertView.setBackgroundDrawable(null);//TODO
}
return convertView;
}
@Override
public Object onExecuteAction() {
this.setSelected(true);
return this.execFragment;
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
public String getText() {
return this.text;
}
public boolean isExpandable() {
return expandable;
}
public void setExpandable(boolean expandable) {
this.expandable = expandable;
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}