gpt4 book ai didi

java - 选择和取消选择 recyclerview 项目未正确更改图像

转载 作者:行者123 更新时间:2023-12-02 02:09:24 27 4
gpt4 key购买 nike

我正在开发一个巴士预订应用程序。我希望当我选择座位时它会改变座位图像。取消选择它应该更改为最后一张图像。我的其他功能运行良好,包括选择座位和显示座位号。在 TextView 中并取消选择也工作正常。但它只是不改变座位形象就是问题所在。如果我点击座位号。 18,即实际上 20 日,它会更改第一个座位的图像,即座位号 A。如果座位 19,则座位号为 B;如果座位为 20,则座位号。 1 等等。

See here i selected seat no 18 and it changes icon of first

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);
}
}
});
}
}

}
}

最佳答案

试试这个:

@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);
}
}
});
}
}

您忘记根据数据设置 View 。由于您的函数中有notifyDataSetChanged(),因此无需在onClcik() 中更改图像。希望有帮助!

关于java - 选择和取消选择 recyclerview 项目未正确更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com