android ViewTreeObserver,无法从 onclicklistener 获取正确的组件

android ViewTreeObserver, can not get right component from onclicklistener

我在GridLayout里面有六个Linearlayout:

     <GridLayout
    android:id="@+id/gridRollShutter"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:columnCount="3"
    android:rowCount="2" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:background="@drawable/border"
        android:gravity="center" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="0"
        android:background="@drawable/border"
        android:gravity="center" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="0"
        android:background="@drawable/border"
        android:gravity="center" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="1"
        android:background="@drawable/border"
        android:gravity="center" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="1"
        android:background="@drawable/border"
        android:gravity="center" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="1"
        android:background="@drawable/border"
        android:gravity="center" />
</GridLayout

>

然后我使用 gridLayout.getViewTreeObserver().addOnGlobalLayoutListener() 检查布局。每个 Layout 我都放了一个 ImageViewsetOnClickListener 给它。当它触发时,监听器总是调用最后一个组件,虽然我点击了第一个Layout 的ImageView。 这是我的简单代码:

             gridLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        // remove the before treeObserver
                        gridLayout.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);

                        // get the measure size of the grid layout
                        int width = gridLayout.getMeasuredWidth();
                        int height = gridLayout.getMeasuredHeight();


                        for (int i = 0; i < gridLayout.getChildCount(); i++) {
                            View childView = inflater
                                        .inflate(
                                                R.layout.item_roller_horizontal_list,
                                                lnlTemp, false);
                        // loop to access each component
                            LinearLayout lnlTemp = (LinearLayout) gridLayout
                                    .getChildAt(i);

                            imgAlpha = (ImageView) childView
                                        .findViewById(R.id.imgRollerAlpha);
                            EnRollerShutterItem enRollerShutterItem = listRollerShutter
                                        .get(i);
                                imgAlpha.setTag(enRollerShutterItem
                                        .getDimPercent());

                                imgAlpha.setOnLongClickListener(new OnLongClickListener() {
                                    @Override
                                    public boolean onLongClick(View v) {
                                        // TODO Auto-generated method stub
                                        Logger.error("tag: " + v.getTag());
                                        imgAlpha.startAnimation(animationEnlarge);
                                        return true;
                                    }
                                });
                              ...............

点击事件触发时。我得到了准确的标签,但事件总是发生在最后 Linearlayout 之后的最后 imageview。我不知道为什么会这样。

请给我检查出问题,在此先感谢。

When click Event was triggerd. I get the exactly tag, but the event always happen in the last imageview from last Linearlayout. I do not know why it happen.

我假设 "event" 是指在该图像的 OnClickListener 中开始的动画。在这种情况下,这可能会发生,因为在 OnClickListener 中您使用 imgAlpha 引用来启动动画。由于 for 循环,该引用将始终指向最后一个 ImageView。解决方案是直接使用 v 参数,因为它代表设置 OnClickListener 的视图, ImageView:

//...
Logger.error("tag: " + v.getTag());
v.startAnimation(animationEnlarge);