Go to the documentation of this file.00001 package com.darvds.ribbonmenu;
00002
00003
00004 import java.util.ArrayList;
00005
00006 import org.xmlpull.v1.XmlPullParser;
00007
00008 import org.ros.android.androidSensorMessage.R;
00009 import android.content.Context;
00010 import android.content.res.XmlResourceParser;
00011 import android.os.Parcel;
00012 import android.os.Parcelable;
00013 import android.util.AttributeSet;
00014 import android.view.LayoutInflater;
00015 import android.view.View;
00016 import android.view.ViewGroup;
00017 import android.view.animation.AnimationUtils;
00018 import android.widget.AdapterView;
00019 import android.widget.AdapterView.OnItemClickListener;
00020 import android.widget.BaseAdapter;
00021 import android.widget.ImageView;
00022 import android.widget.LinearLayout;
00023 import android.widget.ListView;
00024 import android.widget.TextView;
00025
00026 import com.darvds.ribbonmenu.RibbonMenuView.SavedState;
00027
00028 public class RibbonMenuView extends LinearLayout {
00029
00030 private ListView rbmListView;
00031 private View rbmOutsideView;
00032
00033 private iRibbonMenuCallback callback;
00034 private int button_id;
00035 private int icon1;
00036 private int icon2;
00037 private static ArrayList<RibbonMenuItem> menuItems;
00038
00039
00040 public RibbonMenuView(Context context) {
00041 super(context);
00042 button_id = -1;
00043 load();
00044 }
00045
00046 public RibbonMenuView(Context context, AttributeSet attrs) {
00047 super(context, attrs);
00048 button_id = -1;
00049 load();
00050 }
00051
00052
00053
00054
00055 private void load(){
00056
00057 if(isInEditMode()) return;
00058
00059
00060 inflateLayout();
00061
00062 initUi();
00063
00064
00065 }
00066
00067
00068 private void inflateLayout(){
00069
00070
00071
00072
00073 try{
00074 LayoutInflater.from(getContext()).inflate(R.layout.rbm_menu, this, true);
00075 } catch(Exception e){
00076
00077 }
00078
00079
00080 }
00081
00082 private void initUi(){
00083
00084 rbmListView = (ListView) findViewById(R.id.rbm_listview);
00085 rbmOutsideView = (View) findViewById(R.id.rbm_outside_view);
00086
00087 rbmOutsideView.setOnClickListener(new OnClickListener() {
00088
00089 @Override
00090 public void onClick(View v) {
00091 hideMenu();
00092
00093 }
00094 });
00095
00096
00097 rbmListView.setOnItemClickListener(new OnItemClickListener() {
00098
00099 @Override
00100 public void onItemClick(AdapterView<?> parent, View view,
00101 int position, long id) {
00102
00103 if(callback != null)
00104 callback.RibbonMenuItemClick(menuItems.get(position).id);
00105
00106 hideMenu();
00107 }
00108
00109 });
00110
00111
00112 }
00113
00114
00115 public void setMenuClickCallback(iRibbonMenuCallback callback){
00116 this.callback = callback;
00117 }
00118
00119 public void setMenuItems(int menu){
00120
00121 parseXml(menu);
00122
00123 if(menuItems != null && menuItems.size() > 0)
00124 {
00125 rbmListView.setAdapter(new Adapter());
00126
00127 }
00128
00129
00130
00131
00132 }
00133
00134
00135 public void setBackgroundResource(int resource){
00136 rbmListView.setBackgroundResource(resource);
00137 }
00138
00139 public void setItemIcon(int icon1,int icon2){
00140 this.icon1 = icon1;
00141 this.icon2 = icon2;
00142 }
00143
00144 public void clearButtonId(){
00145 button_id = -1;
00146 }
00147
00148 public int addItem(String text,int icon){
00149 RibbonMenuItem item = new RibbonMenuItem();
00150 button_id++;
00151 item.id = button_id;
00152 item.text = text;
00153 if(icon == -1){
00154 item.icon = icon1;
00155 }
00156 else{
00157 item.icon = icon2;
00158 }
00159 menuItems.add(item);
00160
00161 return button_id;
00162 }
00163
00164 public void showMenu(){
00165 rbmOutsideView.setVisibility(View.VISIBLE);
00166
00167 rbmListView.setVisibility(View.VISIBLE);
00168 rbmListView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.rbm_in_from_left));
00169
00170 }
00171
00172
00173 public void hideMenu(){
00174
00175 rbmOutsideView.setVisibility(View.GONE);
00176 rbmListView.setVisibility(View.GONE);
00177
00178 rbmListView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.rbm_out_to_left));
00179
00180 }
00181
00182
00183 public void toggleMenu(){
00184
00185 if(rbmOutsideView.getVisibility() == View.GONE){
00186 showMenu();
00187 } else {
00188 hideMenu();
00189 }
00190 }
00191
00192
00193 private void parseXml(int menu){
00194
00195 menuItems = new ArrayList<RibbonMenuView.RibbonMenuItem>();
00196
00197
00198 try{
00199 XmlResourceParser xpp = getResources().getXml(menu);
00200
00201 xpp.next();
00202 int eventType = xpp.getEventType();
00203
00204
00205 while(eventType != XmlPullParser.END_DOCUMENT){
00206
00207 if(eventType == XmlPullParser.START_TAG){
00208
00209 String elemName = xpp.getName();
00210
00211 if(elemName.equals("item")){
00212
00213 String textId = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "title");
00214 String iconId = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "icon");
00215 String resId = xpp.getAttributeValue("http://schemas.android.com/apk/res/android", "id");
00216
00217
00218 RibbonMenuItem item = new RibbonMenuItem();
00219 item.id = Integer.valueOf(resId.replace("@", ""));
00220 item.text = resourceIdToString(textId);
00221 item.icon = Integer.valueOf(iconId.replace("@", ""));
00222
00223 menuItems.add(item);
00224
00225 }
00226
00227
00228
00229 }
00230
00231 eventType = xpp.next();
00232
00233
00234 }
00235
00236
00237 } catch(Exception e){
00238 e.printStackTrace();
00239 }
00240
00241 }
00242
00243
00244
00245
00246
00247 private String resourceIdToString(String text){
00248
00249 if(!text.contains("@")){
00250 return text;
00251 } else {
00252
00253 String id = text.replace("@", "");
00254
00255 return getResources().getString(Integer.valueOf(id));
00256
00257 }
00258
00259 }
00260
00261
00262 public boolean isMenuVisible(){
00263 return rbmOutsideView.getVisibility() == View.VISIBLE;
00264 }
00265
00266
00267
00268
00269 @Override
00270 protected void onRestoreInstanceState(Parcelable state) {
00271 SavedState ss = (SavedState)state;
00272 super.onRestoreInstanceState(ss.getSuperState());
00273
00274 if (ss.bShowMenu)
00275 showMenu();
00276 else
00277 hideMenu();
00278 }
00279
00280
00281
00282 @Override
00283 protected Parcelable onSaveInstanceState() {
00284 Parcelable superState = super.onSaveInstanceState();
00285 SavedState ss = new SavedState(superState);
00286
00287 ss.bShowMenu = isMenuVisible();
00288
00289 return ss;
00290 }
00291
00292 static class SavedState extends BaseSavedState {
00293 boolean bShowMenu;
00294
00295 SavedState(Parcelable superState) {
00296 super(superState);
00297 }
00298
00299 private SavedState(Parcel in) {
00300 super(in);
00301 bShowMenu = (in.readInt() == 1);
00302 }
00303
00304 @Override
00305 public void writeToParcel(Parcel out, int flags) {
00306 super.writeToParcel(out, flags);
00307 out.writeInt(bShowMenu ? 1 : 0);
00308 }
00309
00310 public static final Parcelable.Creator<SavedState> CREATOR
00311 = new Parcelable.Creator<SavedState>() {
00312 public SavedState createFromParcel(Parcel in) {
00313 return new SavedState(in);
00314 }
00315
00316 public SavedState[] newArray(int size) {
00317 return new SavedState[size];
00318 }
00319 };
00320 }
00321
00322
00323
00324 public class RibbonMenuItem{
00325 int id;
00326 String text;
00327 int icon;
00328 }
00329
00330
00331
00332 private class Adapter extends BaseAdapter {
00333
00334 private LayoutInflater inflater;
00335
00336 public Adapter(){
00337 inflater = LayoutInflater.from(getContext());
00338 }
00339
00340
00341
00342 @Override
00343 public int getCount() {
00344 return menuItems.size();
00345 }
00346
00347 @Override
00348 public Object getItem(int position) {
00349 return null;
00350 }
00351
00352 @Override
00353 public long getItemId(int position) {
00354 return 0;
00355 }
00356
00357 @Override
00358 public View getView(int position, View convertView, ViewGroup parent) {
00359
00360 final ViewHolder holder;
00361
00362 if(convertView == null || convertView instanceof TextView){
00363 convertView = inflater.inflate(R.layout.rbm_item, null);
00364
00365 holder = new ViewHolder();
00366 holder.image = (ImageView) convertView.findViewById(R.id.rbm_item_icon);
00367 holder.text = (TextView) convertView.findViewById(R.id.rbm_item_text);
00368
00369 convertView.setTag(holder);
00370
00371 } else {
00372
00373 holder = (ViewHolder) convertView.getTag();
00374 }
00375
00376 holder.image.setImageResource(menuItems.get(position).icon);
00377 holder.text.setText(menuItems.get(position).text);
00378
00379
00380 return convertView;
00381 }
00382
00383
00384 class ViewHolder {
00385 TextView text;
00386 ImageView image;
00387
00388 }
00389
00390
00391
00392
00393 }
00394
00395
00396
00397 }