我花了好几个小时寻找解决方案,但我尝试过的所有内容都无法正常工作,只会产生与下面代码相同的结果。
我正在尝试onClickListener
更改TextView
的可见性,但此代码始终设置我点击的那个,然后是下面的另一个。
class EventiAdapter extends ArrayAdapter<CustomEventi> {
List<CustomEventi> customEventi;
LayoutInflater inflater;
EventiAdapter(@NonNull Context context, @LayoutRes int resource, List<CustomEventi> objects) {
super(context, resource, objects);
customEventi = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
final EventiViewHolder eventiViewHolder;
if(view == null) {
eventiViewHolder = new EventiViewHolder();
view = inflater.inflate(R.layout.eventi_list, parent, false);
eventiViewHolder.imageTV = (ImageView) view.findViewById(R.id.image);
eventiViewHolder.nameTV = (TextView) view.findViewById(R.id.name);
eventiViewHolder.categoryTV = (TextView) view.findViewById(R.id.category);
eventiViewHolder.latLngTV = (TextView) view.findViewById(R.id.latLng);
eventiViewHolder.placeTV = (TextView) view.findViewById(R.id.place);
eventiViewHolder.dateTV = (TextView) view.findViewById(R.id.date);
eventiViewHolder.timeTV = (TextView) view.findViewById(R.id.time);;
eventiViewHolder.viewInfo = (Button) view.findViewById(R.id.viewInfo);
eventiViewHolder.informationTV = (TextView) view.findViewById(R.id.information);
eventiViewHolder.mapImage = (ImageView) view.findViewById(R.id.mapImage);
view.setTag(eventiViewHolder);
} else {
eventiViewHolder = (EventiViewHolder) view.getTag();
}
eventiViewHolder.viewInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(eventiViewHolder.informationTV.getVisibility() == View.VISIBLE) {
eventiViewHolder.informationTV.setVisibility(View.GONE);
eventiViewHolder.mapImage.setVisibility(View.GONE);
eventiViewHolder.viewInfo.setText("VISUALIZZA INFORMAZIONI");
}
else {
eventiViewHolder.informationTV.setVisibility(View.VISIBLE);
eventiViewHolder.mapImage.setVisibility(View.VISIBLE);
eventiViewHolder.viewInfo.setText("NASCONDI INFORMAZIONI");
}
}
});
System.out.println("POSITION: "+position);
CustomEventi cstEventi = customEventi.get(position);
eventiViewHolder.setDataIntoViewHolder(cstEventi);
return view;
}
}
提前感谢任何试图帮助我的人。
答案 0 :(得分:1)
尝试
1。将boolean
字段添加到CustomEventi
类
private boolean visible;
public boolean isVisible { return visible; }
public void setVisible(boolean visible) { this.visible = visible; }
2. :用以下代码替换适配器代码:
class EventiAdapter extends ArrayAdapter<CustomEventi> {
List<CustomEventi> customEventi;
LayoutInflater inflater;
EventiAdapter(@NonNull Context context, @LayoutRes int resource, List<CustomEventi> objects) {
super(context, resource, objects);
customEventi = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
final EventiViewHolder eventiViewHolder;
System.out.println("POSITION: "+position);
CustomEventi cstEventi = customEventi.get(position);
if(view == null) {
eventiViewHolder = new EventiViewHolder();
view = inflater.inflate(R.layout.eventi_list, parent, false);
eventiViewHolder.imageTV = (ImageView) view.findViewById(R.id.image);
eventiViewHolder.nameTV = (TextView) view.findViewById(R.id.name);
eventiViewHolder.categoryTV = (TextView) view.findViewById(R.id.category);
eventiViewHolder.latLngTV = (TextView) view.findViewById(R.id.latLng);
eventiViewHolder.placeTV = (TextView) view.findViewById(R.id.place);
eventiViewHolder.dateTV = (TextView) view.findViewById(R.id.date);
eventiViewHolder.timeTV = (TextView) view.findViewById(R.id.time);;
eventiViewHolder.viewInfo = (Button) view.findViewById(R.id.viewInfo);
eventiViewHolder.informationTV = (TextView) view.findViewById(R.id.information);
eventiViewHolder.mapImage = (ImageView) view.findViewById(R.id.mapImage);
view.setTag(eventiViewHolder);
} else {
eventiViewHolder = (EventiViewHolder) view.getTag();
}
eventiViewHolder.informationTV.setVisibility(cstEventi.isVisible() ? View.VISIBLE : GONE);
eventiViewHolder.mapImage.setVisibility(cstEventi.isVisible() ? View.VISIBLE : View.GONE);
eventiViewHolder.viewInfo.setText(cstEventi.isVisible() ? "NASCONDI INFORMAZIONI": "VISUALIZZA INFORMAZIONI");
eventiViewHolder.viewInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isVisible = customEventi.isVisible();
customEventi.setVisible(!isVisible);
notifyDataSetChanged();
}
});
eventiViewHolder.setDataIntoViewHolder(cstEventi);
return view;
}
}
答案 1 :(得分:-2)
试试这个....
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
if(view == null) {
view = inflater.inflate(R.layout.eventi_list, parent,false);
imageTV = (ImageView) view.findViewById(R.id.image);
nameTV = (TextView) view.findViewById(R.id.name);
categoryTV = (TextView) view.findViewById(R.id.category);
latLngTV = (TextView) view.findViewById(R.id.latLng);
placeTV = (TextView) view.findViewById(R.id.place);
dateTV = (TextView) view.findViewById(R.id.date);
timeTV = (TextView) view.findViewById(R.id.time);;
viewInfo = (Button) view.findViewById(R.id.viewInfo);
informationTV = (TextView) view.findViewById(R.id.information);
mapImage = (ImageView) view.findViewById(R.id.mapImage);
viewInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(informationTV.getVisibility() == View.VISIBLE) {
informationTV.setVisibility(View.GONE);
mapImage.setVisibility(View.GONE);
viewInfo.setText("VISUALIZZA INFORMAZIONI");
}
else {
informationTV.setVisibility(View.VISIBLE);
mapImage.setVisibility(View.VISIBLE);
viewInfo.setText("NASCONDI INFORMAZIONI");
}
}
});
System.out.println("POSITION: "+position);
CustomEventi cstEventi = customEventi.get(position);
setDataIntoViewHolder(cstEventi);
return view;
}