【问题标题】:Android grid-view item drag and drop rearrange and grid item drag and drop it anywhere in screenAndroid网格视图项目拖放重新排列和网格项目将其拖放到屏幕中的任何位置
【发布时间】:2014-03-06 05:46:01
【问题描述】:

Android 网格视图拖放重新排列同时我想像绝对布局一样拖放屏幕中的任何位置。有可能实现吗?

【问题讨论】:

    标签: android gridview drag-and-drop


    【解决方案1】:

    试试这个库,它也有示例项目

    DraggableGridView

    尝试使用此库将项目从一页拖到另一页

    PagedDragDropGrid

    【讨论】:

      【解决方案2】:

      是的,有可能。这里很好tutorial

      在本教程中,他们使用以下代码:-

      在您的 res 目录中创建可绘制文件夹。在此文件夹中创建以下 shape.xml 文件。

      <?xml version="1.0" encoding="UTF-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle" >
      
          <stroke
              android:width="2dp"
              android:color="#FFFFFFFF" />
      
          <gradient
              android:angle="225"
              android:endColor="#DD2ECCFA"
              android:startColor="#DD000000" />
      
          <corners
              android:bottomLeftRadius="7dp"
              android:bottomRightRadius="7dp"
              android:topLeftRadius="7dp"
              android:topRightRadius="7dp" />
      
      </shape> 
      

      // 在drawable文件夹中创建shape_droptarget.xml文件。

      <?xml version="1.0" encoding="UTF-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle" >
      
          <stroke
              android:width="2dp"
              android:color="#FFFF0000" />
      
          <gradient
              android:angle="225"
              android:endColor="#DD2ECCFA"
              android:startColor="#DD000000" />
      
          <corners
              android:bottomLeftRadius="7dp"
              android:bottomRightRadius="7dp"
              android:topLeftRadius="7dp"
              android:topRightRadius="7dp" />
      
      </shape>
      

      将 Activity 的布局更改为以下代码。

      <?xml version="1.0" encoding="utf-8"?>
      <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:columnCount="2"
          android:columnWidth="300dp"
          android:orientation="vertical"
          android:rowCount="2"
          android:stretchMode="columnWidth" >
      
          <LinearLayout
              android:id="@+id/topleft"
              android:layout_width="160dp"
              android:layout_height="200dp"
              android:layout_column="0"
              android:layout_row="0"
              android:background="@drawable/shape" >
      
              <ImageView
                  android:id="@+id/myimage1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_column="0"
                  android:layout_row="0"
                  android:src="@drawable/ic_launcher" />
          </LinearLayout>
      
          <LinearLayout
              android:id="@+id/topright"
              android:layout_width="160dp"
              android:layout_height="200dp"
              android:layout_column="1"
              android:layout_row="0"
              android:background="@drawable/shape" >
      
              <ImageView
                  android:id="@+id/myimage2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_column="0"
                  android:layout_row="0"
                  android:src="@drawable/ic_launcher" />
          </LinearLayout>
      
          <LinearLayout
              android:id="@+id/bottomleft"
              android:layout_width="160dp"
              android:layout_height="200dp"
              android:layout_column="0"
              android:layout_row="1"
              android:background="@drawable/shape" >
      
              <ImageView
                  android:id="@+id/myimage3"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:src="@drawable/ic_launcher" />
          </LinearLayout>
      
          <LinearLayout
              android:id="@+id/bottomright"
              android:layout_width="160dp"
              android:layout_height="200dp"
              android:layout_column="1"
              android:layout_row="1"
              android:background="@drawable/shape" >
      
              <ImageView
                  android:id="@+id/myimage4"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_column="0"
                  android:layout_row="0"
                  android:src="@drawable/ic_launcher" />
          </LinearLayout>
      
      </GridLayout>
      

      将您的活动类更改为以下代码。

      public class DragActivity extends Activity {
      
      /** Called when the activity is first created. */
      
        @Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
          findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
          findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
          findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
          findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
          findViewById(R.id.topright).setOnDragListener(new MyDragListener());
          findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
          findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());
      
        }
      
        private final class MyTouchListener implements OnTouchListener {
          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;
            }
          }
        }
      
        class MyDragListener implements OnDragListener {
          Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
          Drawable normalShape = getResources().getDrawable(R.drawable.shape);
      
          @Override
          public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
              // do nothing
              break;
            case DragEvent.ACTION_DRAG_ENTERED:
              v.setBackgroundDrawable(enterShape);
              break;
            case DragEvent.ACTION_DRAG_EXITED:
              v.setBackgroundDrawable(normalShape);
              break;
            case DragEvent.ACTION_DROP:
              // Dropped, reassign View to ViewGroup
              View view = (View) event.getLocalState();
              ViewGroup owner = (ViewGroup) view.getParent();
              owner.removeView(view);
              LinearLayout container = (LinearLayout) v;
              container.addView(view);
              view.setVisibility(View.VISIBLE);
              break;
            case DragEvent.ACTION_DRAG_ENDED:
              v.setBackgroundDrawable(normalShape);
            default:
              break;
            }
            return true;
          }
        }
      }
      

      如果您启动此活动,您应该能够将 ImageViews 拖到另一个容器中。

      【讨论】:

        猜你喜欢
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-17
        • 2018-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多