如何强制视图重新计算它的位置改变后父 [英] How to force View to recalculate it's position after changing parent

查看:228
本文介绍了如何强制视图重新计算它的位置改变后父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它已经有些日子,因为我有这个问题。

这是<一个一个改写href=\"http://stackoverflow.com/questions/20228834/android-repositioning-view-wrong-y-value/31047856#31047856\">this 考虑到我在这个答案。请注意,对于一个不太具体情况这个问题的国家,答案可能是许多不同的情况下非常有用。

我从左边的点移动一筹到右侧点,多写原创和新坐标:

所以这是失败的:

 公共无效onTouch(查看视图){
    INT [] = AUX新INT [2];    //获取芯片
    查看movingChip = findViewById(R.id.c1);
    //写它的坐标
    movingChip.getLocationOnScreen(辅助);
    ((的TextView)findViewById(R.id.p1t1))的setText((+ AUX [0] +,+ AUX [1] +))。
    //移动它
    ((的LinearLayout)findViewById(R.id.p1))removeView(movingChip)。
    ((的LinearLayout)findViewById(R.id.p2))addView(movingChip)。
    movingChip.requestLayout(); // ####添加这并没有解决这个问题    //写它的坐标
    movingChip.getLocationOnScreen(辅助);
    ((的TextView)findViewById(R.id.p2t4))的setText((+ AUX [0] +,+ AUX [1] +))。
}

当我获得第二次的坐标,给我的看法的坐标,就好像它是是在同一个地方它是在旧的定位在新的父

所以,新的上,左,下,右等等不被在这一点上计算。
另一方面,我的意见是正常显示,所以这种工作是在某一时刻得到完成。我怎么可能会迫使该计算发生?

我需要做的是这样的,因为我会想触发一个过渡动画


解决方案

根据Dalija的建议的解决方案

 公共类ChipView扩展了ImageView的{
    私人诠释[] = currentLocation新INT [2];
    / * .....
      * 等等等等等等
      * ..... * /    公共无效的init(){
        this.addOnLayoutChangeListener(新OnLayoutChangeListener(){
            @覆盖
            公共无效onLayoutChange(视图V,诠释离开,诠释顶部,右诠释,诠释下,诠释oldLeft,诠释oldTop,诠释oldRight,诠释oldBottom){
                INT [] newLocation =新INT [2];
                v.getLocationOnScreen(newLocation);
                Log.d(新位置:,(+ newLocation [0] +,+ newLocation [1] +));
                / **做任何需要与新老地点** /
                currentLocation = newLocation;
            }
        });
    }

所以,的init()从构造函数调用;现在我会尽力在那里执行动画,但是这是一个不同的tale.I希望它会工作。

It has been some days since I have this problem.

This is a reformulation of this question considering the behavior I describe on this answer. Please, note that this question states for a less specific case, and the answer could be useful for many different scenarios.

I'm moving one chip from the left point to the right point, and writing original and new coordinates:

So this is failing:

public void onTouch(View view) {
    int[] aux = new int[2];

    //Get the chip
    View movingChip = findViewById(R.id.c1);
    //Write it's coordinates
    movingChip.getLocationOnScreen(aux);
    ((TextView)findViewById(R.id.p1t1)).setText("(" + aux[0] + "," + aux[1] + ")");
    //Move it
    ((LinearLayout)findViewById(R.id.p1)).removeView(movingChip);
    ((LinearLayout)findViewById(R.id.p2)).addView(movingChip);
    movingChip.requestLayout();//#### Adding this didn't solve it

    //Write it's coordinates
    movingChip.getLocationOnScreen(aux);
    ((TextView)findViewById(R.id.p2t4)).setText("(" + aux[0] + "," + aux[1] + ")");
}

When I get the coordinates for the second time, I get the coordinates of the view as if it was positioned in the new parent at the same place it was in the old one

So, new top, left, bottom, right etc... are not being calculated at this point. On the other hand, my views are displayed properly, so this job is getting done at some point. How could I force this calculation to happen ?

I need to do it this way, because I'll want to trigger a transition animation

解决方案

Solution based on Dalija's suggestion

public class ChipView extends ImageView {
    private int[] currentLocation = new int[2];
    /*.....
      * blah,blah,blah
      * .....*/

    public void init() {
        this.addOnLayoutChangeListener(new OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                int[] newLocation = new int[2];
                v.getLocationOnScreen(newLocation);
                Log.d("New location:", "("+newLocation[0]+","+newLocation[1]+")");
                /** Do whatever is needed with old and new locations **/
                currentLocation = newLocation;
            }
        });
    }

So, init() is called from the constructor; now I'll try to perform the animation from there, but this is a different tale.I hope it will work.

这篇关于如何强制视图重新计算它的位置改变后父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