表格滚动未按预期工作

Posted

技术标签:

【中文标题】表格滚动未按预期工作【英文标题】:Table scroll not working as expected 【发布时间】:2013-10-31 11:02:10 【问题描述】:

我正在处理 LWUIT 中具有固定标题的某个表。让我直奔问题。当我在模拟器上运行它时,触摸界面工作得非常好,并且表单的滚动工作完美无瑕,但是当我尝试使用导航键导航时,按下的左键会产生故障。问题是当我导航到右边然后回到左边时,左边的滚动条在第二列上出现,无论我按多少次左键,它都不会移动到最左边。经过多次努力,我找到了一种组合,即连续随机按向上和向左键,然后表单滚动到最左侧。我不明白为什么会这样。我在所有模拟器和 2 个实际手机上都试过了,但仍然是同样的问题。是否有一些代码会产生这种问题?我不确定。对此的任何帮助将不胜感激。此外,我发布了整个源代码,以便任何有兴趣的人可以按原样尝试并查看这个问题是否出现在他的模拟器上。

import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.table.DefaultTableModel;
import com.sun.lwuit.table.Table;
import com.sun.lwuit.table.TableModel;
import javax.microedition.lcdui.Canvas;
import javax.microedition.midlet.MIDlet;

public class FixedTableDemo extends MIDlet implements ActionListener 

private static final String[] TITLES = "Title 1", "Title 2", "Title 3", "Title 4", "Title 5";
private static final int ROWS = 100;
private static Dimension[] TITLE_SIZES;
Button b[][];



static class MirroredTable extends Table 

   private MirroredTable mirrorTo;

   public MirroredTable(TableModel m) 

       super(m);

   



   public MirroredTable(TableModel m, boolean b) 

       super(m, b);

   

   public void setScrollX(int x) 

       super.setScrollX(x);

       if(isDragActivated()) 
           mirrorTo.setScrollX(x);
       

   



   /**

    * @param mirrorTo the mirrorTo to set

    */

   public void setMirrorTo(MirroredTable mirrorTo) 
       this.mirrorTo = mirrorTo;
   



   public Component createCell(Object value, int row, int column, boolean editable) 

       Component c = super.createCell(value, row, column, editable);

       if(TITLE_SIZES != null && TITLE_SIZES[column] != null) 
           c.setPreferredSize(TITLE_SIZES[column]);
       

       return c;

   



MirroredTable titlesTable;

MirroredTable bodyTable;

public void startApp() 

   Display.init(this);



   Form f = new Form("Table Title");

   titlesTable = new MirroredTable(new DefaultTableModel(TITLES, new Object[0][0]));

   //TITLE_SIZES = new Dimension[TITLES.length];

   /*for(int iter = 0 ; iter < TITLES.length ; iter++) 
    * 
    * TITLE_SIZES[iter] = titlesTable.createCell(TITLES[iter], -1, iter, false).getPreferredSize();
    * 
    * */

   titlesTable.removeAll();

   Button bt[]=new Button[5];

   for(int i=0;i<5;i++)
   
       bt[i]=new Button("Title  "+i);
       bt[i].setUIID("");
       titlesTable.addComponent(bt[i]);
   


   Object[][] body = new Object[ROWS][TITLES.length];

   /* for(int rows = 0 ; rows < body.length ; rows++) 
    * 
    * for(int cols = 0 ; cols < body[rows].length ; cols++) 
    * 
    * body[rows][cols] = "" + rows + ", " + cols;
    * 
    * 
    * 
    * */

   bodyTable = new MirroredTable(new DefaultTableModel(TITLES, body), false);

   bodyTable.setMirrorTo(titlesTable);

   titlesTable.setMirrorTo(bodyTable);

   bodyTable.setScrollable(true);

   titlesTable.setScrollableX(true);

   f.setScrollable(false);

   f.setLayout(new BorderLayout());

   titlesTable.setTensileDragEnabled(false);

   bodyTable.setTensileDragEnabled(false);

   titlesTable.setIsScrollVisible(false);

   f.addComponent(BorderLayout.NORTH, titlesTable);

   f.addComponent(BorderLayout.CENTER, bodyTable);


   f.addGameKeyListener(Canvas.LEFT, this);
   f.addGameKeyListener(Canvas.RIGHT, this);
   f.addGameKeyListener(Canvas.UP, this);

   f.setCyclicFocus(false);


   /**
    * buttons adding and their listener
    */
   b=new Button[50][5];
   //Label l[][]=new Label[50][5];

   for(int i=0;i<50;i++)
     
     for(int j=0; j<5;j++)
     
     b[i][j]=new Button("Title "+i);
     b[i][j].setUIID("");
     //l[i][j]=new Label("Title 1");
     bodyTable.addComponent(b[i][j]);

     
     





   f.show();

 

 public void actionPerformed(ActionEvent evt) 
    if(evt.getKeyEvent()==Canvas.LEFT)
    
        System.out.println(bodyTable.getScrollX());
        titlesTable.setScrollX(bodyTable.getScrollX());

    
    else if(evt.getKeyEvent()==Canvas.RIGHT)
    
        System.out.println(bodyTable.getScrollX());
        titlesTable.setScrollX(bodyTable.getScrollX());
    
    else if(evt.getKeyEvent()==Canvas.UP)
    
        System.out.println("Up:"+bodyTable.getScrollX()+","+bodyTable.getScrollY());
    
 

 public void pauseApp() 

 



 public void destroyApp(boolean unconditional) 

 


