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

发布于 2024-12-07 10:44:41 字数 2347 浏览 2 评论 0原文

我正在尝试编写一个有 2 个网格视图的应用程序。网格视图在同一屏幕上并排。我用当前安装的应用程序填充左侧网格视图并使用它们的图标,所以基本上我有一个 ImageViews 的网格视图。该活动将 Drawables 的数组列表传递给 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.getView() 方法。我一直在关注其他示例,但我的场景不涉及使用 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;

}

无论如何我可以完成这项工作,还是我必须改变我处理此问题的方式?有任何帮助吗赞赏!

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

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;

}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文