开发者

Moving a view from one gridview to another in Android

I am trying to write an app that has 2 gridviews. The gridviews are side by side on the same screen. I populate the left gridview with the currently installed apps and use their icons so basically I have a gridview of ImageViews. The activity passes an arraylist of Drawables to an ImageAdapter. The imageAdapter extends the BaseAdapter and implements OnTouchListener. My getView() method is similar to the one found on the android developers site

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

}

And here is my onTouch. I have been following other examples but my scenario doesnt involve using AbsoluteLayout and it uses API level 8 so I cant implement an OnDragListener :(

pu开发者_C百科blic 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;

}

Is there anyway I can make this work or do I have to change the way I am approaching this? Any help appreciated!


This tutorial seems to describe what you want: http://blahti.wordpress.com/2011/10/03/drag-drop-for-android-gridview/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