【问题讨论】:

【参考方案1】:

我不知道这是否是实现这一目标的准确方法。但是我对您的代码进行了一些更改。 看看这个。

import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.table.DefaultTableModel;
import com.sun.lwuit.table.Table;
import com.sun.lwuit.table.TableModel;
import javax.microedition.lcdui.Canvas;
import javax.microedition.midlet.MIDlet;

public class FixedTableDemo extends MIDlet implements ActionListener 

private static final String[] TITLES = "Title 1", "Title 2", "Title 3", "Title 4", "Title 5";
private static final int ROWS = 100;
private static Dimension[] TITLE_SIZES;
Button b[][];
int screenWidth;
int lastMove = 0;
int lastModifiedMove = 1;
int temp;

static class MirroredTable extends Table 

    private MirroredTable mirrorTo;

    public MirroredTable(TableModel m) 

        super(m);

    

    public MirroredTable(TableModel m, boolean b) 

        super(m, b);

    

    public void setScrollX(int x) 

        super.setScrollX(x);

        if (isDragActivated()) 

            mirrorTo.setScrollX(x);

        

    

    /**
     *
     * @param mirrorTo the mirrorTo to set
     *
     */
    public void setMirrorTo(MirroredTable mirrorTo) 

        this.mirrorTo = mirrorTo;

    

    public Component createCell(Object value, int row, int column, boolean editable) 

        Component c = super.createCell(value, row, column, editable);

        if (TITLE_SIZES != null && TITLE_SIZES[column] != null) 

            c.setPreferredSize(TITLE_SIZES[column]);

        

        return c;

    

MirroredTable titlesTable;
MirroredTable bodyTable;

public void startApp() 

    Display.init(this);


    screenWidth = Display.getInstance().getDisplayWidth();

    Form f = new Form("Table Title");

    titlesTable = new MirroredTable(new DefaultTableModel(TITLES, new Object[0][0]));
    titlesTable.removeAll();

    Button bt[] = new Button[5];

    for (int i = 0; i < 5; i++) 
        bt[i] = new Button("Title" + (i + 10));
        bt[i].setUIID("");
        titlesTable.addComponent(bt[i]);
    


    Object[][] body = new Object[ROWS][TITLES.length];

    bodyTable = new MirroredTable(new DefaultTableModel(TITLES, body), false);

    bodyTable.setMirrorTo(titlesTable);

    titlesTable.setMirrorTo(bodyTable);

    bodyTable.setScrollable(true);

    titlesTable.setScrollableX(true);

    f.setScrollable(false);

    f.setLayout(new BorderLayout());

    titlesTable.setTensileDragEnabled(false);

    bodyTable.setTensileDragEnabled(false);

    titlesTable.setIsScrollVisible(false);

    f.addComponent(BorderLayout.NORTH, titlesTable);

    f.addComponent(BorderLayout.CENTER, bodyTable);


    f.addGameKeyListener(Canvas.LEFT, this);
    f.addGameKeyListener(Canvas.RIGHT, this);
    f.addGameKeyListener(Canvas.UP, this);

    f.setCyclicFocus(false);


    /**
     * buttons adding and their listener
     */
    b = new Button[50][5];
    //Label l[][]=new Label[50][5];

    for (int i = 0; i < 50; i++) 
        for (int j = 0; j < 5; j++) 
            b[i][j] = new Button("Title" + (i + 10));
            b[i][j].setUIID("");
            //l[i][j]=new Label("Title 1");
            bodyTable.addComponent(b[i][j]);

        
    





    f.show();



public void actionPerformed(ActionEvent evt) 


    if (evt.getKeyEvent() == Canvas.LEFT) 
        if (lastMove != 0) 

            lastMove -= 20;

            bodyTable.setScrollX(lastMove);
            titlesTable.setScrollX(lastMove);
        

     else if (evt.getKeyEvent() == Canvas.RIGHT) 
        if (lastMove != lastModifiedMove) 

            lastMove += 20;


            bodyTable.setScrollX(lastMove);

            titlesTable.setScrollX(lastMove);

            if (temp == bodyTable.getScrollX()) 

                lastModifiedMove = temp;
                lastMove = temp;

             else 
                temp = bodyTable.getScrollX();
            

         else 
            lastMove = lastModifiedMove;
        
     else if (evt.getKeyEvent() == Canvas.UP) 
    


public void pauseApp() 


public void destroyApp(boolean unconditional) 


【讨论】:

以上是关于表格滚动未按预期工作的主要内容,如果未能解决你的问题,请参考以下文章

QGraphicsView 滚动条策略未按预期工作

MySQL ResultSet 可滚动/可更新未按预期工作

CSS 溢出未按预期工作,无法滚动到 div 底部

材料表示例未按预期工作

GSAP ScrollTrigger 未按预期工作

UIScroll 视图未按预期工作