Я работаю над приложением бронирования автобусов.Я создал макет для спящего автобуса.Он создан правильно.У меня есть верхняя и нижняя палуба в автобусе.Поэтому, когда я открываю макет, по умолчанию открывается нижняя колода.И если выбрать несколько мест и перейти на верхнюю палубу, а затем снова перейти на нижнюю палубу, то это создаст новый адаптер, и я не вижу выбранные места.
SearSelectionSleeperActivity
public class SeatSelectionSleeper extends AppCompatActivity implements AdapterView.OnItemClickListener{
RecyclerView recyclerView;
RecyclerView recyclerViewupper;
SeatSelectionSleeper.SeatAdapter seatAdapter;
List<SeatSelectionSleeper.SeatDetails> seatDetailsList;
RecyclerView.LayoutManager layoutManager;
RecyclerView.LayoutManager layoutManagerUpper;
List seatNo;
TextView txtSeatNo;
TextView fare;
public Bitmap seatIcon;
public Bitmap seatSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seat_selection_sleeper);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.busDeckSelectionButtons);
final RelativeLayout driver = (RelativeLayout) findViewById(R.id.driver);
seatIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.sleeping_available);
seatSelect = BitmapFactory.decodeResource(this.getResources(), R.drawable.sleeper_select);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerViewupper = (RecyclerView) findViewById(R.id.recyclerupper);
layoutManager = new GridLayoutManager(this, 4);
layoutManagerUpper = new GridLayoutManager(this, 4);
recyclerView.setLayoutManager(layoutManager);
seatAdapter = new SeatSelectionSleeper.SeatAdapter(SeatSelectionSleeper.this, loadBustListSeats());
recyclerView.setAdapter(seatAdapter);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.lowerDeck:
recyclerView.setLayoutManager(layoutManager);
seatAdapter = new SeatSelectionSleeper.SeatAdapter(SeatSelectionSleeper.this, loadBustListSeats());
recyclerView.setAdapter(seatAdapter);
driver.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.VISIBLE);
recyclerViewupper.setVisibility(View.GONE);
break;
case R.id.upperDeck:
recyclerViewupper.setLayoutManager(layoutManagerUpper);
seatAdapter = new SeatSelectionSleeper.SeatAdapter(SeatSelectionSleeper.this, loadBustListSeatsUpper());
recyclerViewupper.setAdapter(seatAdapter);
driver.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
recyclerViewupper.setVisibility(View.VISIBLE);
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<SeatSelectionSleeper.SeatDetails> loadBustListSeats() {
seatDetailsList = new ArrayList<SeatSelectionSleeper.SeatDetails>();
int seatSerialNumber = 1;
for (int i = 0; i < 24; i++) {
if (isBlankSeat(i)) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("", true, false));
} else {
if (i == 0) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("A5", false, false));
} else if (i == 2) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("A1",false, false));
} else if (i == 3) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("A2",false, false));
} else if (i == 4) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("B5",false, false));
} else if (i == 6) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("B1",false, false));
} else if (i == 7) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("B2",false, false));
} else if (i == 8) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("C5",false, false));
} else if (i == 10) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("C1",false, false));
} else if (i == 11) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("C2",false, false));
} else if (i == 12) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("D5",false, false));
} else if (i == 14) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("D1",false, false));
} else if (i == 15) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("D2",false, false));
} else if (i == 16) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("E5",false, false));
} else if (i == 18) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("E1",false, false));
} else if (i == 19) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("E2",false, false));
} else if (i == 20) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("F5",false, false));
}else if (i == 22) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("F1",false, false));
}else if (i == 23) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("F2",false, false));
}
}
}
return seatDetailsList;
}
public List<SeatSelectionSleeper.SeatDetails> loadBustListSeatsUpper() {
seatDetailsList = new ArrayList<SeatSelectionSleeper.SeatDetails>();
int seatSerialNumber = 1;
for (int i = 0; i < 24; i++) {
if (isBlankSeat(i)) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("", true, false));
} else {
if (i == 0) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("A6", false, false));
} else if (i == 2) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("A3",false, false));
} else if (i == 3) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("A4",false, false));
} else if (i == 4) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("B6",false, false));
} else if (i == 6) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("B3",false, false));
} else if (i == 7) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("B4",false, false));
} else if (i == 8) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("C6",false, false));
} else if (i == 10) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("C3",false, false));
} else if (i == 11) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("C4",false, false));
} else if (i == 12) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("D6",false, false));
} else if (i == 14) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("D3",false, false));
} else if (i == 15) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("D4",false, false));
} else if (i == 16) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("E6",false, false));
} else if (i == 18) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("E3",false, false));
} else if (i == 19) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("E4",false, false));
} else if (i == 20) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("F6",false, false));
}else if (i == 22) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("F3",false, false));
}else if (i == 23) {
seatDetailsList.add(new SeatSelectionSleeper.SeatDetails("F4",false, false));
}
}
}
return seatDetailsList;
}
public boolean isBlankSeat(int i) {
if ((24 - (i - 1)) < 4)
return false;
else
return i % 4 == 1 ;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
public void seatSelected(int pos)
{
seatDetailsList.get(pos).setSelected(true);
charge = charge + amt;
fare.setText(String.valueOf(charge));
seatNo.add(seatDetailsList.get(pos).getName());
txtSeatNo.setText(seatNo.toString().replace("[","").replace("]",""));
seatAdapter.notifyDataSetChanged();
}
public void seatDeselected(int pos)
{
seatDetailsList.get(pos).setSelected(false);
charge = charge - amt;
fare.setText(String.valueOf(charge));
seatNo.remove(seatDetailsList.get(pos).getName());
txtSeatNo.setText(seatNo.toString().replace("[","").replace("]",""));
seatAdapter.notifyDataSetChanged();
}
public class SeatAdapter 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 SeatAdapter(Context context, List<SeatSelectionSleeper.SeatDetails> list){
this.cntx = context;
seatDetailsList = list;
}
// 0= Original view 1= Blank View
@Override
public int getItemViewType(int position) {
if((getItemCount() -(position-1))<4)
return 0;
else
return position % 4 == 1 ? 1 : 0;
}
@Override
public int getItemCount() {
return seatDetailsList.size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (viewType == 1)
return new SeatSelectionSleeper.SeatAdapter.ViewHolderBlank(inflater.inflate(R.layout.item_blank, parent, false));
else
return new SeatSelectionSleeper.SeatAdapter.ViewHolderOriginal(inflater.inflate(R.layout.seatrow_grid_sleep, parent, false));
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof SeatSelection.SeatAdapter.ViewHolderBlank) {
} else if
(holder instanceof SeatSelectionSleeper.SeatAdapter.ViewHolderOriginal) {
SeatSelectionSleeper.SeatDetails seatDetails = seatDetailsList.get(position);
if (seatDetails.isSelected){
((SeatSelectionSleeper.SeatAdapter.ViewHolderOriginal) holder).imageView.setImageBitmap(seatSelect);
}else {
((SeatSelectionSleeper.SeatAdapter.ViewHolderOriginal) holder).imageView.setImageBitmap(seatIcon);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeatSelectionSleeper.SeatDetails seatDetails = seatDetailsList.get(position);
if (seatDetails.isSelected){
seatDeselected(position);
}
else
{
seatSelected(position);
}
}
});
}
}
}
}