Android Gridview:拖动一个项目

时间:2013-12-19 10:59:31

标签: java android gridview draggable drag

我感谢谁试图帮助我,对我来说非常重要......

我正在尝试对gridview内部的项目实施拖动,我在互联网上看到了很多可以理解的例子:/

我在我的xml中有一个带有不同图像的网格视图:

<GridView 
android:id="@+id/contenedorPictogramas"
android:layout_width="780dp" 
android:layout_height="520dp"
android:columnWidth="160dp"
android:numColumns="auto_fit"
android:verticalSpacing="20dp"
android:horizontalSpacing="20dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="@drawable/shape"
/>

我使用一种方法来拖动另一张图像:

private final class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
      } else {
        return false;
      }
    }
  }

要将图像设置为gridview,我尝试这样做:

GridView gridview = (GridView) findViewById(R.id.contenedorPictogramas);
gridview.setOnTouchListener(new OnTouchListener() {         
      public boolean onTouch(View arg0, MotionEvent arg1) {
          arg0.setOnTouchListener(new MyTouchListener());//HERE IS THE PROBLEM (I THING)
          return false;
      }
  });

但我拖动gridview的所有项目:/

1 个答案:

答案 0 :(得分:0)

您应为网格视图项设置触摸侦听器,而不是在网格视图上。

您可以在适配器的getview方法中添加触控侦听器。

样品 -

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(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
**imageView.setOnTouchListener(<your listener>);**
        return imageView;
    }