在Android中的地位下降的GridView麻烦交换项目 [英] swap items in a Gridview trouble with the dropped position in Android

查看:197
本文介绍了在Android中的地位下降的GridView麻烦交换项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图交换在网格视图项目,这是我的了:

i have been trying to swap items in a Grid view, and this is where i got:

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:horizontalSpacing="10dip"
        android:numColumns="4"
        android:verticalSpacing="10dip" />

</RelativeLayout>

主要活动类:

public class MainActivity extends Activity implements OnDragListener,
        OnItemLongClickListener {

    ArrayList drawables;

    GridView gridView;
    private BaseAdapter adapter;
    private int draggedIndex = -1;
    private int droppedIndex = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawables = new ArrayList();
        drawables.add(R.drawable.ic_launcher);
        drawables.add(R.drawable.ic_launcher1);
        drawables.add(R.drawable.ic_launcher2);
        drawables.add(R.drawable.ic_launcher);
        drawables.add(R.drawable.ic_launcher);
        drawables.add(R.drawable.ic_launcher);
        drawables.add(R.drawable.ic_launcher);
        drawables.add(R.drawable.ic_launcher);
        gridView = (GridView) findViewById(R.id.grid_view);
        gridView.setOnItemLongClickListener(MainActivity.this);
        gridView.setAdapter(adapter = new BaseAdapter() {

            @Override
            // Get a View that displays the data at the specified position in
            // the data set.
            public View getView(int position, View convertView,
                    ViewGroup gridView) {
                // try to reuse the views.
                ImageView view = (ImageView) convertView;
                // if convert view is null then create a new instance else reuse
                // it
                if (view == null) {
                    view = new ImageView(MainActivity.this);
                }
                view.setImageResource((Integer) drawables.get(position));
                view.setTag(String.valueOf(position));
                return view;
            }

            @Override
            // Get the row id associated with the specified position in the
            // list.
            public long getItemId(int position) {
                return position;
            }

            @Override
            // Get the data item associated with the specified position in the
            // data set.
            public Object getItem(int position) {
                return drawables.get(position);
            }

            @Override
            // How many items are in the data set represented by this Adapter.
            public int getCount() {
                return drawables.size();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        switch (dragEvent.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            // Ignore this event
             return true;
        case DragEvent.ACTION_DRAG_ENTERED:
            // Ignore this event
            return true;
        case DragEvent.ACTION_DRAG_EXITED:
            // Ignore this event
            return true;
        case DragEvent.ACTION_DRAG_LOCATION:
            // Ignore this event
            return true;
        case DragEvent.ACTION_DROP:
            // Dropped inside a new view\
                adapter.notifyDataSetChanged();
                ImageView v2 = (ImageView)view.getParent();
                final int position1 = gridView.getPositionForView(v2);
                if (position1 >= 0) 
                { 
                    final long droppedIndex = gridView.getAdapter().getItemId(position1); 
                }
                Object item1 = gridView.getAdapter().getItem(draggedIndex);
                Object item2 = gridView.getAdapter().getItem(droppedIndex);
                drawables.remove(draggedIndex);
                drawables.remove(droppedIndex);
                drawables.add(droppedIndex,item1);
                drawables.add(draggedIndex,item2);
                draggedIndex = -1;
                droppedIndex = -1;
                adapter.notifyDataSetChanged();
        case DragEvent.ACTION_DRAG_ENDED:
           //
            view.setOnDragListener(null);
            return true;

         }
        return false;
    }

    @Override
    public boolean onItemLongClick(AdapterView gridView, View view,
            int position, long row) {
        ClipData.Item item = new ClipData.Item((String) view.getTag());
        ClipData clipData = new ClipData((CharSequence) view.getTag(),
                new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }, item);
        view.startDrag(clipData, new View.DragShadowBuilder(view), null, 0);
        view.setVisibility(View.INVISIBLE);
        draggedIndex = position;
        return true;
    }
}

我的问题是DragEvent.ACTION_DROP。我是这样的:
我拖一个项目,当在另一个地方砸,该项目将消失。这就是一切。

my problem is in DragEvent.ACTION_DROP. I works like this: I drag one item and, when drop it in another place, the item disappears. And that is all.

据推测,首先检索两个位置:该项目的位置拖(draggedIndex)并在该项目被丢弃的位置(droppedIndex)。后,我删除这两个项目,并在相对的位置​​再次把它们添加到阵列(项目拖动进入droppedIndex和其他转到draggedIndex,因此它们被交换/ swaped)

Supposedly, first retrieve both positions: the position of the item dragged (draggedIndex) and the position where the item is dropped (droppedIndex). After, i remove both items and add them to the array again in the opposite positions (the item dragged goes to droppedIndex and the other goes to the draggedIndex, so they are exchanged/swaped)

我不知道它是做一个很好的方式,或者,如果我做了尝试检索被丢弃的位置(droppedIndex)任何错误。
任何想法?

I wonder if it is a good way to do this, or if i made any mistakes trying to retrieve the dropped position (droppedIndex). any ideas?

推荐答案

我参加了一个略微不同的方法来获取下降指标。对于ACTION_DROP事件的getX()和的getY()返回在下降的时刻阻力点的X和Y位置,用坐标所收到的下降(即GridView控件)查看系统。

I took a slightly different approach to get the dropped index. For an ACTION_DROP event, getX() and getY() return the X and Y position of the drag point at the moment of the drop, using the coordinate system of the View that received the drop (i.e. the gridview).

float dropX = event.getX();
float dropY = event.getY();

一旦你的X和Y坐标,你可以计算出你的网格视图相应的行和列,然后使用类似下面来获取对象的索引数据数组(我有一个4× 4格):

Once you have the x and y co-ordinates you can calculate the corresponding row and column in your grid view, and then use something like the following to get the index of the object in your data array (I have a 4 x 4 grid):

index = row * 4 + column;

最后,我用一个数组列表(targetDrawables)抱到我的GridView内容的引用,所以要重新排序使用:

Finally, I'm using an array list (targetDrawables) to hold the references to my gridview contents, so to reorder use:

targetDrawables.remove(draggedIndex);
targetDrawables.add(droppedIndex, draggedContents);

//  Invalidate the view to force a redraw of the Grid View

imageAdapter.notifyDataSetChanged();

这篇关于在Android中的地位下降的GridView麻烦交换项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