如何在Gridview中删除项目的位置

时间:2014-03-19 09:14:10

标签: android android-gridview

我正在尝试知道如何在物品掉落时获取物品的位置。我使用gridview列出图像。

我的xml代码是:

<RelativeLayout xmlns:android="..."
    xmlns: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_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);
        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
            // get the position where the item is been dropped

                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时获得该位置,但我无法想出获得该位置的方法!

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

使用getPositionForView(视图视图)获取当前拖动项目的位置。

答案 1 :(得分:0)

View.callOnClick()是答案

假设您已经将OnDragListener添加到GridView的每个项目中。

  

步骤1:callOnClick()的GridView项上调用case DragEvent.ACTION_DROP:方法

@Override
public boolean onDrag(View view, DragEvent dragEvent) {

    switch (dragEvent.getAction()) {

    case DragEvent.ACTION_DRAG_STARTED:

    ...


    case DragEvent.ACTION_DROP:
        view.callOnClick();           //<----- This is what i was talking
        return true;


   ....

     }
    return false;
}
  

步骤2:在gridView的每个项目上添加一个onTouchListner并处理事件

@Override
public void onBindViewHolder(@NonNull RecycleViewHolder viewHolder, int i) {

    viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

            Toast.makeText(context,"Click happened"+ i, Toast.LENGTH_LONG).show(); 

    });
}