ViewFlipper in ListView row

泄露秘密 提交于 2019-12-01 10:03:43

问题


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


回答1:


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;
}


来源:https://stackoverflow.com/questions/14531897/viewflipper-in-listview-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!