How to get the position of an item when dropped in a Gridview

你离开我真会死。 提交于 2020-02-02 15:48:31

问题


I'm trying to know how to get the position of an item when it is already been dropped. I have used a gridview to list images.

my xml code is:

<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>

here is my mainactivity class:

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;
    }
}

I guessed that i can get the position when the action is DragEvent.ACTION_DROP but I cannot come up with the way to get the position!

Any ideas?


回答1:


Use getPositionForView (View view) to get position of current dragged item.




回答2:


View.callOnClick() is the answer

Assuming you have already added OnDragListener to each item of the GridView.

STEP 1: Invoke callOnClick() method on the GridView item in 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;
}

STEP 2: Add an onTouchListner on each item of the gridView and handle the event

@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(); 

    });
}


来源:https://stackoverflow.com/questions/22500972/how-to-get-the-position-of-an-item-when-dropped-in-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!