简体   繁体   中英

ListView OnLongClick strange behaviour

I have a ListView within my project. It has many elements, and it uses a custom adapter, since its populated dynamically from a rails server.

I want to change the content of a ListItem when the item is longpressed. In order to achieve this, I have 2 layouts inside the ListItem, with one visible and one hidden.

The issue is that when I longpress an item, the layout changes (As expected), but other ListItems are also affected, and changed in the same way. This appear to occur once for every 5 items, and I cant figure out why.

This is the LongClickListener I'm using, it is located inside de GetView method on the custom adapter:

View v = convertView;

if (v == null){
   LayoutInflater vi =
      (LayoutInflater)getActivity().getSystemService(
         Context.LAYOUT_INFLATER_SERVICE);
   v = vi.inflate(R.layout.list_item, null);
}

final LinearLayout placeInfo =
   (LinearLayout) v.findViewById(R.id.list_item_info);

final RelativeLayout placeBrief =
   (RelativeLayout)v.findViewById(R.id.list_item_brief);

v.setOnLongClickListener(new OnLongClickListener(){

   @Override public boolean onLongClick(View v) {
      placeInfo.setVisibility(View.GONE);
      placeBrief.setVisibility(View.VISIBLE);
      return false;
   }});

I would appreciate any help, many thanks in advance.

ListViews recycle Views, so you only have a few views for all of your items. You're directly changing one of these view instances to switch between the info|brief. What you need is to save the status of the info|brief flag for the affected position somewhere else (eg a list of positions that should be "briefs" in the adapter). That way when you come back into getView() you can display the right one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM