我正在开发一个巴士预订应用程序。我想当我选择座位时它会改变座位图像。并取消选择它应该更改为最后一张图像。我的其他功能很好地选择座位和显示座位号。在textview中取消选择也正常工作。但它只是不改变座位图像是问题所在。如果我点击座位号。 18即实际上是第20个,它改变第一个座位的图像,即座位号A.如果座位19然后B,如果座位20然后座位号。 1等等。
public class SeatSelection extends AppCompatActivity implements AdapterView.OnItemClickListener{
RecyclerView recyclerView;
SeatAdapter seatAdapter;
List<SeatDetails> seatDetailsList;
List seatNo;
TextView txtSeatNo;
TextView fare;
public Bitmap seatIcon;
public Bitmap seatSelect;
String cost;
int amt;
int charge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seat_selection);
seatIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.seat_layout_screen_nor_avl);
seatSelect = BitmapFactory.decodeResource(this.getResources(), R.drawable.seat_layout_screen_nor_std);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 5);
recyclerView.setLayoutManager(layoutManager);
seatAdapter = new SeatAdapter(this, loadBustListSeats());
recyclerView.setAdapter(seatAdapter);
}
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<SeatDetails> loadBustListSeats() {
seatDetailsList = new ArrayList<SeatDetails>();
int seatSerialNumber = 1;
for (int i = 0; i < 60; i++) {
if (isBlankSeat(i)) {
seatDetailsList.add(new SeatDetails("", true, false));
} else {
if (i == 0) {
seatDetailsList.add(new SeatDetails("A", true, false));
} else if (i == 1) {
seatDetailsList.add(new SeatDetails("B", true, false));
} else {
seatDetailsList.add(new SeatDetails(String.valueOf(seatSerialNumber), true, false));
seatSerialNumber = seatSerialNumber + 1;
}
}
}
return seatDetailsList;
}
public boolean isBlankSeat(int i) {
if ((60 - (i - 1)) < 5)
return false;
else
return i % 5 == 2 ;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
public void seatSelected(int pos)
{
charge = charge + amt;
seatNo.add(seatDetailsList.get(pos).getName());
txtSeatNo.setText(seatNo.toString());
fare.setText(String.valueOf(charge));
seatDetailsList.get(pos).setSelected(true);
seatAdapter.notifyDataSetChanged();
}
public void seatDeselected(int pos)
{
charge = charge - amt;
fare.setText(String.valueOf(charge));
seatNo.remove(seatDetailsList.get(pos).getName());
txtSeatNo.setText(seatNo.toString());
seatDetailsList.get(pos).setSelected(false);
seatAdapter.notifyDataSetChanged();
}
public class SeatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
class ViewHolderOriginal extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView;
ViewHolderOriginal(View itemView) {
super(itemView);
imageView= (ImageView) itemView.findViewById(R.id.item_image);
textView = (TextView) itemView.findViewById(R.id.item_text);
}
}
class ViewHolderBlank extends RecyclerView.ViewHolder{
ViewHolderBlank(View itemView) {
super(itemView);
}
}
public Context cntx;
public SeatAdapter(Context context, List<SeatDetails> list){
cntx = context;
seatDetailsList = list;
}
// 0= Original view 1= Blank View
@Override
public int getItemViewType(int position) {
if((getItemCount()-(position-1))<5)
return 0;
else
return position % 5 == 2 ? 1 : 0;
}
@Override
public int getItemCount() {
return 60;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (viewType == 1)
return new ViewHolderBlank(inflater.inflate(R.layout.item_blank, parent, false));
else
return new ViewHolderOriginal(inflater.inflate(R.layout.seatrow_grid, parent, false));
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderBlank) {
} else if (holder instanceof ViewHolderOriginal) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeatDetails seatDetails = seatDetailsList.get(position);
if (seatDetails.isSelected){
seatDeselected(position);
((ViewHolderOriginal) holder).imageView.setImageBitmap(seatIcon);
}
else
{
seatSelected(position);
((ViewHolderOriginal) holder).imageView.setImageBitmap(seatSelect);
}
}
});
}
}
}
}
答案 0 :(得分:1)
当用户选择/取消选择座位时,您需要刷新列表。因此,您可以使用notifydatasetchanged()在事件后刷新列表。
答案 1 :(得分:1)
试试这个:
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderBlank) {
} else if (holder instanceof ViewHolderOriginal) {
SeatDetails seatDetails = seatDetailsList.get(position);
if (seatDetails.isSelected){
((ViewHolderOriginal) holder).imageView.setImageBitmap(seatSelect);
}else{
((ViewHolderOriginal) holder).imageView.setImageBitmap(seatIcon);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeatDetails seatDetails = seatDetailsList.get(position);
if (seatDetails.isSelected){
seatDeselected(position);
}else{
seatSelected(position);
}
}
});
}
}
您忘记根据数据设置视图。由于函数中有notifyDataSetChanged(),因此无需在onClcik()中更改图像。希望有所帮助!