【发布时间】:2016-01-03 09:34:50
【问题描述】:
这是我正在尝试做的图片:
Image: Drag-and-drop app with a list and an item outside the list
我正在尝试制作一个类似于资源管理器的文件浏览器,通过拖放来移动文件,但我遇到了问题。
我知道有一个特殊的 RecyclerView 拖放界面(例如 this),但我一直无法找到说明如何在内部和 之间移动东西的示例列表之外的。
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="net.wbord.recyclerviewdragdropautoscrolltest.MainActivity">
<TextView
android:id="@+id/exampleItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Hello World!"/>
<Space
android:layout_width="match_parent"
android:layout_height="50dp"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/mainList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="100dp"
android:paddingRight="100dp"
android:clipToPadding="false"
/>
</LinearLayout>
还有 Java:
package net.wbord.recyclerviewdragdropautoscrolltest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
// Find Views:
final TextView exampleItem = (TextView) findViewById (R.id.exampleItem);
final RecyclerView rv = (RecyclerView) findViewById (R.id.mainList);
// Define Drag Listener:
final View.OnDragListener onDrag = new View.OnDragListener () {
@Override
public boolean onDrag (View v, DragEvent event) {
switch (event.getAction ()) {
case DragEvent.ACTION_DRAG_ENTERED:
v.setScaleX (1.5f); v.setScaleY (1.5f);
handleScroll (rv, v);
break;
case DragEvent.ACTION_DRAG_EXITED:
case DragEvent.ACTION_DRAG_ENDED:
v.setScaleX (1); v.setScaleY (1);
break;
case DragEvent.ACTION_DROP:
ClipData data = event.getClipData ();
String folder = (String) v.getTag ();
String msg = "File '" + data.getItemAt (0).getText () + "' " +
"moved into folder '" + folder + "'";
Toast.makeText (MainActivity.this, msg, Toast.LENGTH_LONG).show ();
break;
}
return true;
}
};
// The "file" for the user to drag-drop into a folder:
exampleItem.setOnLongClickListener (new View.OnLongClickListener () {
@Override
public boolean onLongClick (View v) {
// Start drag:
ClipData.Item item = new ClipData.Item (exampleItem.getText ());
ClipData data = new ClipData (exampleItem.getText (),
new String [] {ClipDescription.MIMETYPE_TEXT_PLAIN},
item);
View.DragShadowBuilder builder = new View.DragShadowBuilder (exampleItem);
v.startDrag (data, builder, null, 0);
return true;
}
});
// The list of "folders" that can accept the file:
rv.setLayoutManager (new LinearLayoutManager (this, LinearLayoutManager.VERTICAL, false));
rv.setAdapter (new RecyclerView.Adapter () {
class ViewHolder extends RecyclerView.ViewHolder {
private final TextView vItem;
public ViewHolder (TextView textView) {
super (textView);
vItem = textView;
}
public void bind (String itemName) {
vItem.setText (itemName);
vItem.setTag (itemName);
vItem.setOnDragListener (onDrag);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
TextView vText = new TextView (MainActivity.this);
vText.setTextSize (50);
return new ViewHolder (vText);
}
@Override
public void onBindViewHolder (RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder)
((ViewHolder) holder).bind (getItem (position));
}
@Override
public int getItemCount () {
return 100;
}
public String getItem (int position) {
return "Folder " + (1 + position);
}
});
}
protected void handleScroll (RecyclerView vList, View viewHoveredOver) {
LinearLayoutManager mgr = (LinearLayoutManager) vList.getLayoutManager ();
int iFirst = mgr.findFirstCompletelyVisibleItemPosition ();
int iLast = mgr.findLastCompletelyVisibleItemPosition ();
// Auto-Scroll:
if (mgr.findViewByPosition (iFirst) == viewHoveredOver)
vList.smoothScrollToPosition (Math.max (iFirst - 1, 0));
else if (mgr.findViewByPosition (iLast) == viewHoveredOver)
vList.smoothScrollToPosition (Math.min (iLast + 1,
mgr.getChildCount ()));
}
}
基本上每次“文件夹”被 ACTION_DRAG_ENTER 编辑时,都会调用 handleScroll() 方法:它检查拖动的文件悬停在哪个文件夹上,并使用它来滚动 RecyclerView。
问题是RecyclerView的回收机制:据我了解,回收池中的视图不是ACTION_DRAG_STARTED,所以自动滚动到视图中的视图不能接收文件,也不能自动滚动列表进一步。
RecyclerView 与其外部之间的拖放是如何工作的?带自动滚动?
有没有办法在拖动开始后将新视图添加到拖动中?
谢谢。
【问题讨论】:
-
我已经制作了一个帮助类,以便在回收站视图中实现拖放更容易stackoverflow.com/questions/42486167/…
标签: android drag-and-drop android-recyclerview