我正在制作巴士预订应用程序。我为座位预订创建了几个布局。每个布局都与几乎相同的代码完美配合。但只有1个布局有问题。每个布局都有下层和上层。我用Radio Buttons切换上下甲板的视图。但是,当我点击上层甲板时,它没有显示上层甲板。
SeatSelection
public class SeatSelectionSeatSleep3 extends AppCompatActivity implements AdapterView.OnItemClickListener{
RecyclerView recyclerLowerLeft;
RecyclerView recyclerLowerRight1;
RecyclerView recyclerLowerRight2;
RecyclerView recyclerUpper;
SeatSelectionSeatSleep3.SeatAdapterLowerLeft seatAdapterLowerLeft;
SeatSelectionSeatSleep3.SeatAdapterLowerRight1 seatAdapterLowerRight1;
SeatSelectionSeatSleep3.SeatAdapterLowerRight2 seatAdapterLowerRight2;
SeatSelectionSeatSleep3.SeatAdapterUpper seatAdapterUpper;
List<SeatSelectionSeatSleep3.SeatDetails> seatDetailsListLowerLeft;
List<SeatSelectionSeatSleep3.SeatDetails> seatDetailsListLowerRight1;
List<SeatSelectionSeatSleep3.SeatDetails> seatDetailsListLowerRight2;
List<SeatSelectionSeatSleep3.SeatDetails> seatDetailsListUpper;
RecyclerView.LayoutManager layoutManagerLowerLeft;
RecyclerView.LayoutManager layoutManagerLowerRight1;
RecyclerView.LayoutManager layoutManagerLowerRight2;
RecyclerView.LayoutManager layoutManagerUpper;
List seatNo;
List seatAvl;
TextView txtSeatNo;
TextView fare;
public Bitmap seatIcon;
public Bitmap seatSelect;
public Bitmap seatBooked;
public Bitmap sleepIcon;
public Bitmap sleepSelect;
public Bitmap sleepBooked;
String cost;
int amt;
int charge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seat_selection_seat_sleep3);
seatNo = new ArrayList();
seatAvl = new ArrayList();
amt = Integer.parseInt(cost);
charge = 0;
Button btnDone = (Button) findViewById(R.id.doneButton);
fare = (TextView) findViewById(R.id.fare);
txtSeatNo = (TextView) findViewById(R.id.txtSeatNo);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.busDeckSelectionButtons);
RadioButton rdoLower = (RadioButton) findViewById(R.id.lowerDeck);
RadioButton rdoUpper = (RadioButton) findViewById(R.id.upperDeck);
final LinearLayout driver = (LinearLayout) findViewById(R.id.driver);
seatIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.seater_available);
seatSelect = BitmapFactory.decodeResource(this.getResources(), R.drawable.seater_selected);
seatBooked = BitmapFactory.decodeResource(this.getResources(), R.drawable.seater_booked);
sleepIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.sleeping_available);
sleepSelect = BitmapFactory.decodeResource(this.getResources(), R.drawable.sleeper_select);
sleepBooked = BitmapFactory.decodeResource(this.getResources(), R.drawable.sleeper_booked);
recyclerLowerLeft = (RecyclerView) findViewById(R.id.recyclerLowerLeft);
recyclerLowerRight1 = (RecyclerView) findViewById(R.id.recyclerLowerRight1);
recyclerLowerRight2 = (RecyclerView) findViewById(R.id.recyclerLowerRight2);
recyclerUpper = (RecyclerView) findViewById(R.id.recyclerupper);
layoutManagerLowerLeft = new GridLayoutManager(this,1);
layoutManagerLowerRight1 = new GridLayoutManager(this, 4);
layoutManagerLowerRight2 = new GridLayoutManager(this, 4);
layoutManagerUpper = new GridLayoutManager(this, 4);
recyclerLowerLeft.setLayoutManager(layoutManagerLowerLeft);
recyclerLowerRight1.setLayoutManager(layoutManagerLowerRight1);
recyclerLowerRight2.setLayoutManager(layoutManagerLowerRight2);
seatAdapterLowerLeft = new SeatSelectionSeatSleep3.SeatAdapterLowerLeft(this, loadSeatListLowerLeft());
seatAdapterLowerRight1 = new SeatSelectionSeatSleep3.SeatAdapterLowerRight1(this, loadSeatListLowerRight1());
seatAdapterLowerRight2 = new SeatSelectionSeatSleep3.SeatAdapterLowerRight2(this, loadSeatListLowerRight2());
recyclerLowerLeft.setAdapter(seatAdapterLowerLeft);
recyclerLowerRight1.setAdapter(seatAdapterLowerRight1);
recyclerLowerRight2.setAdapter(seatAdapterLowerRight2);
recyclerLowerLeft.setNestedScrollingEnabled(false);
recyclerLowerRight1.setNestedScrollingEnabled(false);
recyclerLowerRight2.setNestedScrollingEnabled(false);
recyclerUpper.setNestedScrollingEnabled(false);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.lowerDeck:
recyclerLowerLeft.setLayoutManager(layoutManagerLowerLeft);
recyclerLowerRight1.setLayoutManager(layoutManagerLowerRight1);
recyclerLowerRight2.setLayoutManager(layoutManagerLowerRight2);
seatAdapterLowerLeft = new SeatSelectionSeatSleep3.SeatAdapterLowerLeft(SeatSelectionSeatSleep3.this, loadSeatListLowerLeft());
seatAdapterLowerRight1 = new SeatSelectionSeatSleep3.SeatAdapterLowerRight1(SeatSelectionSeatSleep3.this, loadSeatListLowerRight1());
seatAdapterLowerRight2 = new SeatSelectionSeatSleep3.SeatAdapterLowerRight2(SeatSelectionSeatSleep3.this, loadSeatListLowerRight2());
recyclerLowerLeft.setAdapter(seatAdapterLowerLeft);
recyclerLowerRight1.setAdapter(seatAdapterLowerRight1);
recyclerLowerRight2.setAdapter(seatAdapterLowerRight2);
driver.setVisibility(View.VISIBLE);
recyclerLowerLeft.setVisibility(View.VISIBLE);
recyclerLowerRight1.setVisibility(View.VISIBLE);
recyclerLowerRight2.setVisibility(View.VISIBLE);
recyclerUpper.setVisibility(View.GONE);
break;
case R.id.upperDeck:
recyclerUpper.setLayoutManager(layoutManagerUpper);
seatAdapterUpper = new SeatSelectionSeatSleep3.SeatAdapterUpper(SeatSelectionSeatSleep3.this, loadBustListSeatsUpper());
recyclerUpper.setAdapter(seatAdapterUpper);
recyclerUpper.setVisibility(View.VISIBLE);
driver.setVisibility(View.GONE);
recyclerLowerLeft.setVisibility(View.GONE);
recyclerLowerRight1.setVisibility(View.GONE);
recyclerLowerRight2.setVisibility(View.GONE);
break;
}
}
});
}
public class SeatDetails {
String name;
boolean isBlank;
boolean isSelected;
public SeatDetails(String name, boolean isBlank, boolean isSelected) {
this.name = name;
this.isBlank = isBlank;
this.isSelected = isSelected;
}
public String getName() {
return name;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
public List<SeatSelectionSeatSleep3.SeatDetails> loadBustListSeatsUpper() {
seatDetailsListUpper = new ArrayList<SeatSelectionSeatSleep3.SeatDetails>();
for (int i = 0; i < 24; i++) {
if (isBlankSeatUpper(i)) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("", true, false));
} else {
if (i == 0) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU1", false, false));
} else if (i == 2) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU2",false, false));
} else if (i == 3) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU3",false, false));
} else if (i == 4) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU4",false, false));
} else if (i == 6) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU5",false, false));
} else if (i == 7) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU6",false, false));
} else if (i == 8) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU7",false, false));
} else if (i == 10) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU8",false, false));
} else if (i == 11) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU9",false, false));
} else if (i == 12) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU10",false, false));
} else if (i == 14) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU11",false, false));
} else if (i == 15) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU12",false, false));
} else if (i == 16) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU13",false, false));
} else if (i == 18) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU14",false, false));
} else if (i == 19) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU15",false, false));
} else if (i == 20) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU16",false, false));
}else if (i == 22) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU17",false, false));
}else if (i == 23) {
seatDetailsListUpper.add(new SeatSelectionSeatSleep3.SeatDetails("SU18",false, false));
}
}
}
return seatDetailsListUpper;
}
public boolean isBlankSeatUpper(int i) {
if (i == 1)
return true;
if (i == 5)
return true;
if (i == 9)
return true;
if (i == 13)
return true;
if (i == 17)
return true;
if (i == 21)
return true;
else
return false;
}
public class SeatAdapterUpper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
class ViewHolderOriginal extends RecyclerView.ViewHolder{
ImageView imageView;
ViewHolderOriginal(View itemView) {
super(itemView);
imageView= (ImageView) itemView.findViewById(R.id.item_image);
}
}
class ViewHolderBlank extends RecyclerView.ViewHolder{
ViewHolderBlank(View itemView) {
super(itemView);
}
}
public Context cntx;
public SeatAdapterUpper(Context context, List<SeatSelectionSeatSleep3.SeatDetails> list){
this.cntx = context;
seatDetailsListUpper = list;
}
// 0= Original view 1= Blank View
@Override
public int getItemViewType(int position) {
if (position == 1)
return 1;
if (position == 5)
return 1;
if (position == 9)
return 1;
if (position == 13)
return 1;
if (position == 17)
return 1;
if (position == 21)
return 1;
else
return 0;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (viewType == 1)
return new SeatSelectionSeatSleep3.SeatAdapterUpper.ViewHolderBlank(inflater.inflate(R.layout.item_blank, parent, false));
else
return new SeatSelectionSeatSleep3.SeatAdapterUpper.ViewHolderOriginal(inflater.inflate(R.layout.seatrow_grid_sleep, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof SeatSelectionSeatSleep3.SeatAdapterUpper.ViewHolderBlank) {
} else if
(holder instanceof SeatSelectionSeatSleep3.SeatAdapterUpper.ViewHolderOriginal) {
SeatSelectionSeatSleep3.SeatDetails seatDetails = seatDetailsListUpper.get(position);
if (seatNo.contains(seatDetails.getName())) {
((SeatSelectionSeatSleep3.SeatAdapterUpper.ViewHolderOriginal) holder).imageView.setImageBitmap(sleepSelect);
} else if (seatAvl.contains(seatDetails.getName())) {
((SeatSelectionSeatSleep3.SeatAdapterUpper.ViewHolderOriginal) holder).imageView.setImageBitmap(sleepIcon);
} else {
((SeatSelectionSeatSleep3.SeatAdapterUpper.ViewHolderOriginal) holder).imageView.setImageBitmap(sleepBooked);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeatSelectionSeatSleep3.SeatDetails seatDetails = seatDetailsListUpper.get(position);
if (seatNo.contains(seatDetails.getName())) {
seatDeselectedUpper(position);
} else if (seatAvl.contains(seatDetails.getName())) {
seatSelectedUpper(position);
}
}
});
}
}
@Override
public int getItemCount() {
return seatDetailsListUpper.size();
}
}
}
仅添加了与Upper Deck相关的代码。拆下下甲板适配器。当我点击Lower时,下层甲板工作正常。但问题只在于上层甲板。
答案 0 :(得分:1)
要回答这个问题非常困难,因为您的代码看起来有些令人困惑,您需要删除一些业务逻辑,只需包含影响手头问题的代码。
我强烈建议您阅读一本名为Clean Code by Robert C. Martin的书,以提高您的编码能力。对于Android,我建议您查看this page。通过清理代码并使其更加模块化和有序,您将能够更好地自行调试和解决此问题。