黑莓:创建自定义字段VerticalFieldManager用于复制的tableview [英] Blackberry: creating a custom field to use with VerticalFieldManager to replicate tableview
问题描述
我不知道如果我走的是正确的路由,但我已经决定做一个自定义字段和verticalfieldmanager中使用它。
I don't know if I'm taking the correct route, but I've decided on making a custom field and using it within a verticalfieldmanager.
在自定义字段我想在左边添加图像和文本在右边。
In the custom field I want to add an image on the left, and text on the right.
我刚刚开始,但遇到了一些问题。
I've just started, but have run into some problems.
package mypackage;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
public class CustomEventField extends Field implements DrawStyle {
private String title;
private String by;
private String date;
private String desc;
public CustomEventField(String title, String by, String date, String desc){
super();
this.title = title;
this.by = by;
this.date = date;
this.desc = desc;
}
protected void layout(int width, int height) {
setExtent(width,height);
}
protected void paint(Graphics graphics) {
graphics.drawText(this.title, 0, 0);
graphics.drawText(this.by, 50, 0);
}
}
我怎么有去的drawText到下一行从之前的drawText呢?我必须计算字体的高度,并添加到y位置?
How do I have the drawText go to the next line from the drawText before it? Do I have to calculate the height of the font and add that to the y position?
推荐答案
根据上述code也可能没有更多钞票,所以我做它可以显示图片,标题,日期一个自定义类。当你点击件商品的对话框将显示其索引号
这也是自定义列表视图
According to above code it may not posible so I made one custom class which can display image , title ,date. when you click on item one dialog will display with its Index number this is also useful for custom list view
请注意:如果你想试试这个下面的图像可在这里你只需要使用此请下载从样本图像这里
Note: if you want try this following images available here you just use this PLease download sample images from here
如果这里的任何更正,请让我知道
If here any corrections please let me know
class CustomListField extends HorizontalFieldManager{
private Bitmap scale_image;
private int width=0;
private int height=0;
private int background_color=0;
private BitmapField bitmap_field;
private boolean flag=false;
public CustomListField(String title, Bitmap image, String date,int image_width,int image_height,int background_color){
super(NO_HORIZONTAL_SCROLL|USE_ALL_WIDTH);
this.background_color=background_color;
width=image_width;
height=image_width;
if(image!=null){
scale_image=new Bitmap(image_width, image_height);
image.scaleInto(scale_image, Bitmap.FILTER_LANCZOS);
bitmap_field=new BitmapField(scale_image);
flag=false;
bitmap_field.setMargin(5, 5, 5, 5);
add(bitmap_field);
}
VerticalFieldManager vmanager=new VerticalFieldManager(USE_ALL_WIDTH|Field.FIELD_VCENTER){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth()-width, height);
setExtent(Display.getWidth()-width, height);
}
};
LabelField title_lbl=new LabelField("Title: "+title,Field.NON_FOCUSABLE|DrawStyle.ELLIPSIS);
vmanager.add(title_lbl);
LabelField date_lbl=new LabelField("Date: "+date,Field.NON_FOCUSABLE);
vmanager.add(date_lbl);
Font fon=title_lbl.getFont();
int title_height=fon.getHeight();
Font font=date_lbl.getFont();
int date_height=font.getHeight();
int pad=title_height+date_height;
title_lbl.setPadding((height-pad)/2, 0, 0, 0);
add(vmanager);
add(new NullField(FOCUSABLE));
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(), height);
setExtent(Display.getWidth(), height);
}
protected void paint(Graphics graphics) {
if(flag)
graphics.setBackgroundColor(background_color);
graphics.clear();
super.paint(graphics);
}
protected void onFocus(int direction) {
super.onFocus(direction);
flag=true;
invalidate();
}
protected void onUnfocus() {
invalidate();
flag=false;
}
protected boolean navigationClick(int status, int time) {
if(Touchscreen.isSupported()){
return false;
}else{
fieldChangeNotify(1);
return true;
}
}
protected boolean touchEvent(TouchEvent message)
{
if (TouchEvent.CLICK == message.getEvent())
{
FieldChangeListener listener = getChangeListener();
if (null != listener)
this.setFocus();
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
}
您可以调用这个类你必须通过位图,标题,IMAGE_HEIGHT和宽度,并按照以下方式背景clolor
you can call this class as following way you have to pass Bitmap,title,image_height and width, and background clolor
class sampleScreen extends MainScreen implements FieldChangeListener
{
private CustomListField cu_field[];
private Bitmap image=null;
int size=8;
public sampleScreen() {
VerticalFieldManager vmanager=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
}
};
cu_field=new CustomListField[size];
for(int i=0;i<size;i++){
image=Bitmap.getBitmapResource("sample_"+i+".jpg");
cu_field[i]=new CustomListField("BlackBerry models that had a built-in mobile phone, were the first models that natively ran Java, and transmitted data over the normal 2G cellular network. RIM began to advertise these devices as email-capable mobile phones rather than as 2-way pagers.",
image, "jan2011", 100, 100,Color.RED);
cu_field[i].setChangeListener(this);
vmanager.add(new SeparatorField());
vmanager.add(cu_field[i]);
}
add(vmanager);
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
for(int i=0;i<size;i++){
if(field==cu_field[i]){
final int k=i;
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("You click on Item No "+k);
}
});
}
}
}
}
在这里,你会得到输出,如下图
Here you will get output as following image
这篇关于黑莓:创建自定义字段VerticalFieldManager用于复制的tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!