BlackBerry定制自己喜爱的List Field

Come from : http://sinfrancis.javaeye.com/blog/508430

 

===============================================================================

 

BB上面提供可继承的ListField类,我们可以直接继承此类,然后重写里面的方法,另外继承ListFieldCallBack接口,实现回调。

 

请看代码: 

 MyList.java

 

 

Java代码
  1. package  com.mdev;  
  2.   
  3. import  net.rim.device.api.ui.ContextMenu;  
  4. import  net.rim.device.api.ui.Graphics;  
  5. import  net.rim.device.api.ui.component.ListField;  
  6. import  net.rim.device.api.ui.component.ListFieldCallback;  
  7.   
  8.   
  9. /**  
  10.  *   
  11.  * @author Sinfrancis wong  
  12.  * @site: http://mdev.cc  
  13.  *  
  14.  */   
  15. public   class  MyList  extends  ListField  implements  ListFieldCallback {  
  16.   
  17.     String[] datas = {"1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "11" , "12" , "13" , "14" , "15" , "16" , "17" , "18" , "19" , "20" };  
  18.       
  19.     /**  
  20.      * 数据总长度  
  21.      */   
  22.     int  dataSize;  
  23.     /**  
  24.      * 每一行的高度  
  25.      */   
  26.     int  rowHeight;  
  27.       
  28.       
  29.       
  30.     public  MyList() {  
  31.           
  32.         rowHeight = 40 ;  
  33.         dataSize = datas.length;  
  34.         /**  
  35.          * 设置回调函数  
  36.          */   
  37.         setCallback(this );  
  38.         /**  
  39.          * 设置一共有多少行  
  40.          */   
  41.         setSize(dataSize);  
  42.         /**  
  43.          * 设置每一行高度  
  44.          */   
  45.         setRowHeight(rowHeight);  
  46.     }  
  47.     protected   void  paint(Graphics graphics) {  
  48.         /**  
  49.          * 画出背景  
  50.          */   
  51.         graphics.setColor(0x336699 );  
  52.         graphics.fillRect(00 , getPreferredWidth(), getPreferredHeight());  
  53.           
  54.         graphics.setColor(0x000000 );  
  55.         int  i =  0 ;  
  56.         while ( i < dataSize){  
  57.             drawListRow(this , graphics, i, i*getRowHeight(), getPreferredWidth());  
  58.             i++;  
  59.         }  
  60.     }  
  61.       
  62.     protected   void  layout( int  width,  int  height) {  
  63.           
  64.         /**  
  65.          * 设置整个ListField的总高度和宽度  
  66.          */   
  67.         setExtent(getPreferredWidth(), getPreferredHeight());  
  68.     }  
  69.     public   int  getPreferredHeight() {  
  70.         return  dataSize * rowHeight ;  
  71.     }  
  72.   
  73.     public   int  getPreferredWidth() {  
  74.         return   360 ;  
  75.     }  
  76.   
  77.     public   void  drawListRow(ListField listField, Graphics graphics,  int  index,  
  78.             int  y,  int  width) {  
  79.         /**  
  80.          * 画出文字  
  81.          */   
  82.         graphics.drawText(datas[index], 10 , y);  
  83.           
  84.         /**  
  85.          * 画出下划线  
  86.          */   
  87.         graphics.drawLine(0 , y+rowHeight,  360 , y+rowHeight);  
  88.     }  
  89.   
  90.     public  Object get(ListField listField,  int  index) {  
  91.         return  datas[index];  
  92.     }  
  93.     public   int  getPreferredWidth(ListField listField) {  
  94.         return  getPreferredWidth();  
  95.     }  
  96.   
  97.     public   int  indexOfList(ListField listField, String prefix,  int  start) {  
  98.         return   0 ;  
  99.     }  
  100.       
  101.     protected   void  makeContextMenu(ContextMenu contextMenu) {  
  102.     }  
  103.       
  104. }  
package com.mdev;

import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;


/**
 * 
 * @author Sinfrancis wong
 * @site: http://mdev.cc
 *
 */
public class MyList extends ListField implements ListFieldCallback {

	String[] datas = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"};
	
	/**
	 * 数据总长度
	 */
	int dataSize;
	/**
	 * 每一行的高度
	 */
	int rowHeight;
	
	
	
	public MyList() {
		
		rowHeight = 40;
		dataSize = datas.length;
		/**
		 * 设置回调函数
		 */
		setCallback(this);
		/**
		 * 设置一共有多少行
		 */
		setSize(dataSize);
		/**
		 * 设置每一行高度
		 */
		setRowHeight(rowHeight);
	}
	protected void paint(Graphics graphics) {
		/**
		 * 画出背景
		 */
		graphics.setColor(0x336699);
		graphics.fillRect(0, 0, getPreferredWidth(), getPreferredHeight());
		
		graphics.setColor(0x000000);
		int i = 0;
		while( i < dataSize){
			drawListRow(this, graphics, i, i*getRowHeight(), getPreferredWidth());
			i++;
		}
	}
	
	protected void layout(int width, int height) {
		
		/**
		 * 设置整个ListField的总高度和宽度
		 */
		setExtent(getPreferredWidth(), getPreferredHeight());
	}
	public int getPreferredHeight() {
		return dataSize * rowHeight ;
	}

	public int getPreferredWidth() {
		return 360;
	}

	public void drawListRow(ListField listField, Graphics graphics, int index,
			int y, int width) {
		/**
		 * 画出文字
		 */
		graphics.drawText(datas[index], 10, y);
		
		/**
		 * 画出下划线
		 */
		graphics.drawLine(0, y+rowHeight, 360, y+rowHeight);
	}

	public Object get(ListField listField, int index) {
		return datas[index];
	}
	public int getPreferredWidth(ListField listField) {
		return getPreferredWidth();
	}

	public int indexOfList(ListField listField, String prefix, int start) {
		return 0;
	}
	
	protected void makeContextMenu(ContextMenu contextMenu) {
	}
	
}

 

 

MyScreen.java

 

 

Java代码
  1. package  com.mdev;  
  2.   
  3. import  java.util.Vector;  
  4. import  net.rim.device.api.ui.ContextMenu;  
  5. import  net.rim.device.api.ui.Manager;  
  6. import  net.rim.device.api.ui.component.Menu;  
  7. import  net.rim.device.api.ui.container.MainScreen;  
  8. import  net.rim.device.api.ui.container.VerticalFieldManager;  
  9.   
  10.   
  11. /**  
  12.  *   
  13.  * @author Sinfrancis wong  
  14.  * @site: http://mdev.cc  
  15.  *  
  16.  */   
  17. public   class  MyScreen  extends  MainScreen {  
  18.     public  MyScreen() {  
  19.         MyList m = new  MyList();  
  20.       
  21.         /**  
  22.          * 设置VerticalFieldManager可以滚动  
  23.          */   
  24.         VerticalFieldManager fieldManager = new  VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.VERTICAL_SCROLLBAR_MASK){  
  25.               
  26.             protected   void  sublayout( int  maxWidth,  int  maxHeight) {  
  27.                 super .sublayout(maxWidth, maxHeight);  
  28.                   
  29.                 /**  
  30.                  * 设置Manager的大小为300*200,list里面的数据只在这个范围内滚动,如果注释掉就会显示全屏。  
  31.                  */   
  32.                 setExtent(300200 );  
  33.             }  
  34.             protected   void  makeContextMenu(ContextMenu contextMenu) {  
  35.             }  
  36.               
  37.             protected   void  makeMenu(Menu menu,  int  instance) {  
  38.             }  
  39.         };  
  40.           
  41.           
  42.           
  43.         /**  
  44.          * 设置外上边距和外左边距  
  45.          */   
  46.         //fieldManager.setPadding(50, 0, 0, 10);   
  47.           
  48.         /**  
  49.          * 设置内上边距和内左边距  
  50.          */   
  51.         fieldManager.setMargin(500010 );  
  52.           
  53.           
  54.           
  55.         //**注意:如果设置外上边距,请将list field的总高度加上您设置的外上边距高度,   
  56.         //**比如manager外上边距为 50,那么list field的总高度也要加上50,不然无法滚动到底。   
  57.         //**设置内上边距,list field无需在加上内上边距的高度   
  58.         //**以上是在非全屏模式下的listfield   
  59.           
  60.         fieldManager.add(m);  
  61.         add(fieldManager);  
  62.     }  
  63.       
  64.     protected   void  makeContextMenu(ContextMenu contextMenu) {  
  65.     }  
  66.       
  67.     protected   void  makeMenu(Menu menu,  int  instance) {  
  68.     }  
  69. }  
