0

在我调用 notifyDataSetChanged 后,我对 onItemSelected 的所有引用都已过时。

例如

gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                onFront1 = (ImageView) view;

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

获取视图

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

             imView = new ImageView(this.myContext);

            imView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            imView.setOnTouchListener(new OnTouchListener() {


                public boolean onTouch(View v, MotionEvent event) {

                    ImageView view = (ImageView) v;

                    onFront2 = view;
                    return true; // indicate event was handled
                }
            });
            onFront3 = imView;
            return imView;
        }

旋转

public void rotateS(View v) {
        ImageView iv = onFront;
        Bitmap b = ((BitmapDrawable) iv.getDrawable()).getBitmap();
        Matrix matrix = new Matrix();
        matrix.postRotate(geg);
        Bitmap bMapRotate = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                b.getHeight(), matrix, true);
        iv.setImageBitmap(bMapRotate);
        geg = 90;
        Log.d("rorate", "yes");
    }

onFront引用了旧的 imageView,因此我不能再使用它了。如何再次调用 onItemselected?

更详细一点:

我正在使用 imageView 并在rotateS函数中使用它。它使用 onFront 但将其替换为全局变量 onFront1|onFront2|onFront3
onFron1 允许在图像出现在屏幕上时立即旋转图像,但在 notifydatasetChanged 后变得无用。
onFront2 不受 notifyDataSetChanged 的​​影响,但它仅在屏幕被录制后才起作用(非常合乎逻辑)
onFront3 根本不起作用。
我想要实现的 - 保持对当前 imageview 的引用并在 imageview 更新后立即更新它。

4

1 回答 1

0

最好onFront在调用的同一代码块重新分配notifyDataSetChanged.

如果您无法在您的场景中做到这一点,那么您需要额外的机制来跟踪您的 ListAdapter 中的有效负载。

例如,您想用特定颜色标记所选项目。您可以引入一个 ItemData 类:

public class ItemData{
    public bool IsSelected;
    // some other data if you need, maybe link to the image view
}

您的 ListAdapter 应该使用这些对象的列表 (listData) 并实现 getItem() 方法:

public Object getItem(int i) {
    return this.listData.get(i);
}

当您选择项目时 - 在您调用的侦听器中

((ItemData)parent.getItem(position)).IsSelected = true;

然后,如果您在之前选择的上方添加新项目,您还应该在里面添加新项目listData。先前选择的项目的位置会改变,但关联的ItemData实例不会。

于 2013-07-31T07:52:24.050 回答