Search code examples
androidlistviewandroid-listviewandroid-scrollbar

View Visiblity issue while scrolling of Listview


I have a custom listview with textview and imageview and the imageview like checkmark. on OnItemClickListener of Listview when textview is selected, i will make imageview Visibilty to visible to show that is selected.

 stickyList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                stickyList.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        // TODO Auto-generated method stub
                        String tag = (String) ((TextView) view
                                .findViewById(R.id.tvtag)).getText();
                         ImageView cb = (ImageView)
                         view.findViewById(R.id.ic_check);
                        if (cb.getVisibility() == View.VISIBLE) {
                            cb.setVisibility(
                                    View.GONE);

                            selectedtags.remove(tag);
                        } else {
                            cb.setVisibility(
                                    View.VISIBLE);
                            selectedtags.add(tag);
                        }
                    }
                });

It working fine untill Listview scrolldown. If I have selected first two postions textview of listview, when I scrolldown listview the visibilty of imageview(cb) is visible for 1st two postions.It means imageview is getting visible for same positions after scrolldown.

GetviewMethod:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.recenttag_list_item,
                parent, false);
        holder.text = (TextView) convertView.findViewById(R.id.tvtag);
        holder.checkmark = (ImageView) convertView
                .findViewById(R.id.ic_check);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.text.setText(data.get(position).mytag);
    return convertView;
}

Solution

  • The reason you are seeing that is because the list item views get recycled (this is what convertView is for). When you scroll the ListView, the previous item that scrolls off screen is passed back to your adapter as the convertView for the next position.

    You need to make sure you completely reset the state of your item view in each call to getView() so that changes like this don't leak from one use of the view item to the next. This may mean that you have to always inspect the current "checked" value of that position in getView() to determine if it ought to be visible or not.

    EDIT: Perhaps this will help...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        ViewHolder holder;
    
        /* Unchanged code omitted */
    
        holder.text.setText(data.get(position).mytag);
    
        boolean checkVisible = ???; //Use whatever logic your app has to determine if this position should be checked
        holder.checkmark.setVisibility(checkVisible);  //Always set the visibility to what it should be for this position
    
        return convertView;
    }