0

我有一个带有 4 个按钮的片段。我想将每个按钮在屏幕上的位置保存在哈希图中(每个按钮都将映射到它在屏幕上的位置)。最简单的方法是什么?在使用片段之前,我使用了 onWindowFocusChanged 方法(在 Activity 中),我可以在其中检索按钮位置。对于片段,我尝试使用 ViewTreeObserver(使用 GlobalLayoutListener)。在匿名类中,我可以检索并保存(在实例变量中)按钮位置,但在它之外,这些值已经消失(等于 0)。怎么做到呢?

谢谢!

编辑:添加了一个代码片段 -

public void getViewsPosition(final ArrayList<View> viewArr) {
    for (final View v : viewArr) {
        final ViewTreeObserver vto = v.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int[] i = new int[2];
                v.getLocationOnScreen(i);
                _rect = new Rect(i[0], i[1], i[0] + v.getWidth(), i[1]
                        + v.getHeight());
                view_to_rect.put(v, _rect);
                if (Build.VERSION.SDK_INT < 16) {
                    vto.removeGlobalOnLayoutListener(this);
                } else {
                    vto.removeOnGlobalLayoutListener(this);
                }
            }
        });
    }
    Log.i(TAG, "size of view to rect = " + String.valueOf(view_to_rect.size()));
}

其中 viewArr 是我在此布局中的所有视图的数组列表;view_to_rect 是一个实例变量,声明为:

私有静态地图 view_to_rect = new HashMap();

日志始终显示“矩形视图大小 = 0”。

还附上了onViewCreated的实现:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.i(TAG, "onViewCreated");
    final ViewGroup _mainView = (ViewGroup) getView().findViewById(
            R.id.fragment01);
    getAllViews(viewsList, _mainView);
    Log.i(TAG, "size of view list = " + String.valueOf(viewsList.size()));
    getViewsPosition(viewsList);
}

从哪里调用 getViewsPosition。

编辑#2: 我会尽量澄清我的意图。我正在用片段构建应用程序。比方说,每个片段都有 4 个按钮。我希望这些按钮能够监听触摸事件,以便用户触摸一个按钮并将他的手指移动到另一个按钮(不抬起手指)会有按钮名称的音频反馈。为此,我需要拥有所有按钮的位置,以便能够识别从一个按钮移动到另一个按钮(在 onTouch 方法中)。为此,我需要一个实例变量,它将所有按钮位置保留在屏幕上,并且我将能够在 onTouch 中使用它。您建议的解决方案不能解决问题,因为在

公共无效getViewsPosition(最终ArrayList viewArr)

view_to_rect 的大小仍然为 0。

编辑#3:

这是我尝试构建我的收藏的过程:

1)类签名+集合和数组定义:

public class Fragment01 extends Fragment implements OnTouchListener,
    OnClickListener{
        private Map<View, Rect> view_to_rect = new HashMap<View, Rect>();
        ArrayList<View> viewsList = new ArrayList<View>();

2) onViewCreated 实现:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.i(TAG, "onViewCreated");
    final ViewGroup _mainView = (ViewGroup) getView().findViewById(
            R.id.fragment01);
    getAllViews(viewsList, _mainView);
    Log.i(TAG, "size of view list = " + String.valueOf(viewsList.size()));
    getViewsPosition(viewsList);
    Log.i(TAG, "size of view to rect = " + String.valueOf(view_to_rect.size()));
}

3) getViewsPosition(...) 和 onPositioUpdate() 方法 -

public void getViewsPosition(final ArrayList<View> viewArr) {
    for (final View v : viewArr) {
        final ViewTreeObserver vto = v.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int[] i = new int[2];
                v.getLocationOnScreen(i);
                _rect = new Rect(i[0], i[1], i[0] + v.getWidth(), i[1]
                        + v.getHeight());
                if (Build.VERSION.SDK_INT < 16) {
                    vto.removeGlobalOnLayoutListener(this);
                } else {
                    vto.removeOnGlobalLayoutListener(this);
                }

                onPositionUpdate();
            }
        });
    }
}

private void onPositionUpdate() {
}

onViewCreated(...) 中的第一个登录是“视图列表的大小 = 7”。

我的问题是如何让第二个日志打印出“视图大小为 rect = 7”?它总是返回 0。

我可能并不真正了解如何将您的解决方案与 onPositionUpdate() 方法一起使用。应该如何实施以实现我的目标?

4

1 回答 1

1

ViewTreeObserver 只会在视图被渲染时被通知,你是什么意思

“但在它之外,这些价值已经消失了”

您可以在 GlobalLayoutListener 回调中获取按钮的位置,并将其保存为实例。请记住,您只能在渲染时获得位置。

提醒:

把这段代码

Log.i(TAG, "矩形视图大小 = " + String.valueOf(view_to_rect.size()));

在 public void onGlobalLayout() {} 内,您将得到非零值。

为什么:

只有 onGlobalLayout 方法被调用,可以得到非零。

         public void onGlobalLayout() {
                int[] i = new int[2];
                v.getLocationOnScreen(i);
                _rect = new Rect(i[0], i[1], i[0] + v.getWidth(), i[1]
                        + v.getHeight());
                view_to_rect.put(v, _rect);
                if (Build.VERSION.SDK_INT < 16) {
                    vto.removeGlobalOnLayoutListener(this);
                } else {
                    vto.removeOnGlobalLayoutListener(this);
                }

                onPositionUpdate();

            }

private void onPositionUpdate()
{
    //do your work with positions
}
于 2013-08-15T08:25:15.647 回答