放置在Gridview中时如何获取项目的位置 [英] How to get the position of an item when dropped in a Gridview

查看:80
本文介绍了放置在Gridview中时如何获取项目的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图知道如何在已删除项目时获取其位置.我已使用gridview列出图像.

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.

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

这是我的主要活动类别:

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

我猜想当动作为DragEvent.ACTION_DROP时我可以得到位置,但是我无法提出获得位置的方法!

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!

有什么想法吗?

推荐答案

View.callOnClick()是答案

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

View.callOnClick() is the answer

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

步骤1:case DragEvent.ACTION_DROP:

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

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

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

    });
}

这篇关于放置在Gridview中时如何获取项目的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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