拖放列表视图

时间:2013-05-10 03:36:33

标签: android drag-and-drop android-listview

我对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,所以我不期待直截了当的答案。

3 个答案:

答案 0 :(得分:1)

Here is link拖放列表视图演示

在Android 4.0中,支持拖放视图。 Here is full demo与拖放列表视图的扩展

答案 1 :(得分:1)

从谷歌官方视频学习教程中试用此代码。 Google DEVByte

答案 2 :(得分:0)

有一个图书馆电话drag-sort listview。你应该看看它。