Search code examples
androidandroid-listviewviewflipper

ViewFlipper in ListView row


I have a ListView with an XML view for each of the rows.

On each row there is a ViewFlipper and a Button, the intention being to press the button and flip through the views on the ViewFlipper.

The problem is I can not get the Button to flip the correct ViewFlipper. I am setting the row up in the ListView adapter so I assume this is where I should handle the button click.

Although the click is being handled, the ViewFlipper being 'flipped' is on a different row. I assume this is because the adapter is recycling the views - I just can not work out how to resolve this.

My code is

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = mLayoutInflater.inflate(mLayout, null);

        viewHolder = new ViewHolder();
        viewHolder.flipper = (ViewFlipper) convertView.findViewById(R.id.viewConfFlipper1);
        viewHolder.v2FieldName = (TextView) convertView.findViewById(R.id.tvLongName);
        viewHolder.button01 = (ImageButton) convertView.findViewById(R.id.imageButton1);

        viewHolder.button01.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                viewHolder.flipper.showNext();
            }
        });

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.v2FieldName.setText(items.get(position).get("name"));

    return convertView;
}

Any help appreciated


Solution

  • Managed to get it working now, the code is

    public View getView(int position, View convertView, ViewGroup parent) {
    
        final ViewFlipper flipperTemp;
    
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(mLayout, null);
    
            viewHolder = new ViewHolder();
            viewHolder.flipper = (ViewFlipper) convertView.findViewById(R.id.viewConfFlipper1);
            viewHolder.flipper.setDisplayedChild(0);
            viewHolder.v2FieldName = (TextView) convertView.findViewById(R.id.tvLongName);
            viewHolder.button01 = (ImageButton) convertView.findViewById(R.id.imageButton1);    
            convertView.setTag(viewHolder);
    
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.flipper.setDisplayedChild(0);
        }
    
    
        flipperTemp = (ViewFlipper) convertView.findViewById(R.id.viewConfFlipper1);
    
        viewHolder.v2FieldName.setText(items.get(position).get("name"));        
        viewHolder.button01.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                flipperTemp.showNext();
            }
        });
    
        return convertView;
    }