我有一个应用程序,它一直运作良好。但是,在我将手机升级到Android 7.0后,拖放功能被破坏了。如果我进一步将手机升级到Android 7.1,它再次运行良好。
然后我创建了一个测试应用程序以找出原因,但我只能“证明”它与RelativeLayout中嵌套的EditText有关。
以下是显示日志的代码:
private class EntryHolder{
View mEntryLayout;
View mEntryContainer;
}
void addView() {
EntryHolder eh = new EntryHolder();
eh.mEntryLayout = getLayoutInflater()
.inflate(R.layout.edit_item, mContainer, false);
eh.mEntryContainer = eh.mEntryLayout.findViewById(R.id.field_container);
eh.mEntryContainer.setTag(eh);
eh.mEntryLayout.setTag(eh);
mContainer.addView(eh.mEntryLayout, mIndex++);
eh.mEntryContainer.setOnLongClickListener(this);
eh.mEntryLayout.setOnDragListener(mDragListener);
}
private class DragListener implements View.OnDragListener {
String[] ACTIONS = {"---", "STARTED ", "LOCATION ", "DROP ",
"ENDED ", "ENTERED ", "EXITED "};
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
int action = dragEvent.getAction();
Log.d("Drag&Drop", ACTIONS[action]+view.toString());
switch(action) {
case DragEvent.ACTION_DRAG_STARTED:
case DragEvent.ACTION_DRAG_ENTERED:
case DragEvent.ACTION_DRAG_ENDED:
case DragEvent.ACTION_DRAG_LOCATION:
case DragEvent.ACTION_DROP:
return true;
}
return false;
}
@Override
public boolean onLongClick(View view) {
ClipData not_used_clip = ClipData.newPlainText("", "");
EntryHolder eh = (EntryHolder)view.getTag();
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
eh.mEntryLayout.startDrag(not_used_clip, new View.DragShadowBuilder(eh.mEntryLayout),
eh.mEntryLayout, 0);
}
else {
eh.mEntryLayout.startDragAndDrop(not_used_clip, new View.DragShadowBuilder(eh.mEntryLayout),
eh.mEntryLayout, 0);
}
view.setAlpha(0.0f);
return true;
}
在测试活动的onCreateView方法中,addView被调用3次以动态添加3个frame_layout。
如果我以这种方式放置frame_layout xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:elevation="8dp">
<RelativeLayout android:id="@+id/field_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:longClickable="true"
android:background="@android:color/holo_blue_bright">
<LinearLayout android:id="@+id/line1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="36dp">
<!--Something is supposed to be here-->
</LinearLayout>
<EditText
android:id="@+id/field_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Value"
android:layout_below="@id/line1"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>
</RelativeLayout>
</FrameLayout>
logcat中的输出只是Android API 24中的以下内容,
...D/Drag&Drop: STARTED android.widget.FrameLayout{63144b4 ...
...D/Drag&Drop: STARTED android.widget.FrameLayout{231eb23 ...
...D/Drag&Drop: STARTED android.widget.FrameLayout{d141b9e ...
...D/Drag&Drop: ENDED android.widget.FrameLayout{63144b4 ...
...D/Drag&Drop: ENDED android.widget.FrameLayout{d141b9e ...
...D/Drag&Drop: ENDED android.widget.FrameLayout{231eb23 ...
在其他版本(包括API 23-和API 25)中,使用ENTERED,LOCATION和EXITED
输出更多如果我删除了中间的RelativeLayout或者我删除了EditText,那么它在API 24中也没问题。但是我在实际的应用程序中需要这种级别的布局,所以我不能这样解决。
有人对这个问题有任何建议吗?谢谢!