Java Android-检索并使用片段中的位置视图
我有一个有4个按钮的片段。我想将每个按钮在屏幕上的位置保存在一个哈希映射中(每个按钮都将映射到它在屏幕上的位置)。最简单的方法是什么? 在使用片段之前,我使用了onWindowFocusChanged方法(在活动中),在该方法中我可以检索按钮位置。对于片段,我尝试使用ViewTreeObserver(带有GlobalLayoutListener)。在匿名类内部,我可以检索并保存(在实例变量中)按钮位置,但在它外部,这些值消失了(等于0)。 怎样才能做到呢 谢谢 编辑:添加了一个代码段-Java Android-检索并使用片段中的位置视图,java,android,android-fragments,Java,Android,Android Fragments,我有一个有4个按钮的片段。我想将每个按钮在屏幕上的位置保存在一个哈希映射中(每个按钮都将映射到它在屏幕上的位置)。最简单的方法是什么? 在使用片段之前,我使用了onWindowFocusChanged方法(在活动中),在该方法中我可以检索按钮位置。对于片段,我尝试使用ViewTreeObserver(带有GlobalLayoutListener)。在匿名类内部,我可以检索并保存(在实例变量中)按钮位置,但在它外部,这些值消失了(等于0)。 怎样才能做到呢 谢谢 编辑:添加了一个代码段- publ
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()));
}
从其中调用getViewsPosition
编辑#2:
我会尽力澄清我的意图。我正在用fragment构建应用程序。假设每个片段有4个按钮。我想让这些按钮听触摸事件,这样当用户触摸一个按钮并将手指移动到另一个按钮(不抬起手指)时,就会有按钮名称的音频反馈。为此,我需要所有按钮的位置,以便能够识别从一个按钮移动到另一个按钮(在onTouch方法中)。为此,我需要一个实例变量,它将保持屏幕上所有按钮的位置,我将能够在onTouch中使用它。您建议的解决方案无法解决问题,因为
public void getViewsPosition(最终ArrayList viewArr)
视图的大小仍然是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>();
3) getViewsPosition(…)和OnPositionUpdate()方法-
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() {
}
public void getViewsPosition(最终数组列表viewArr){
用于(最终视图v:viewArr){
final ViewTreeObserver vto=v.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新的OnGlobalLayoutListener(){
@凌驾
公共图书馆{
int[]i=新的int[2];
v、 getLocationOnScreen(i);
_rect=newrect(i[0],i[1],i[0]+v.getWidth(),i[1]
+v.getHeight());
如果(Build.VERSION.SDK_INT<16){
vto.移除LobalonlayOutliner(本);
}否则{
vto.removeOnGlobalLayoutListener(此);
}
onPositionUpdate();
}
});
}
}
私有void onPositionUpdate(){
}
onViewCreated(…)中的第一个日志是“视图列表的大小=7”
我的问题是如何打印第二个日志“视图大小到rect=7”?它总是返回0
我可能真的不知道如何使用onPositionUpdate()方法的解决方案。如何实现我的目标?只有在呈现视图时才会通知ViewTreeObserver, 你这是什么意思 “但除此之外,这些价值观都消失了” 您只需获取GlobalLayoutListener回调中按钮的位置,并将其保存到实例中。记住,您只能获取渲染的位置。 提醒: 把这个密码 Log.i(标记,“视图到矩形的大小=”+String.valueOf(视图到矩形的大小()) 在公共void 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
}
原因:
只有调用了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
}
public void onGlobalLayout(){
int[]i=新的int[2];
v、 getLocationOnScreen(i);
_rect=newrect(i[0],i[1],i[0]+v.getWidth(),i[1]
+v.getHeight());
视图到正确放置(v,正确);
如果(Build.VERSION.SDK_INT<16){
vto.移除LobalonlayOutliner(本);
}否则{
vto.removeOnGlobalLayoutListener(此);
}
onPositionUpdate();
}
私有void onPositionUpdate()
{
//用职位做你的工作
}
我想在所有这些按钮上循环,并将它们的位置保存在某个集合中。我希望能够在类的其他方法中使用此集合,但其(集合)大小始终为0。我该怎么做?你是在什么时候使用这些收藏品的?在调用GlobalLayoutListener回调之前,您将得到零。发布一些代码片段来演示如何使用集合?kevinhoo-没错。但是-我想从其他类方法访问此集合。在那里,我的尺寸也为0。是否有可能访问此方法之外的视图,并获得其所需大小(在本例中,大小假定为7)?这是关于计时的。在渲染视图层次之前,没有其他方法可以知道确切的位置。您可以调用自定义侦听器的某些方法,在获得位置后通知外部。
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
}