package com.mdev;

import java.util.Vector;
import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;


/**
 * 
 * @author Sinfrancis wong
 * @site: http://mdev.cc
 *
 */
public class MyScreen extends MainScreen {
	public MyScreen() {
		MyList m = new MyList();
	
		/**
		 * 设置VerticalFieldManager可以滚动
		 */
		VerticalFieldManager fieldManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.VERTICAL_SCROLLBAR_MASK){
			
			protected void sublayout(int maxWidth, int maxHeight) {
				super.sublayout(maxWidth, maxHeight);
				
				/**
				 * 设置Manager的大小为300*200,list里面的数据只在这个范围内滚动,如果注释掉就会显示全屏。
				 */
				setExtent(300, 200);
			}
			protected void makeContextMenu(ContextMenu contextMenu) {
			}
			
			protected void makeMenu(Menu menu, int instance) {
			}
		};
		
		
		
		/**
		 * 设置外上边距和外左边距
		 */
		//fieldManager.setPadding(50, 0, 0, 10);
		
		/**
		 * 设置内上边距和内左边距
		 */
		fieldManager.setMargin(50, 0, 0, 10);
		
		
		
		//**注意:如果设置外上边距,请将list field的总高度加上您设置的外上边距高度,
		//**比如manager外上边距为 50,那么list field的总高度也要加上50,不然无法滚动到底。
		//**设置内上边距,list field无需在加上内上边距的高度
		//**以上是在非全屏模式下的listfield
		
