Android中动态添加Panel的框架代码

项目经常会有这种需求,就是想动态的在某个界面上添加一个Panel。比如,有一个按钮,点击后会显示下拉菜单式的界面。这种需求,就属于动态添加一个Panel。需求多了,就要研究是否可以抽象出通用的框架代码,以方便开发,所以就有了以下内容。

这里说是框架,说的大了点,其实没有那么复杂,只是一个容易扩展的基类而已。不过至少算是框架类的代码。

  1. package arui;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Handler;  
  5. import android.os.Looper;  
  6. import android.os.Message;  
  7. import android.view.Gravity;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.view.ViewManager;  
  12. import android.widget.FrameLayout;  
  13.   
  14. /** 
  15.  * Base class for panel. 
  16.  *  
  17.  */  
  18. public abstract class BasePanel {  
  19.   
  20.     /** 
  21.      * left up position 
  22.      */  
  23.     public static final int LEFT_UP = 1;  
  24.   
  25.     /** 
  26.      * right up position 
  27.      */  
  28.     public static final int RIGHT_UP = 2;  
  29.   
  30.     /** 
  31.      * left bottom position 
  32.      */  
  33.     public static final int LEFT_BOTTOM = 3;  
  34.   
  35.     /** 
  36.      * right bottom position 
  37.      */  
  38.     public static final int RIGHT_BOTTOM = 4;  
  39.   
  40.     private static final int DEFAULT_MARGIN = 10;  
  41.   
  42.     private static final int SHOW_PANEL = 0;  
  43.   
  44.     private Activity activity;  
  45.   
  46.     private LayoutParams parameters;  
  47.   
  48.     private View view = null;  
  49.   
  50.     private int layoutId;  
  51.   
  52.     /** 
  53.      * constructor. 
  54.      *  
  55.      * @param activity 
  56.      *            this panel will be attached to the activity 
  57.      * @param layoutId 
  58.      *            the panel's layout id 
  59.      */  
  60.     public BasePanel(Activity activity, int layoutId) {  
  61.         this.activity = activity;  
  62.         this.layoutId = layoutId;  
  63.     }  
  64.   
  65.     /** 
  66.      * The developer can use this method to add the panel to the Activity. 
  67.      *  
  68.      * @param act 
  69.      *            Activity 
  70.      * @param params 
  71.      *            LayoutParams 
  72.      */  
  73.     public void attach(LayoutParams params) {  
  74.         parameters = params;  
  75.         mHandler.sendMessage(mHandler.obtainMessage(SHOW_PANEL));  
  76.   
  77.     }  
  78.   
  79.     /** 
  80.      * The developer can use this method to add the panel to the Activity. 
  81.      *  
  82.      * @param act 
  83.      *            Activity 
  84.      * @param position 
  85.      *            int. You can use BasePanel.LEFT_UP,BasePanel.RIGHT_UP, 
  86.      *            BasePanel.RIGHT_BOTTOM or BasePanel.LEFT_BOTTOM. 
  87.      */  
  88.     public void attach(int position) {  
  89.         attach(position, DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN,  
  90.                 DEFAULT_MARGIN);  
  91.     }  
  92.   
  93.     /** 
  94.      * The developer can use this method to add the panel to the Activity. 
  95.      *  
  96.      * @param act 
  97.      *            Activity 
  98.      * @param position 
  99.      *            int. You can use BasePanel.LEFT_UP,BasePanel.RIGHT_UP, 
  100.      *            BasePanel.RIGHT_BOTTOM or BasePanel.LEFT_BOTTOM. 
  101.      * @param leftMargin 
  102.      *            int, left margin. 
  103.      * @param topMargin 
  104.      *            int, top margin. 
  105.      * @param rightMargin 
  106.      *            int, right margin. 
  107.      * @param bottomMargin 
  108.      *            int, bottom margin. 
  109.      *  
  110.      */  
  111.     public void attach(int position, int leftMargin, int topMargin,  
  112.             int rightMargin, int bottomMargin) {  
  113.         FrameLayout.LayoutParams params = null;  
  114.         params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,  
  115.                 LayoutParams.WRAP_CONTENT);  
  116.         params.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);  
  117.         switch (position) {  
  118.         case LEFT_UP:  
  119.             params.gravity = Gravity.LEFT;  
  120.             break;  
  121.         case RIGHT_UP:  
  122.             params.gravity = Gravity.RIGHT;  
  123.             break;  
  124.         case LEFT_BOTTOM:  
  125.             params.gravity = Gravity.LEFT | Gravity.BOTTOM;  
  126.             break;  
  127.         case RIGHT_BOTTOM:  
  128.             params.gravity = Gravity.RIGHT | Gravity.BOTTOM;  
  129.             break;  
  130.         default:  
  131.             break;  
  132.         }  
  133.         attach(params);  
  134.     }  
  135.   
  136.     /** 
  137.      * The developer can use this method to remove the panel from the Activity. 
  138.      *  
  139.      */  
  140.     public void remove() {  
  141.         if (view != null) {  
  142.             ViewManager mViewManager = (ViewManager) view.getParent();  
  143.             if (mViewManager != null) {  
  144.                 mViewManager.removeView(view);  
  145.             }  
  146.         }  
  147.     }  
  148.   
  149.     private Handler mHandler = new Handler(Looper.getMainLooper()) {  
  150.   
  151.         @Override  
  152.         public void handleMessage(Message msg) {  
  153.             switch (msg.what) {  
  154.             case SHOW_PANEL:  
  155.                 if (view == null) {  
  156.                     LayoutInflater factory = LayoutInflater.from(activity);  
  157.                     view = factory.inflate(layoutId, null);  
  158.                 }  
  159.                 dealwithPanel(view);  
  160.                 remove();  
  161.                 activity.addContentView(view, parameters);  
  162.                 break;  
  163.             }  
  164.         }  
  165.   
  166.     };  
  167.   
  168.     /** 
  169.      * do something with this panel. 
  170.      *  
  171.      * @param view 
  172.      *            View of the panel 
  173.      */  
  174.     public abstract void dealwithPanel(View view);  
  175. }  



转自:http://blog.csdn.net/arui319/article/details/8057224

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值