Flight オブジェクトを表すアイテムで満たされた LWUIT List があります。各 Flight アイテムには、フライト番号、各レッグの出発時刻と到着時刻、およびチケットの価格が表示されます。
一部のフライトには 2 つのレッグ (ストップオーバー、航空機の変更、または航空会社の変更を含むフライトのセグメントを意味するレッグ) があるため、これらのフライト アイテムは、各レッグの出発時刻と到着時刻、および出発地のコードでレンダリングする必要があります。と各脚の目的地。
最初は脚が 1 つしかないアイテムは正常にレンダリングされますが、リストをスクロールして複数の脚を持つアイテムがレンダリングされ始めると、アイテムは情報がカットされて間違ってレンダリングされ、さらに悪いことに、すべての脚がレンダリングされます。リストのアイテムは、脚が 1 つしかないアイテムでも、この影響を受けます。
このスクリーンショットでは、複数のレッグを持つアイテムに到達する前に、リスト アイテムがどのように見えるかを確認できます。
次に、リストをスクロールして複数のレッグを持つアイテムに到達すると、すべてのアイテムがレンダリングされ、レッグ時間情報がカットされ、複数のレッグが情報の上に貼り付けられた最初のアイテムの時間情報が配置されます。
以下に、List Renderer クラスを示します。
package com.yallaya.screens.elements;
import com.dotrez.model.Flight;
import com.dotrez.model.Segment;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.yallaya.Main;
/*
* Renderer for a List of Flights to display
*/
public class FlightItem extends Container implements ListCellRenderer {
/*
* Components for this List Item
*/
private Label flightNumberLbl = new Label("");
private Label origin1Lbl = new Label("");
private Label destination1Lbl = new Label("");
private Label origin2Lbl = new Label("");
private Label destination2Lbl = new Label("");
private Label priceLbl = new Label("");
/*
* Constructor for this List Item
*/
public FlightItem() {
this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
this.getStyle().setBgColor(0xf0f0f0);
this.getStyle().setBgTransparency(255);
this.getStyle().setMargin(0, 0, 0, 0);
this.getStyle().setPadding(5, 5, 0, 0);
flightNumberLbl.getStyle().setMargin(14, 0, 0, 0);
origin1Lbl.getStyle().setPadding(0, 0, 24, 0);
origin1Lbl.getStyle().setMargin(0, 0, 0, 0);
destination1Lbl.getStyle().setPadding(0, 0, 24, 0);
destination1Lbl.getStyle().setMargin(0, 0, 0, 0);
origin2Lbl.getStyle().setPadding(0, 0, 24, 0);
origin2Lbl.getStyle().setMargin(0, 0, 0, 0);
destination2Lbl.getStyle().setPadding(0, 0, 24, 0);
destination2Lbl.getStyle().setMargin(0, 0, 0, 0);
priceLbl.getStyle().setPadding(0, 0, 24, 0);
priceLbl.getStyle().setMargin(14, 0, 0, 0);
priceLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
this.addComponent(flightNumberLbl);
this.addComponent(origin1Lbl);
this.addComponent(destination1Lbl);
this.addComponent(origin2Lbl);
this.addComponent(destination2Lbl);
this.addComponent(priceLbl);
}
public void updateColorItem(boolean isSelected){
if(isSelected){
this.getStyle().setBgColor(0x9a3d96);
flightNumberLbl.getStyle().setFgColor(0xffffff);
origin1Lbl.getStyle().setFgColor(0xffffff);
destination1Lbl.getStyle().setFgColor(0xffffff);
origin2Lbl.getStyle().setFgColor(0xffffff);
destination2Lbl.getStyle().setFgColor(0xffffff);
priceLbl.getStyle().setFgColor(0xffffff);
}
else{
this.getStyle().setBgColor(0xffffff);
flightNumberLbl.getStyle().setFgColor(0x9a3d96);
origin1Lbl.getStyle().setFgColor(0x555555);
destination1Lbl.getStyle().setFgColor(0x555555);
origin2Lbl.getStyle().setFgColor(0x555555);
destination2Lbl.getStyle().setFgColor(0x555555);
priceLbl.getStyle().setFgColor(0x555555);
}
}
/*
* Functions called to render this List Item
*/
public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
Flight tmp = (Flight) value;
int displayIndex = index + 1;
flightNumberLbl.setText(displayIndex + ". " + Main.localize("VUELO") + " #" + tmp.getFlightNumber());
Segment tmpSeg = (Segment) tmp.getSegments().elementAt(0);
String originStr = Main.localize("SALIDA") + " " + "(" + tmpSeg.getOrigin().getCode() + ")" + ": " + tmpSeg.getDepartureTimeString();
String destinationStr = Main.localize("LLEGADA") + "(" + tmpSeg.getDestination().getCode() + ")" + ": " + tmpSeg.getArrivalTimeString() + "";
origin1Lbl.setText(originStr);
destination1Lbl.setText(destinationStr);
if(tmp.getSegments().size() > 1){
tmpSeg = (Segment) tmp.getSegments().elementAt(1);
originStr = Main.localize("SALIDA") + " " + "(" + tmpSeg.getOrigin().getCode() + ")" + ": " + tmpSeg.getDepartureTimeString();
destinationStr = Main.localize("LLEGADA") + "(" + tmpSeg.getDestination().getCode() + ")" + ": " + tmpSeg.getArrivalTimeString() + "";
origin2Lbl.setText(originStr);
destination2Lbl.setText(destinationStr);
}
/************************************************************************/
/******************* UPDATE PIECE OF CODE STARTS HERE *******************/
/************************************************************************/
else{
origin2Lbl.setText("");
destination2Lbl.setText("");
}
/************************************************************************/
/******************* UPDATE PIECE OF CODE ENDS HERE *******************/
/************************************************************************/
priceLbl.setText("$" + tmp.getAdultFare() + " " + tmp.getCurrency().getCode());
updateColorItem(isSelected);
return this;
}
/*
* Function called to get this component when it get focus
*/
public Component getListFocusComponent(List list) {
return this;
}
}
他のプラットフォームでは、この問題は「ゴースト」と呼ばれていると思いますが、Nokia Asha の LWUIT でそれを回避する方法を見つけられませんでした。
すべてのアイテムをレンダリングするために同じレンダラーが使用されているため、これが問題の原因である可能性があることはわかっています。その場合、必要に応じて 1 つのアイテムを別の方法でレンダリングする方法が必要です。
アイテムの 1 つが異なる長さの情報を表示する必要がある場合に、リスト内のすべてのアイテムが変更されるのを防ぐにはどうすればよいですか?
編集
コードを更新しました。現在の Flight オブジェクトに複数の脚がない場合は、2 番目の脚のラベルを空の文字列で設定します。これにより、脚が 1 つしかないアイテムの問題が解決されます。
それで、質問のトピックを、複数の脚を持つフライトアイテムに残っている問題に変更します。アイテムのサイズが正しく変更されず、それぞれを内容に合わせて調整する方法が見つかりません。すべてがペイントされています同じサイズ。
アイテムの高さを正しく調整するにはどうすればよいですか?
編集2
if 節内でレンダラーの高さを変更しようとしました。ここでは、以下を使用して脚 (モデルではセグメントと呼ばれます) の数を確認しますthis.setHeight(800);
。
if(tmp.getSegments().size() > 1){
.....
flightHolder.setHeight(800);
this.setHeight(800);
}
else{
.....
flightHolder.setHeight(200);
this.setHeight(200);
}
しかし、その方法ではまったく異なる高さのサイズを取得できませんでした。
それから私は使用しようとしましたがthis.preferredH(800);
、最終的にサイズが変わりました:
if(tmp.getSegments().size() > 1){
......
flightHolder.setPreferredH(800);
this.setPreferredH(800);
}
else{
...........
flightHolder.setPreferredH(200);
this.setPreferredH(200);
}
しかし、すべてのアイテムは 800 でレンダリングされています。これは、最後のフライトに複数の脚があるためだと思いますが、実際にはわかりません。複数の脚があるフライトの高さを大きく設定してから、にリセットすることを望んでいました高さが 1 つしかない場合は、より低い高さが機能しますが、高さがすべてのアイテムで同じに強制されているようです。誰かがこれを確認できますか?