📄 scrollpaneitem.java
字号:
package com.gt.mrs.ui.uiitem;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
public class ScrollPaneItem extends BaseUiItem {
private Vector items=new Vector();
private int itemPoint;
private int totalLength;
private boolean verticalLayout=true;//默认是垂直,可设水平.在draw中还未实现水平.
private boolean visibleBar;
private int preFocusOnIndex;
private int preSelectedIndex;
private int viewStartIndex;
private int viewEndIndex;
private boolean isUp=true;
private static final int STATE_NORMAL=0;
private static final int STATE_POINT_UP=1;
private static final int STATE_POINT_DOWN=2;
private static final int STATE_LIST_UP=3;
private static final int STATE_LIST_DOWN=4;
private boolean canRoll=false;
private int state=STATE_NORMAL;
private int moveOffset=0;
private int oldFocusOnIndex=0;
private int rollStepLength=10;
public static final int ZERO=0;
private int offsetForDown=ZERO;
public ScrollPaneItem(int width,int height){
super(width,height);
}
public void resetIndexPoint(){
itemPoint=ZERO;
preFocusOnIndex=ZERO;
preSelectedIndex=ZERO;
viewStartIndex=ZERO;
viewEndIndex=ZERO;
isUp=true;
state=STATE_NORMAL;
moveOffset=0;
oldFocusOnIndex=0;
offsetForDown=ZERO;
}
public void addUiItem(BaseUiItem item){
items.addElement(item);
adjust();
}
public void removeUiItem(int itemIndex){
items.removeElementAt(itemIndex);
adjust();
}
public void removeAllItems(){
items.removeAllElements();
adjust();
}
private void adjust(){
BaseUiItem tmpItem=null;
totalLength=ZERO;
if(verticalLayout){
for(int i=ZERO;i<items.size();i++){
tmpItem=(BaseUiItem)items.elementAt(i);
totalLength+=tmpItem.getHeight();
}
}
else{
for(int i=ZERO;i<items.size();i++){
tmpItem=(BaseUiItem)items.elementAt(i);
totalLength+=tmpItem.getWidth();
}
}
if(itemPoint>=items.size()){
itemPoint=items.size()-1;
}
if(itemPoint<ZERO){
itemPoint=ZERO;
}
focusOnCurrentItem();
adjustView();
}
private void adjustView(){
int tmp_state=STATE_NORMAL;
offsetForDown=ZERO;
if(items.size()<=ZERO){
return;
}
if(isUp){
if(itemPoint>=ZERO && itemPoint<=viewStartIndex){
if(itemPoint<viewStartIndex){
tmp_state=STATE_LIST_DOWN;
}
else{
tmp_state=STATE_POINT_UP;
}
viewStartIndex=itemPoint;
BaseUiItem tmpItem=null;
int usedLength=ZERO;
for(int i=viewStartIndex;i<items.size();i++){
tmpItem=(BaseUiItem)items.elementAt(i);
tmpItem.g_id=i;
usedLength+=tmpItem.getHeight();
viewEndIndex=i;
if(usedLength>=getHeight()){
viewEndIndex=i;
break;
}
}
}
else{
tmp_state=STATE_POINT_UP;
if(getUiItem(viewStartIndex).getHeight()>=getHeight()){
isUp=false;
viewEndIndex=itemPoint;
}
}
}
else{
if(itemPoint<items.size() && itemPoint>=viewEndIndex){
if(itemPoint>viewEndIndex){
tmp_state=STATE_LIST_UP;
}
else{
tmp_state=STATE_POINT_DOWN;
}
viewEndIndex=itemPoint;
BaseUiItem tmpItem=null;
int usedLength=ZERO;
for(int i=viewEndIndex;i>=0;i--){
tmpItem=(BaseUiItem)items.elementAt(i);
tmpItem.g_id=i;
usedLength+=tmpItem.getHeight();
viewStartIndex=i;
if(usedLength>=getHeight()){
viewStartIndex=i;
break;
}
}
if(usedLength<getHeight()){
offsetForDown=getHeight()-usedLength;
}
}
else{
BaseUiItem tmpItem=null;
int usedLength=ZERO;
for(int i=viewEndIndex;i>=0;i--){
tmpItem=(BaseUiItem)items.elementAt(i);
tmpItem.g_id=i;
usedLength+=tmpItem.getHeight();
if(usedLength>=(getHeight())){
break;
}
}
if(usedLength<getHeight()){
offsetForDown=getHeight()-usedLength;
}
tmp_state=STATE_POINT_DOWN;
if(getUiItem(viewEndIndex).getHeight()>=getHeight()){
isUp=true;
viewStartIndex=itemPoint;
}
}
}
this.state=tmp_state;
if(this.state!=STATE_NORMAL){
this.moveOffset=getCurrentUiItem().getHeight();
}
}
public boolean moveUp(){
isHandleControl=false;
BaseUiItem baseItem=getCurrentUiItem();
int lheight=baseItem.getHeight();
if(lheight>=getHeight()){
isHandleControl=baseItem.moveUp();
}
if(!isHandleControl){
isHandleControl=true;
if(--itemPoint<ZERO){
itemPoint=ZERO;
isHandleControl=false;
}
if(preFocusOnIndex!=getCurrentIndex()){
focusOnCurrentItem();
isUp=true;
adjustView();
}
}
return isHandleControl;
}
public boolean moveDown(){
isHandleControl=false;
BaseUiItem baseItem=getCurrentUiItem();
int lheight=baseItem.getHeight();
if(lheight>=getHeight()){
isHandleControl=baseItem.moveDown();
}
if(!isHandleControl){
isHandleControl=true;
if(++itemPoint>=items.size()){
itemPoint=items.size()-1;
itemPoint=itemPoint<ZERO?ZERO:itemPoint;
isHandleControl=false;
}
if(preFocusOnIndex!=getCurrentIndex()){
focusOnCurrentItem();
isUp=false;
adjustView();
}
}
return isHandleControl;
}
public boolean moveLeft(){
isHandleControl=false;
BaseUiItem baseItem=getCurrentUiItem();
int lwidth=baseItem.getWidth();
if(lwidth>=getWidth()){
isHandleControl=baseItem.moveLeft();
}
return isHandleControl;
}
public boolean moveRight(){
isHandleControl=false;
BaseUiItem baseItem=getCurrentUiItem();
int lwidth=baseItem.getWidth();
if(lwidth>=getWidth()){
isHandleControl=baseItem.moveRight();
}
return isHandleControl;
}
/*public boolean moveUpPoint(){
BaseUiItem baseItem=getCurrentUiItem();
int lheight=baseItem.getHeight();
boolean canMove=true;
if(lheight>=getHeight()){
if(baseItem instanceof TextWarpItem){
canMove=((TextWarpItem)baseItem).moveUpPoint();
}
else if(baseItem instanceof CmImageScrollItem){
canMove=((CmImageScrollItem)baseItem).moveUp();
}
else{
canMove=true;
}
}
if(canMove){
if(--itemPoint<ZERO){
itemPoint=ZERO;
}
if(preFocusOnIndex!=getCurrentIndex()){
focusOnCurrentItem();
isUp=true;
adjustView();
}
}
return canMove;
}*/
/*public boolean moveDownPoint(){
BaseUiItem baseItem=getCurrentUiItem();
int lheight=baseItem.getHeight();
boolean canMove=true;
if(lheight>=getHeight()){
if(baseItem instanceof TextWarpItem){
canMove=((TextWarpItem)baseItem).moveDownPoint();
}
else if(baseItem instanceof CmImageScrollItem){
canMove=((CmImageScrollItem)baseItem).moveDown();
}
else{
canMove=true;
}
}
if(canMove){
if(++itemPoint>=items.size()){
itemPoint=items.size()-1;
itemPoint=itemPoint<ZERO?ZERO:itemPoint;
}
if(preFocusOnIndex!=getCurrentIndex()){
focusOnCurrentItem();
isUp=false;
adjustView();
}
}
return canMove;
}*/
/*public boolean moveLeftPoint(){
BaseUiItem baseItem=getCurrentUiItem();
int lwidth=baseItem.getWidth();
boolean canMove=true;
if(lwidth>=getWidth()){
if(baseItem instanceof CmImageScrollItem){
canMove=((CmImageScrollItem)baseItem).moveLeft();
}
else{
canMove=true;
}
}
return canMove;
}*/
/*public boolean moveRightPoint(){
BaseUiItem baseItem=getCurrentUiItem();
int lwidth=baseItem.getWidth();
boolean canMove=true;
if(lwidth>=getWidth()){
if(baseItem instanceof CmImageScrollItem){
canMove=((CmImageScrollItem)baseItem).moveRight();
}
else{
canMove=true;
}
}
return canMove;
}*/
public void setPoint(int point){
if(point>=items.size()){
point=items.size()-1;
}
if(point<ZERO){
point=ZERO;
}
itemPoint=point;
if(preFocusOnIndex!=getCurrentIndex()){
focusOnCurrentItem();
isUp=true;
adjust();
adjustView();
}
moveOffset=ZERO;
}
public BaseUiItem getUiItem(int index){
if(index>=items.size() || index<0){
return null;
}
return (BaseUiItem)items.elementAt(index);
}
public BaseUiItem getCurrentUiItem(){
return getUiItem(itemPoint);
}
public int getCurrentIndex(){
return itemPoint;
}
public int size(){
return items.size();
}
public void focusOnCurrentItem(){
BaseUiItem tmpItem=getUiItem(preFocusOnIndex);
if(tmpItem!=null){
tmpItem.setFocusOn(false);
}
preFocusOnIndex=getCurrentIndex();
tmpItem=getUiItem(preFocusOnIndex);
if(tmpItem!=null){
tmpItem.setFocusOn(true);
}
}
public void setCanRoll(boolean canRoll){
this.canRoll=canRoll;
}
public boolean canRoll(){
return canRoll;
}
public void selectCurrentItem(){
selectItem(getCurrentIndex());
}
public void selectItem(int index){
BaseUiItem tmpItem=getUiItem(preSelectedIndex);
if(tmpItem!=null){
tmpItem.setSelected(false);
}
preSelectedIndex=index;
tmpItem=getUiItem(preSelectedIndex);
if(tmpItem!=null){
tmpItem.setSelected(true);
}
}
public int getSelectedIndex(){
return preSelectedIndex;
}
public void deselectAll(){
for(int i=ZERO;i<items.size();i++){
deselectItem(i);
}
}
public void deselectPreSelectedItem(){
deselectItem(preSelectedIndex);
}
public void deselectItem(int index){
BaseUiItem tmpItem=getUiItem(index);
if(tmpItem!=null){
tmpItem.setSelected(false);
}
}
public void draw(Graphics g,int clipX,int clipY,int clipWidth,int clipHeight){
int usedLength=ZERO;
BaseUiItem tmpItem=null;
g.setClip(x, y, width, height);
if(items.size()<=0){
return;
}
if(isUp){
int viewOffsetY=0;
if(canRoll){
if(state!=STATE_NORMAL){
if((moveOffset-=rollStepLength)<0)
state=STATE_NORMAL;
}
switch(state){
case STATE_POINT_UP:
break;
case STATE_LIST_DOWN:
viewOffsetY=moveOffset;
break;
}
}
for(int i=viewStartIndex;i<items.size();i++){
tmpItem=(BaseUiItem)items.elementAt(i);
tmpItem.setPosition(x, y+usedLength-viewOffsetY);
tmpItem.draw(g);
usedLength+=tmpItem.getHeight();
if(usedLength>=(getHeight()+viewOffsetY)){
break;
}
}
}
else{
int viewOffsetY=0;
if(canRoll){
if(state!=STATE_NORMAL){
if((moveOffset-=rollStepLength)<0)
state=STATE_NORMAL;
}
switch(state){
case STATE_POINT_DOWN:
break;
case STATE_LIST_UP:
viewOffsetY=moveOffset;
break;
}
}
for(int i=viewEndIndex;i>=0;i--){
tmpItem=(BaseUiItem)items.elementAt(i);
usedLength+=tmpItem.getHeight();
tmpItem.setPosition(x, y+height-usedLength+viewOffsetY-offsetForDown);
tmpItem.draw(g);
if(usedLength>=(getHeight()+viewOffsetY)){
break;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -