2013-10-08 108 views
1

我正在使用ListFields。我需要自定義ListField。我看到這樣的畫面( http://supportforums.blackberry.com/t5/Java-Development/custom-list-field/td-p/2618419):如何顯示和隱藏ListField中的圖像點擊黑莓

enter image description here

我得到安裝的應用程序列表與CodeModuleManager,現在我想表明的是,在ListField,你可以在上面的圖片中看到。你可以看到黑色的鎖定圖片,我需要點擊自定義該圖片。

我的意思是當我點擊任何行我想從列表中提交的單擊行中刪除黑色的鎖定圖片,當我再次單擊相同的行時,我想將該鎖定圖片插入該行。

我想我必須在點擊或其他東西時調用drawListRow()方法嗎?當我點擊列表時,黑色中的鎖定圖像應該消失。當我再次點擊列表時,它應該再次出現。

這裏是屏幕的代碼,我使用:

public final class MyScreen extends MainScreen { 

    CustomListField myListView; 

    public MyScreen() { 

     Bitmap listThumb = Bitmap.getBitmapResource("icon.png"); 
     String listTitle="Headline"; 
     String listDesc = "Mobile news feeds"; 
     String listDesc2 = " "; 
     final Bitmap navBar = Bitmap.getBitmapResource("lock.png"); 
     Vector v = new Vector(); 
     for(int i=0; i<30; i++){ 
      v.addElement(new ListRander(listThumb, listTitle, listDesc,listDesc2, navBar)); 
     } 
     //v.addElement(new ListRander(listThumb, listTitle, listDesc,listDesc2, navBar)); 

     myListView = new CustomListField(v){ 


      protected boolean trackwheelClick (int status, int time) { 

       Dialog.alert(" You have selected :" + getSelectedIndex()); 

       return super.trackwheelClick(status, time); 
      } 
     } 
     ; 
     add(myListView); 


    } 
} 

行數據類:

package mypackage; 

import net.rim.device.api.system.Bitmap; 

public class ListRander { 

    private Bitmap listThumb= null; 
    private Bitmap navBar = null; 
    private String listTitle=null; 
    private String listDesc= null; 
    private String listDesc2= null; 

    public ListRander(Bitmap listThumb,String listTitle, String listDesc, String listDesc2, Bitmap navBar) { 
     this.listDesc = listDesc; 
     this.listDesc2 = listDesc2; 
     this.listThumb = listThumb; 
     this.listTitle = listTitle; 
     this.navBar = navBar; 
    } 
    public Bitmap getListThumb() { 
     return listThumb; 
    } 
    public void setListThumb(Bitmap listThumb) { 
     this.listThumb = listThumb; 
    } 
    public Bitmap getNavBar() { 
     return navBar; 
    } 
    public void setNavBar(Bitmap navBar) { 
     this.navBar = navBar; 
    } 
    public String getListTitle() { 
     return listTitle; 
    } 
    public void setListTitle(String listTitle) { 
     this.listTitle = listTitle; 
    } 
    public String getListDesc() { 
     return listDesc; 
    } 
    public void setListDesc(String listDesc) { 
     this.listDesc = listDesc; 
    } 
    public String getListDesc2() { 
     return listDesc2; 
    } 
    public void setListDesc2(String listDesc2) { 
     this.listDesc2 = listDesc2; 
    } 
} 

自定義列表字段:

package mypackage; 

import java.util.Enumeration; 
import java.util.Vector; 

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Color; 
import net.rim.device.api.ui.Font; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.XYRect; 
import net.rim.device.api.ui.component.ListField; 
import net.rim.device.api.ui.component.ListFieldCallback; 

public class CustomListField extends ListField implements ListFieldCallback { 

    private Vector _listData; 
    private int _MAX_ROW_HEIGHT = 60; 
    public CustomListField (Vector data) { 

     _listData = data; 
     setSize(_listData.size()); 
     setSearchable(true); 
     setCallback(this); 
     setRowHeight(_MAX_ROW_HEIGHT); 

    } 

    protected void drawFocus (Graphics graphics, boolean on) { 

     XYRect rect = new XYRect(); 
     graphics.setGlobalAlpha(150); 
     graphics.setColor(Color.BLUE); 
     getFocusRect(rect); 
     drawHighlightRegion(graphics,HIGHLIGHT_FOCUS,true,rect.x,rect.y,rect.width,rect.height); 

    } 

    public int moveFocus (int amount, int status, int time) { 

     this.invalidate(this.getSelectedIndex()); 
     return super.moveFocus(amount, status, time); 

    } 

    public void onFocus (int direction) { 

     super.onFocus(direction); 

    } 

    protected void onUnFocus() { 

     this.invalidate(this.getSelectedIndex()); 

    } 

    public void refresh() { 

     this.getManager().invalidate(); 

    } 

    public void drawListRow (ListField listField, Graphics graphics, int index, int y, int w) { 

     ListRander listRander = (ListRander)_listData.elementAt(index); 
     graphics.setGlobalAlpha(255); 
     graphics.setFont(Font.getDefault().getFontFamily().getFont(Font.PLAIN, 24)); 
     final int margin =5; 
     final Bitmap thumb= listRander.getListThumb(); 
     final String listHeading = listRander.getListTitle(); 
     final String listDesc= listRander.getListDesc(); 
     final String listDesc2= listRander.getListDesc2(); 
     final Bitmap nevBar = listRander.getNavBar(); 
     //list border 
     graphics.setColor(Color.BLACK); 
     graphics.drawRect(0, y, w, _MAX_ROW_HEIGHT); 

     //thumbnail border & thumbnail image 
     graphics.setColor(Color.BLACK); 
     graphics.drawRoundRect(margin-2, y+margin-2,thumb.getWidth()+2, thumb.getHeight()+2, 5, 5); 
     graphics.drawBitmap(margin, y+margin, thumb.getWidth(), thumb.getHeight(), thumb, 0, 0); 

     //drawing texts 
     // graphics.setFont(FontGroup.fontBold); 
     graphics.drawText(listHeading, 2*margin+thumb.getWidth(), y+margin); 
     graphics.setColor(Color.BLACK); 

     // graphics.setFont(FontGroup.smallFont); 
     graphics.drawText(listDesc, 2*margin+thumb.getWidth(), y+ margin+20); 
     graphics.drawText(listDesc2, 2*margin+thumb.getWidth(), y+ margin+32); 

     //draw navigation button 
     final int navBarPosY = y+(_MAX_ROW_HEIGHT/2 - nevBar.getHeight()/2); 
     final int navBarPosX = Graphics.getScreenWidth()- nevBar.getWidth()+ margin; 
     graphics.drawBitmap(navBarPosX-10, navBarPosY, nevBar.getWidth(), nevBar.getHeight(), nevBar, 0 , 0); 

    } 

    public Object get(ListField listField, int index) { 

     String rowString = (String) _listData.elementAt(index); 
     return rowString; 

    } 

    public int indexOfList (ListField listField, String prefix, int start) { 

     for (Enumeration e = _listData.elements(); e.hasMoreElements();) { 

      String rowString = (String) e.nextElement(); 
      if (rowString.startsWith(prefix)) { 

       return _listData.indexOf(rowString); 

      } 

     } 

     return 0; 

    } 

    public int getPreferredWidth(ListField listField) { 

     return 3 * listField.getRowHeight(); 

    } 

} 
+0

當我點擊列表中,鎖定圖像在黑色應該消失。當我再次點擊列表時,它應該再次出現。 – Achin

回答

0

這是一個有點難以遵循你的代碼,因爲你的變量沒有正確命名(例如。 lock.png使用navBar)。請儘量在將來清理一下,這樣你就可以得到更好的答案。

從你的代碼中,我可以看到,鎖定圖像繪製在drawListRow()使用此代碼:

final int navBarPosY = y+(_MAX_ROW_HEIGHT/2 - nevBar.getHeight()/2); 
    final int navBarPosX = Graphics.getScreenWidth()- nevBar.getWidth()+ margin; 
    graphics.drawBitmap(navBarPosX-10, navBarPosY, nevBar.getWidth(), nevBar.getHeight(), nevBar, 0 , 0); 

所以,你只需要跟蹤一個布爾值的每一行是drawListRow()可以檢查來決定如果鎖應該繪製或不。添加新鎖定財產ListRander,它代表一行:

public class ListRander { 
    private boolean locked = true; // defaults to "locked" 
    public boolean isLocked() { 
     return locked; 
    } 
    public void setLocked(boolean value) { 
     locked = value; 
    } 

那麼,你會改變你的點擊處理程序代碼到這一點:

myListView = new CustomListField(v){ 

    protected boolean navigationClick(int status, int time) { 
     int index = getSelectedIndex(); 
     ListRander lr = (ListRander) get(this, index); 
     lr.setLocked(!lr.isLocked()); 
     // force a repaint of this row 
     invalidate(index); 
     return true; // event consumed 
    } 

    //protected boolean trackwheelClick (int status, int time) { 
    // keep this commented out, unless trackwheel devices need special logic 
    //} 
    }; 

這將需要改變你的CustomListField#get(ListField, int)方法,該方法通常會返回一行的完整數據對象:

public Object get(ListField listField, int index) { 
    return _listData.elementAt(index); 
    } 

最後,更改drawListRow()使用這個新的布爾值:

 if (listRander.isLocked()) { 
     // draw lock button 
     final int navBarPosY = y+(_MAX_ROW_HEIGHT/2 - nevBar.getHeight()/2); 
     final int navBarPosX = Display.getWidth() - nevBar.getWidth()+ margin;   
     graphics.drawBitmap(navBarPosX-10, navBarPosY, nevBar.getWidth(), nevBar.getHeight(), nevBar, 0 , 0); 
    } 
+0

嗨,我已經改變了我的代碼,正如你上面所建議的,但它不工作。當我點擊行消失的鎖圖標什麼也沒有發生。我的要求是當我點擊列表中的特定行時,黑色中的鎖定圖像應該消失。當我再次點擊列表中的同一行時,它應該再次出現。 – Achin

+0

@ user12345,對不起,我的代碼在'navigationClick()'方法中出現了剪切和粘貼錯誤。我已經修復它。請複製新的'navigationClick()'代碼並重試。我在我的應用程序中測試了這段代碼,它工作正常。但是,我不得不將小片段粘貼到我的答案中,並且我錯過了一塊:( – Nate

+0

謝謝,它工作良好 – Achin