在 Android 中将视图从一个网格视图移动到另一个网格视图
我正在尝试编写一个有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本教程似乎描述了您想要的内容: http:// /blahti.wordpress.com/2011/10/03/drag-drop-for-android-gridview/
This tutorial seems to describe what you want: http://blahti.wordpress.com/2011/10/03/drag-drop-for-android-gridview/