问题
I am having a listview in my app. Each listview row expands on tap and a hidden view becomes visible but problem is that hidden view also becomes visible for many others items in the list. I know the reason for this behaviour but I don't know how to resolve it. Here is my adapter class
public class ScheduleTaskAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
// List<InterestAndLanguageBean> interestAndLanguageBeans=new ArrayList<>();
List<BirdsDataBean> imageList = new ArrayList<>();
public ScheduleTaskAdapter(Context context, List<BirdsDataBean> imagesList) {
this.context = context;
this.imageList = imagesList;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return imageList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.birds_row, parent, false);
holder.iv_birds = (ImageView) convertView.findViewById(R.id.iv_bird);
holder.frameLayout = (FrameLayout) convertView.findViewById(R.id.frame_birds);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.detail_view);
holder.imageView = (ImageView) convertView.findViewById(R.id.up);
holder.iv_video = (ImageView) convertView.findViewById(R.id.iv_video);
holder.iv_sound = (ImageView) convertView.findViewById(R.id.iv_sound);
holder.tv_short_descript = (TextView) convertView.findViewById(R.id.tv_short_descript);
holder.tv_category = (TextView) convertView.findViewById(R.id.tv_category);
holder.tv_long_description = (TextView) convertView.findViewById(R.id.tv_long_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Uri myUri = Uri.parse(birdsUrlList.get(position).getUrl());
Glide.with(getActivity()).load(myUri).placeholder(R.drawable.birds).into(holder.iv_birds);
holder.tv_short_descript.setText(birdsUrlList.get(position).getDescrip());
holder.tv_long_description.setText(birdsUrlList.get(position).getDescrip_larga());
holder.tv_category.setText(birdsUrlList.get(position).getNombre_cientifico());
final ViewHolder finalHolder = holder;
holder.frameLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finalHolder.linearLayout.setVisibility(View.VISIBLE);
finalHolder.iv_sound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
//startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(birdsUrlList.get(position).getUrl_video())));
} catch (Exception e) {
e.printStackTrace();
}
}
});
finalHolder.iv_video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hitBirdSeenService(birdsUrlList.get(position).getIdave());
finalHolder.iv_video.setImageResource(R.drawable.eye_selected);
}
});
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finalHolder.linearLayout.setVisibility(View.GONE);
}
});
if (birdsUrlList.get(position).getFlag()==1)
{
holder.iv_video.setImageResource(R.drawable.eye_selected);
}else {
holder.iv_video.setImageResource(R.drawable.eye110);
}
//Picasso.with(context).load(myUri).placeholder(R.drawable.image).into(holder.pic);
//malevich.load(helperTaskBeanList.get(position).getImage()).into(holder.pic);
return convertView;
}
}
回答1:
ListView items are reusable. When you scroll the list, some items becomes "invisible" and the view item will be reused for the one that will become visible.
You do not keep the data of which item is expanded and which do not, so you cannot set the visibility to GONE or VISIBLE for "expanded" part depending on its previous "expanded" state in getView method. Simple workaround is to make a Map<Integer,Boolean>
and keep the state here, when expanding put(position,true)
and on hide put(position,false)
. In getView
boolean state = expandedStateMap.get(position);
finalHolder.linearLayout.setVisibility((state) ? View.VISIBLE : View.GONE);
Step zero (adding map for expanded state)
public class ScheduleTaskAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
// List<InterestAndLanguageBean> interestAndLanguageBeans=new ArrayList<>();
List<BirdsDataBean> imageList = new ArrayList<>();
Map<Integer,Boolean> expandedStateMap = new HashMap<>();
Step one:
//determine if we already expanded the item
Boolean state = expandedStateMap.get(position);
if (state == null) {
state = false;
}
finalHolder.linearLayout.setVisibility((state) ? View.VISIBLE : View.GONE);
holder.frameLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//save expanded state
expandedStateMap.put(position,true);
finalHolder.linearLayout.setVisibility(View.VISIBLE);
Step two:
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expandedStateMap.put(position,false);
finalHolder.linearLayout.setVisibility(View.GONE);
}
});
来源:https://stackoverflow.com/questions/35538353/onclick-event-repeats-with-listview-items