Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidでドラッグアンドドロップを使用するにはどうすればよいですか?


この例は、Androidでドラッグアンドドロップを使用する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal"
   tools:context="MainActivity" >
   <LinearLayout
      android:id="@+id/leftView"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_margin="10dp"
      android:layout_weight="1"
      android:background="@android:color/darker_gray"
      android:gravity="center_vertical"
      android:orientation="vertical" >
   <ImageView
      android:id="@+id/boxView"
      android:layout_width="75dp"
      android:layout_height="75dp"
      android:layout_gravity="center_vertical|center_horizontal"
      android:layout_margin="10dp"
      android:background="@drawable/one" />
   </LinearLayout>
   <LinearLayout
      android:id="@+id/rightView"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_margin="10dp"
      android:layout_weight="1"
      android:background="@android:color/darker_gray"
      android:gravity="center_vertical"
      android:orientation="vertical" >
   </LinearLayout>
</LinearLayout>
>

ステップ3 −次のコードをsrc / MainActivity.java

に追加します
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnDragListener {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.boxView).setOnTouchListener(this);
      findViewById(R.id.leftView).setOnDragListener(this);
      findViewById(R.id.rightView).setOnDragListener(this);
   }
   @Override
   public boolean onDrag(View view, DragEvent event) {
      if (event.getAction() == DragEvent.ACTION_DROP) {
         view = (View) event.getLocalState();
         if (view.getId() == R.id.leftView || view.getId() == R.id.rightView) {
            ViewGroup source = (ViewGroup) view.getParent();
            source.removeView(view);
            LinearLayout target = (LinearLayout) view;
            target.addView(view);
         }
         view.setVisibility(View.VISIBLE);
      }
      return true;
   }
   @Override
   public boolean onTouch(View view, MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
         View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
         view.startDrag(null, shadowBuilder, view, 0);
         view.setVisibility(View.INVISIBLE);
         return true;
      }
      return false;
   }
}

ステップ4 -次のコードをandroidManifest.xmlに追加します

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–

Androidでドラッグアンドドロップを使用するにはどうすればよいですか?


  1. iOS11でドラッグアンドドロップする方法

    ドラッグアンドドロップは、iOS 11のiPhoneとiPadに登場する新機能であり、PCとMacで何十年も利用可能であったものを改善するように見えます。非常に簡単です。ドラッグしたいアイテムを押し続けるだけで、指に固定されます。そこから、もう一方の手でディスプレイをスワイプして、目的の場所にアイテムをドロップします。 機能はiPhoneの同じアプリ内でアイテムをドラッグアンドドロップすることに制限されていますが、機能が実際に違いを生むのはiPadです。 iPadでは、アプリ間でアイテムをドラッグアンドドロップしたり、アプリ自体をドラッグアンドドロップしたりすることができます。以下のiOS1

  2. Android 可視性リスナーを使用する方法と理由

    Android UI は Views から構築され、通常のアプリケーションには通常いくつかあります。ユーザーが現在どのビューを見ているかを調べるには、Visibility Listeners をインストールする必要があります . ビューの可視性ステータスを識別するために必要なさまざまなオプションについては、以下をお読みください。 可視化する方法 リスナーが機能するためには、まず View がレイアウト階層にあることを確認する必要があります。これには 2 つの方法があります: ビューは XML ファイルで定義されているため、既にレイアウトの一部です View を動的に作成したため、addV