我对android没有什么经验。
我正在尝试制作一个拖放感知列表视图,因为其他各种解决方案都不能满足客户的需求,而且这个项目可能会让我做出很多其他“有趣”的拖放操作,比如这有一段时间。
要求很简单:用户可以使用 android 拖放 API(如果你这么称呼它)将一个东西从列表中拖出并拖到其他东西中。只要所说的东西不在列表视图中或拦截长按或其他任何东西,它就可以工作。
到目前为止,这是行不通的:
package blah;
import android.os.*;
import android.app.*;
import android.content.*;
import android.util.*;
import android.view.*;
import android.view.View*;
import android.widget.*;
public class ListFragment extends Fragment
{
public static class ItemView extends TextView
{
public int position;
public ItemView(Context context, AttributeSet attrs)
{
super(context,attrs);
}
}
private class _Adapter extends BaseAdapter
{
private Activity _activity;
@Override
public int getCount()
{
// TEST: Make it go
return 16;
}
@Override
public Object getItem(int arg0)
{
return null;
}
@Override
public long getItemId(int arg0)
{
return 0;
}
@Override
public View getView(int position, View convert, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)this._activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE
);
ItemView itemView = (ItemView)inflater.inflate(R.layout.list_item_view,parent,false);
// TEST: Just display positions
itemView.setText(Integer.toString(position));
itemView.setOnClickListener(new _ItemClickListener());
itemView.setOnDragListener(new _ItemDragListener());
itemView.position = position;
return itemView;
};
public _Adapter(Activity activity)
{
this._activity = activity;
}
}
private class _ItemDragListener implements OnDragListener
{
@Override
public boolean onDrag(View view, DragEvent event)
{
ItemView targetItemView = (ItemView)view;
int action = event.getAction();
switch(action){
case DragEvent.ACTION_DROP:
{
// not really sure which is which here, since nothing works
ItemView sourceItemView = (ItemView)event.getLocalState();
Log.d("DERP",
"DROPPED " + Integer.toString(sourceItemView.position) +
" ONTO " + Integer.toString(targetItemView.position)
);
}break;
}
return true;
}
}
private class _ItemClickListener implements OnClickListener
{
@Override
public void onClick(View view)
{
ItemView itemView = (ItemView)view;
Log.d("DERP", Integer.toString(itemView.position) + " WAS CLICKED");
// nothing happens here
view.startDrag(
ClipData.newPlainText("",""),
new View.DragShadowBuilder(view),
view,
0
);
}
public _ItemClickListener()
{
}
}
private Activity _activity;
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
this._activity = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ListView listView = (ListView)inflater.inflate(R.layout.list_fragment, container, false);
listView.setAdapter(new _Adapter(this._activity));
return listView;
}
}
我的直觉告诉我,考虑到 android UI 支持的功能的“广度”,制作拖放列表应该比这更简单。但是,这也是 Java,所以我不期待直接的答案。