开发者社区> 问答> 正文

大牛帮忙分析github slidingmenu源码?报错

  1.    /**
  2.          * Attaches the SlidingMenu to an entire Activity
  3.          * 
  4.          * @param activity the Activity
  5.          * @param slideStyle either SLIDING_CONTENT or SLIDING_WINDOW
  6.          * @param actionbarOverlay whether or not the ActionBar is overlaid
  7.          */
  8.         public void attachToActivity(Activity activity, int slideStyle, boolean actionbarOverlay) {
  9.                 if (slideStyle != SLIDING_WINDOW && slideStyle != SLIDING_CONTENT)
  10.                         throw new IllegalArgumentException("slideStyle must be either SLIDING_WINDOW or SLIDING_CONTENT");

  11.                 if (getParent() != null)
  12.                         throw new IllegalStateException("This SlidingMenu appears to already be attached");

  13.                 // get the window background
  14.                 TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowBackground});
  15.                 int background = a.getResourceId(0, 0);
  16.                 a.recycle();

  17.                 switch (slideStyle) {
  18.                 case SLIDING_WINDOW:
  19.                         mActionbarOverlay = false;
  20.                         ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
  21.                         ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
  22.                         
  23.                         // save ActionBar themes that have transparent assets
  24.                         decorChild.setBackgroundResource(background);
  25.                         decor.removeView(decorChild);
  26.                         decor.addView(this);
  27.                         setContent(decorChild);
  28.                         break;
  29.                 case SLIDING_CONTENT:
  30.                         mActionbarOverlay = actionbarOverlay;
  31.                         // take the above view out of
  32.                         ViewGroup contentParent = (ViewGroup)activity.findViewById(android.R.id.content);
  33.                         View content = contentParent.getChildAt(0);
  34.                         contentParent.removeView(content);
  35.                         contentParent.addView(this);
  36.                         setContent(content);
  37.                         // save people from having transparent backgrounds
  38.                         if (content.getBackground() == null)
  39.                                 content.setBackgroundResource(background);
  40.                         break;
  41.                 }
  42.         }
复制代码
上面代码来自github 上的slidingmenu, 地址:https://github.com/jfeinstein10/SlidingMenu
上面方法中的两个case: 如果执行第一个case, 那么滑动actionBar, 滑动菜单会被滑动. 如果执行第二个case, 滑动actionBar, 滑动菜单没有反应.. 求分析???? 


问题一:       TypedArray a = activity.getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowBackground});
                   int background = a.getResourceId(0, 0);  // 两个0 分别表示什么???
                   a.recycle();   // 为什么要调用recycle方法???


问题二:   ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
                                ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);                                  与                                     ViewGroup contentParent = (ViewGroup)activity.findViewById(android.R.id.content);
                                 View content = contentParent.getChildAt(0); 两者有什么区别???     getChildAt(0) 是滑动菜单? 还是菜单?

                问题三:   decor.removeView(decorChild);                                                             decor.addView(this);
                              setContent(decorChild);

                              contentParent.removeView(content);
                              contentParent.addView(this);
                              setContent(content);
                            为什么两个case里面都是先 移除子布局?  然后添加this布局??  其中this布局代表的是哪个布局???  我试过,如果注释removeView                             方法, 程序会报错...   同时主调removeView方法和addView方法程序也会报错...    如果仅仅只是主调addView方法, 那么程序跑起来就是一片空白....

展开
收起
爱吃鱼的程序员 2020-06-20 16:35:23 559 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    感觉楼主想的好深入,用的时候没有想这么多。

    问题三中this 当然是指SlidingMenu.this

    我也是,最近比较闲,所以就想研究研究..
    2020-06-20 16:35:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
APICloud服务CAF开发者 立即下载
《阿里巴巴Android开发手册》 立即下载
阿里开发者手册-Golang专题 立即下载