- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
简短版:
DragEvent
?有 How to register a DragEvent while already inside one and have it listen in the current DragEvent? ,但我真的想要一个更清洁的解决方案。
建议的 GONE->VISIBLE 解决方法要“正确”是相当复杂的,因为您需要确保仅在列表项变得可见时才使用它,而不是无条件地在所有当前 ListView 项上使用它。在此 hack 略有漏洞,没有更多的解决方法代码来使其正确。
长版:
我有一个 ListView
。 ListView
的元素是包含可拖动符号(小框)的自定义 View ,例如类似这样:
可以在 ListView
的项目之间拖动小框,就像将元素排序到框中一样。列表项上的拖动处理程序或多或少是微不足道的:
@Override
public boolean onDragEvent(DragEvent event)
{
if ((event.getLocalState() instanceof DragableSymbolView)) {
final DragableSymbolView draggedView = (DragableSymbolView) event.getLocalState();
if (draggedView.getTag() instanceof SymbolData) {
final SymbolData symbol = (SymbolData) draggedView.getTag();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DRAG_ENTERED:
setSelected(true);
return true;
case DragEvent.ACTION_DRAG_ENDED:
case DragEvent.ACTION_DRAG_EXITED:
setSelected(false);
return true;
case DragEvent.ACTION_DROP:
setSelected(false);
// [...] remove symbol from soruce box and add to current box
requestFocus();
break;
}
}
}
return super.onDragEvent(event);
}
将指针悬停在符号上并开始拖动(即将其移动到一个小阈值之外)时,拖动开始。
但是,现在屏幕尺寸可能不足以包含所有框,因此 ListView
需要滚动。我发现我需要自己实现滚动的困难方法,因为 ListView
在拖动时不会自动滚动。
进来ListViewScrollingDragListener
:
public class ListViewScrollingDragListener
implements View.OnDragListener {
private final ListView _listView;
public static final int DEFAULT_SCROLL_BUFFER_DIP = 96;
public static final int DEFAULT_SCROLL_DELTA_UP_DIP = 48;
public static final int DEFAULT_SCROLL_DELTA_DOWN_DIP = 48;
private int _scrollDeltaUp;
private int _scrollDeltaDown;
private boolean _doScroll = false;
private boolean _scrollActive = false;
private int _scrollDelta = 0;
private int _scrollDelay = 250;
private int _scrollInterval = 100;
private int _scrollBuffer;
private final Rect _visibleRect = new Rect();
private final Runnable _scrollHandler = new Runnable() {
@Override
public void run()
{
if (_doScroll && (_scrollDelta != 0) && _listView.canScrollVertically(_scrollDelta)) {
_scrollActive = true;
_listView.smoothScrollBy(_scrollDelta, _scrollInterval);
_listView.postDelayed(this, _scrollInterval);
} else {
_scrollActive = false;
}
}
};
public ListViewScrollingDragListener(final ListView listView, final boolean attach)
{
_scrollBuffer = UnitUtil.dipToPixels(listView, DEFAULT_SCROLL_BUFFER_DIP);
_scrollDeltaUp = -UnitUtil.dipToPixels(listView, DEFAULT_SCROLL_DELTA_UP_DIP);
_scrollDeltaDown = UnitUtil.dipToPixels(listView, DEFAULT_SCROLL_DELTA_DOWN_DIP);
_listView = listView;
if (attach) {
_listView.setOnDragListener(this);
}
}
public ListViewScrollingDragListener(final ListView listView)
{
this(listView, true);
}
protected void handleDragLocation(final float x, final float y)
{
_listView.getGlobalVisibleRect(_visibleRect);
if (_visibleRect.contains((int) x, (int) y)) {
if (y < _visibleRect.top + _scrollBuffer) {
_scrollDelta = _scrollDeltaUp;
_doScroll = true;
} else if (y > _visibleRect.bottom - _scrollBuffer) {
_scrollDelta = _scrollDeltaDown;
_doScroll = true;
} else {
_doScroll = false;
_scrollDelta = 0;
}
if ((_doScroll) && (!_scrollActive)) {
_scrollActive = true;
_listView.postDelayed(_scrollHandler, _scrollDelay);
}
}
}
public ListView getListView()
{
return _listView;
}
@Override
public boolean onDrag(View v, DragEvent event)
{
/* hide sequence controls during drag */
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_ENTERED:
_doScroll = true;
break;
case DragEvent.ACTION_DRAG_EXITED:
case DragEvent.ACTION_DRAG_ENDED:
case DragEvent.ACTION_DROP:
_doScroll = false;
break;
case DragEvent.ACTION_DRAG_LOCATION:
handleDragLocation(event.getX(), event.getY());
break;
}
return true;
}
}
当您在其可见区域的上边界或下边界附近拖动时,这基本上会滚动 ListView
。它并不完美,但已经足够好了。
但是,有一个问题:
当列表滚动到之前不可见的元素时,该元素不会接收到 DragEvent
。在其上拖动符号时,它不会被选中(突出显示),也不接受掉落。
关于如何使“卷入” View 从已经激活的拖放操作接收 DragEvent
有什么想法吗?
最佳答案
所以根本问题是 ViewGroup
(即 ListView
扩展)缓存了一个子级列表以通知 DragEvent
。此外,它仅在接收到 ACTION_DRAG_STARTED 时填充此缓存。有关更多详细信息,请阅读源代码 here .
关于解决方案!我们不是在 ListView
的各个行上监听放置事件,而是在 ListView
本身上监听它们。然后,根据事件的坐标,我们将找出被拖动的 View 从哪一行拖动到哪一行或悬停在哪一行。当删除发生时,我们将执行从前一行中删除并添加到新行的事务。
private void init(Context context) {
setAdapter(new RandomIconAdapter()); // Adapter that contains our data set
setOnDragListener(new ListDragListener());
mListViewScrollingDragListener = new ListViewScrollingDragListener(this, false);
}
ListViewScrollingDragListener mListViewScrollingDragListener;
private class ListDragListener implements OnDragListener {
// The view that our dragged view would be dropped on
private View mCurrentDropZoneView = null;
private int mDropStartRowIndex = -1;
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
// Update the active drop zone based on the position of the event
updateCurrentDropZoneView(event);
// Funnel drag events to separate listener to handle scrolling near edges
mListViewScrollingDragListener.onDrag(v, event);
if( mDropStartRowIndex == -1 ) // Only initialize once per drag->drop gesture
{
mDropStartRowIndex = indexOfChild(mCurrentDropZoneView) + getFirstVisiblePosition();
log("mDropStartRowIndex %d", mDropStartRowIndex);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
case DragEvent.ACTION_DRAG_EXITED:
mCurrentDropZoneView = null;
mDropStartRowIndex = -1;
break;
case DragEvent.ACTION_DROP:
// Update our data set based on the row that the dragged view was dropped in
int finalDropRow = indexOfChild(mCurrentDropZoneView) + getFirstVisiblePosition();
updateDataSetWithDrop(mDropStartRowIndex, finalDropRow);
// Let adapter update ui
((BaseAdapter)getAdapter()).notifyDataSetChanged();
break;
}
// The ListView handles ALL drag events all the time. Fine for now since we don't need to
// drag -> drop outside of the ListView.
return true;
}
private void updateDataSetWithDrop(int fromRow, int toRow) {
log("updateDataSetWithDrop fromRow %d and toRow %d", fromRow, toRow);
sIconsForListItems[fromRow]--;
sIconsForListItems[toRow]++;
}
// NOTE: The DragEvent in local to DragDropListView, as are children coordinates
private void updateCurrentDropZoneView(DragEvent event) {
View previousDropZoneView = mCurrentDropZoneView;
mCurrentDropZoneView = findFrontmostDroppableChildAt(event.getX(), event.getY());
log("mCurrentDropZoneView updated to %d for x/y : %f/%f with action %d",
mCurrentDropZoneView == null ? -1 : indexOfChild(mCurrentDropZoneView) + getFirstVisiblePosition(),
event.getX(), event.getY(), event.getAction());
if (mCurrentDropZoneView != previousDropZoneView) {
if (previousDropZoneView != null) previousDropZoneView.setSelected(false);
if (mCurrentDropZoneView != null) mCurrentDropZoneView.setSelected(true);
}
}
}
/**
* The next four methods are utility methods taken from Android Source Code. Most are package-private on View
* or ViewGroup so I'm forced to replicate them here. Original source can be found:
* http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/view/ViewGroup.java#ViewGroup.findFrontmostDroppableChildAt%28float%2Cfloat%2Candroid.graphics.PointF%29
*/
private View findFrontmostDroppableChildAt(float x, float y) {
int childCount = this.getChildCount();
for(int i=0; i<childCount; i++)
{
View child = getChildAt(i);
if (isTransformedTouchPointInView(x, y, child)) {
return child;
}
}
return null;
}
static public boolean isTransformedTouchPointInView(float x, float y, View child) {
PointF point = new PointF(x, y);
transformPointToViewLocal(point, child);
return pointInView(child, point.x, point.y);
}
static public void transformPointToViewLocal(PointF pointToModify, View child) {
pointToModify.x -= child.getLeft();
pointToModify.y -= child.getTop();
}
static public boolean pointInView(View v, float localX, float localY) {
return localX >= 0 && localX < (v.getRight() - v.getLeft())
&& localY >= 0 && localY < (v.getBottom() - v.getTop());
}
static final int[] sIconsForListItems;
static final int NUM_LIST_ITEMS = 50;
static final int MAX_NUM_ICON_PER_ELEMENT = 8;
static {
sIconsForListItems = new int[NUM_LIST_ITEMS];
for (int i=0; i < NUM_LIST_ITEMS; i++)
{
sIconsForListItems[i] = (getRand(MAX_NUM_ICON_PER_ELEMENT));
}
}
private static final String TAG = DragDropListView.class.getSimpleName();
private static void log(String format, Object... args) {
Log.d(TAG, String.format(format, args));
}
很多评论,希望代码是自文档化的。一些注意事项:
关于android - 如何使列表元素在滚动后获得 DragEvents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29825676/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!