		fieldManager.add(m);
		add(fieldManager);
	}
	
	protected void makeContextMenu(ContextMenu contextMenu) {
	}
	
	protected void makeMenu(Menu menu, int instance) {
	}
}

 

 

MyApplication.java

 

 

Java代码
  1. package  com.mdev;  
  2.   
  3. import  net.rim.device.api.ui.UiApplication;  
  4.   
  5. /**  
  6.  *   
  7.  * @author Sinfrancis wong  
  8.  * @site: http://mdev.cc  
  9.  *  
  10.  */   
  11. public   class  MyApplication  extends  UiApplication {  
  12.       
  13.     public  MyApplication() {  
  14.         MyScreen m = new  MyScreen();  
  15.         pushScreen(m);  
  16.     }  
  17.       
  18.     public   static   void  main(String[] args) {  
  19.      MyApplication myApplication = new  MyApplication();  
  20.      myApplication.enterEventDispatcher();  
  21.        
  22.     }  
  23. }  
package com.mdev;

import net.rim.device.api.ui.UiApplication;

/**
 * 
 * @author Sinfrancis wong
 * @site: http://mdev.cc
 *
 */
public class MyApplication extends UiApplication {
	
	public MyApplication() {
		MyScreen m = new MyScreen();
		pushScreen(m);
	}
	
	public static void main(String[] args) {
	 MyApplication myApplication = new MyApplication();
	 myApplication.enterEventDispatcher();
	 
	}
}

 

 

 

所有应该注意的地方都写在注释里面了,请看注释,截图:

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值