在Android中将视图从一个gridview移动到另一个gridview

时间:2011-09-29 21:47:21

标签: android gridview drag-and-drop imageview adapter

我正在尝试编写一个包含2个网格视图的应用。网格视图在同一屏幕上并排显示。我用当前安装的应用程序填充左侧gridview并使用它们的图标,所以基本上我有一个ImageViews的gridview。该活动将Drawables的arraylist传递给ImageAdapter。 imageAdapter扩展了BaseAdapter并实现了OnTouchListener。我的getView()方法类似于Android开发者网站上的方法

// create a new ImageView for each item referenced by the Adapter    
public View getView(int position, View convertView, ViewGroup parent) 
{        
    ImageView imageView;

    if (convertView == null) 
    {  
        // if it's not recycled, initialize some attributes            
        imageView = new ImageView(mContext);            
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));            
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);            
        imageView.setPadding(6, 6, 6, 6);        
    } 
    else 
    {            
        imageView = (ImageView) convertView;        
    }        

    //imageView.setImageResource(mThumbIds[position]);        
    imageView.setImageDrawable(mList.get(position));
    imageView.setContentDescription(mNames.get(position));
    imageView.setDrawingCacheEnabled(true);
    imageView.setOnTouchListener(this);

    return imageView;

}

这是我的onTouch。我一直在关注其他示例,但我的场景不涉及使用AbsoluteLayout,它使用API​​级别8,因此我无法实现OnDragListener:(

public boolean onTouch(View v, MotionEvent event) {

LayoutParams par = (LayoutParams) v.getLayoutParams();
int x = (int) event.getX();
int y = (int) event.getY();

    if (event.getAction() == MotionEvent.ACTION_DOWN)
    {
        Log.i("ACT DOWN",  (String) v.getContentDescription());
        status = START_DRAGGING;
        v.setLayoutParams(par);

        //image = new ImageView(mContext);
        //mLayout.addView(image, par);
        //Toast.makeText(AppMoverActivity.this, "" + v.getContentDescription(), Toast.LENGTH_SHORT).show();
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        status = STOP_DRAGGING;
        v.setLayoutParams(par); 

        Log.i("ACT UP", "Stopped Dragging");
    }
    else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        if (status == START_DRAGGING) {
            Log.i("ACT MOVE", "Moving");
            //Implement ability to drag the view to other grid...
        }
    }
    return true;

}

无论如何我可以做这项工作还是我必须改变我接近这个的方式?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

本教程似乎描述了您想要的内容:http://blahti.wordpress.com/2011/10/03/drag-drop-for-android-gridview/