如何水平拖动视图?
在 ListView 和 Gallery 等控件中,您可以沿垂直或水平方向拖动项目。
我有一个 TextView,我成功拖动了它。但我想将其水平移动。我怎样才能实现它?
谢谢,
我的代码:
public class MyDragShadowBuilder extends View.DragShadowBuilder {
private static View view;
public MyDragShadowBuilder(View v) {
super(v);
view = v;
}
@Override
public void onProvideShadowMetrics(Point size, Point touch) {
width = getView().getWidth();
height = getView().getHeight();
size.set(width, height);
touch.set(width / 2, height / 2);
}
private int width, height;
@Override
public void onDrawShadow(Canvas canvas) {
view.draw(canvas);
}
}
在我的活动中:
tv.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData data = ClipData.newPlainText("dot",
"Dot : " + v.toString());
View.DragShadowBuilder myShadow = new MyDragShadowBuilder(v);
v.startDrag(data, myShadow,(Object) v, 0);
return false;
}
});
我想模拟一个画廊。我有 10000 张照片可以用它来展示。我的画廊可以显示 10 张照片。我想水平拖动包含画廊内容的画廊的 LinearLayout,当 LinearLayout 像 SlidingDrawer 一样到达窗口末尾时,更改其内容并显示接下来的 10 张照片。 SlidingDrawer 是一个很好的解决方案,但它有一些局限性。当您将整个 LinearLayout 设置为其句柄时,它无法被单击。当你为其设置单独的句柄时,你无法确定句柄相对于内容的位置。
In controls like ListView and Gallery you can drag items in one direction vertical or horizontal.
I have a TextView that I drag it successfully. But I want to move it in horizontal direction. how can i implement it?
Thanks,
my code:
public class MyDragShadowBuilder extends View.DragShadowBuilder {
private static View view;
public MyDragShadowBuilder(View v) {
super(v);
view = v;
}
@Override
public void onProvideShadowMetrics(Point size, Point touch) {
width = getView().getWidth();
height = getView().getHeight();
size.set(width, height);
touch.set(width / 2, height / 2);
}
private int width, height;
@Override
public void onDrawShadow(Canvas canvas) {
view.draw(canvas);
}
}
in my activity:
tv.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData data = ClipData.newPlainText("dot",
"Dot : " + v.toString());
View.DragShadowBuilder myShadow = new MyDragShadowBuilder(v);
v.startDrag(data, myShadow,(Object) v, 0);
return false;
}
});
I want to simulate a Gallery. I have 10000 photos to show by it. My gallery can show 10 photos. I want to drag the LinearLayout of my gallery which is containing the gallery's content horizontally and when the LinearLayout reached to the end of window like SlidingDrawer, change its content and show the next 10 photos. SlidingDrawer is a good solution but it has some limitations. When you set your whole LinearLayout as its handle it can not be clicked. When you set a separated handle for it, you can not determine the position of handle relative to the content.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 Bluefalcon 的建议开始,我想出了这样的建议:
......
Starting with Bluefalcon's advice, I came up with something like:
...
水平滚动视图是 android 中最简单的水平拖动选项...
水平滚动视图
尝试一下....在指定的范围内,给出文本视图或您喜欢使用的任何布局或小部件...甚至对于动态过程它也可以工作...
Horizontal Scroll view is the simplest horizontal drag option in android...
HORIZONTAL SCROLL VIEW
Try that....and inside the specified,give the textview or any layouts or widgets you prefer to use...and it will work even for dynamic procedures...
如果您想滚动,请按照其余部分的建议使用水平滚动视图。
但如果您的意思是“拖放”中的拖动,那么您可以执行以下操作。
TextView
上设置触摸监听器,ACTION_DOWN
和ACTION_MOVE
事件之后的所有事件,x
和 <上述事件中的 code>y 位置setTop
和setLeft
并将它们设置为x
和y< /code> 你得到上面
invalidate
这应该将 UI 元素拖动到屏幕周围。
If you want to Scroll the use the Horizontal scroll view as suggested by the rest.
But if you mean drag as in "drag and drop" then you can do the following.
TextView
ACTION_DOWN
and eventsACTION_MOVE
x
andy
position from the above eventssetTop
andsetLeft
and set them tox
andy
you get aboveinvalidate
on the whole viewThis should drag the UI element around the screen.
HorizontalScroll 视图是完美的解决方案,请参阅此处的博客
http://android-coding.blogspot.com/2011/01 /scrollview-and-horizontalscrollview.html
HorizontalScroll view is perfect solution for this refer the blog here
http://android-coding.blogspot.com/2011/01/scrollview-and-horizontalscrollview.html
您可以使用水平滚动视图来模拟水平滑动画廊。
You can use horizontal scroll view to emulate a horizontal sliding gallery.