আপনি ইভেন্টে রেসপন্স করার মাধ্যমে ভিউতে টেনে নিয়ে রাখুন প্রসেস প্রয়োগ করতে পারবেন যা টেনে নিয়ে যাওয়া শুরু করতে পারে এবং ড্রপ ইভেন্ট রেসপন্স ও ব্যবহার করতে পারে।
টেনে আনার প্রক্রিয়া শুরু করুন
ব্যবহারকারী কোনও আইটেম টেনে নিয়ে যাওয়ার জন্য সাধারণত সেটিতে টাচ করে বা ক্লিক করে ধরে রেখে জেসচারের মাধ্যমে টেনে নিয়ে যাওয়া শুরু করেন।
View-এ এটি ম্যানেজ করতে, ClipData অবজেক্ট এবং
যে ডেটা সরানো হচ্ছে তার জন্য
ClipData.Item অবজেক্ট তৈরি করুন। ClipData-এর অংশ হিসেবে, মেটাডেটা প্রদান করুন যা
ClipData-এর মধ্যে
ClipDescription অবজেক্টে
সেভ করা থাকে। টেনে নিয়ে রাখুন অপারেশন যা ডেটা মুভমেন্টকে দেখায় না, তার জন্য আপনি আসল অবজেক্টের পরিবর্তে null ব্যবহার করতে চাইতে পারেন।
যেমন, এই কোড স্নিপেট দেখায় যে কীভাবে ImageView-এ টাচ করে ধরে রাখার
জেসচারের উত্তর দিতে হয়। এর জন্য ClipData অবজেক্ট তৈরি করতে হয় যাতে
ImageView-এর ট্যাগ (বা লেবেল) থাকে:
Kotlin
// Create a string for the ImageView label. val IMAGEVIEW_TAG = "icon bitmap" ... val imageView = ImageView(context).apply { // Set the bitmap for the ImageView from an icon bitmap defined elsewhere. setImageBitmap(iconBitmap) tag = IMAGEVIEW_TAG setOnLongClickListener { v -> // Create a new ClipData. This is done in two steps to provide // clarity. The convenience method ClipData.newPlainText() can // create a plain text ClipData in one step. // Create a new ClipData.Item from the ImageView object's tag. val item = ClipData.Item(v.tag as? CharSequence) // Create a new ClipData using the tag as a label, the plain text // MIME type, and the already-created item. This creates a new // ClipDescription object within the ClipData and sets its MIME type // to "text/plain". val dragData = ClipData( v.tag as? CharSequence, arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN), item) // Instantiate the drag shadow builder. We use this imageView object // to create the default builder. val myShadow = View.DragShadowBuilder(view: this) // Start the drag. v.startDragAndDrop(dragData, // The data to be dragged. myShadow, // The drag shadow builder. null, // No need to use local data. 0 // Flags. Not currently used, set to 0. ) // Indicate that the long-click is handled. true } }
জাভা
// Create a string for the ImageView label. private static final String IMAGEVIEW_TAG = "icon bitmap"; ... // Create a new ImageView. ImageView imageView = new ImageView(context); // Set the bitmap for the ImageView from an icon bitmap defined elsewhere. imageView.setImageBitmap(iconBitmap); // Set the tag. imageView.setTag(IMAGEVIEW_TAG); // Set a long-click listener for the ImageView using an anonymous listener // object that implements the OnLongClickListener interface. imageView.setOnLongClickListener( v -> { // Create a new ClipData. This is done in two steps to provide clarity. The // convenience method ClipData.newPlainText() can create a plain text // ClipData in one step. // Create a new ClipData.Item from the ImageView object's tag. ClipData.Item item = new ClipData.Item((CharSequence) v.getTag()); // Create a new ClipData using the tag as a label, the plain text MIME type, // and the already-created item. This creates a new ClipDescription object // within the ClipData and sets its MIME type to "text/plain". ClipData dragData = new ClipData( (CharSequence) v.getTag(), new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }, item); // Instantiate the drag shadow builder. We use this imageView object // to create the default builder. View.DragShadowBuilder myShadow = new View.DragShadowBuilder(imageView); // Start the drag. v.startDragAndDrop(dragData, // The data to be dragged. myShadow, // The drag shadow builder. null, // No need to use local data. 0 // Flags. Not currently used, set to 0. ); // Indicate that the long-click is handled. return true; });
টেনে আনার প্রক্রিয়া শুরু করার জন্য প্রতিক্রিয়া জানান
ড্র্যাগ অপারেশন চলাকালীন, সিস্টেম বর্তমান লেআউটে View অবজেক্টের ড্র্যাগ ইভেন্ট লিসনারে
ড্র্যাগ ইভেন্ট পাঠায়। অ্যাকশনের ধরন পেতে
DragEvent.getAction() কল করে শ্রোতারা প্রতিক্রিয়া জানান। টেনে আনার শুরুতে,
এই পদ্ধতি ACTION_DRAG_STARTED রিটার্ন করে।
ACTION_DRAG_STARTED অ্যাকশন টাইপ সহ কোনও ইভেন্টের উত্তরে, ড্র্যাগ ইভেন্ট লিসনারকে
অবশ্যই নিম্নলিখিত কাজগুলি করতে হবে:
DragEvent.getClipDescription()কল করুন এবং টেনে আনা ডেটা লিসেনার গ্রহণ করতে পারবে কিনা তা দেখতে, ফেরত আসাClipDescription-এ MIME-এর প্রকার পদ্ধতি ব্যবহার করুন।টেনে এনে ড্রপ করার অপারেশন যদি ডেটা মুভমেন্ট না দেখায়, তাহলে এটি প্রয়োজনীয় নাও হতে পারে।
ড্র্যাগ ইভেন্ট লিসনার যদি ড্রপ গ্রহণ করতে পারে, তাহলে লিসনারকে ড্র্যাগ ইভেন্ট পাঠানো চালিয়ে যাওয়ার জন্য সিস্টেমকে জানাতে এটি অবশ্যই
trueরিটার্ন করবে। লিসনার ড্রপ গ্রহণ করতে না পারলে, লিসনারকে অবশ্যইfalseরিটার্ন করতে হবে এবং সিস্টেম লিসনারকে ড্র্যাগ ইভেন্ট পাঠানো বন্ধ করে দেয় যতক্ষণ না সিস্টেমACTION_DRAG_ENDEDপাঠায়, যাতে টেনে নিয়ে রাখুন অপারেশন শেষ করা যায়।
ACTION_DRAG_STARTED ইভেন্টের জন্য, নিম্নলিখিত DragEvent পদ্ধতিগুলি
সঠিক নয়: getClipData(),
getX(),
getY() এবং
getResult()।
টেনে আনার সময় ইভেন্ট ম্যানেজ করুন
ড্র্যাগ অ্যাকশন চলাকালীন, ড্র্যাগ ইভেন্ট লিসনার যা ACTION_DRAG_STARTED ড্র্যাগ ইভেন্টের উত্তরে true রিটার্ন করে,
সেটি ড্র্যাগ ইভেন্ট পাওয়া চালিয়ে যায়। ড্র্যাগ করার সময় লিসনার যে ধরনের ড্র্যাগ ইভেন্ট পায় তা ড্র্যাগ শ্যাডোর লোকেশন এবং লিসনারের View-এর দৃশ্যমানতার উপর নির্ভর করে। শ্রোতারা মূলত ড্র্যাগ ইভেন্ট ব্যবহার করে সিদ্ধান্ত নেন যে তাদের View-এর চেহারা পরিবর্তন করতে হবে কিনা।
টেনে আনার সময়, DragEvent.getAction() তিনটি ভ্যালুর মধ্যে একটি রিটার্ন করে:
ACTION_DRAG_ENTERED: টাচ পয়েন্ট—ব্যবহারকারীর আঙুল বা মাউসের নিচে স্ক্রিনের পয়েন্ট—লিসনারেরView-এর বাউন্ডিং বক্সে প্রবেশ করলে, লিসনার এই ইভেন্ট অ্যাকশন টাইপ রিসিভ করে।ACTION_DRAG_LOCATION: লিসেনার একবারACTION_DRAG_ENTEREDইভেন্ট পেলে, টাচ পয়েন্ট যতবার সরে যায় ততবার এটি নতুনACTION_DRAG_LOCATIONইভেন্ট পায়, যতক্ষণ না এটিACTION_DRAG_EXITEDইভেন্ট পায়।getX()এবংgetY()পদ্ধতি টাচ পয়েন্টের X এবং Y কোঅর্ডিনেট রিটার্ন করে।ACTION_DRAG_EXITED: এই ইভেন্ট অ্যাকশন টাইপ এমন একজন শ্রোতাকে পাঠানো হয় যিনি আগেACTION_DRAG_ENTEREDপেয়েছেন। ড্র্যাগ শ্যাডো টাচ পয়েন্ট লিসনারেরView-এর বাউন্ডিং বক্সের ভিতর থেকে বাউন্ডিং বক্সের বাইরে চলে গেলে ইভেন্টটি পাঠানো হয়।
ড্র্যাগ ইভেন্ট লিসনারকে এইসব অ্যাকশন টাইপের কোনওটিতেই প্রতিক্রিয়া জানাতে হয় না। যদি লিসেনার সিস্টেমে কোনও ভ্যালু ফেরত পাঠায়, তাহলে সেটি উপেক্ষা করা হয়।
এই ধরনের প্রতিটি অ্যাকশনের উত্তর দেওয়ার জন্য এখানে কিছু নির্দেশিকা দেওয়া হল:
ACTION_DRAG_ENTEREDবাACTION_DRAG_LOCATION-এর উত্তরে, লিসেনারView-এর উপস্থিতি পরিবর্তন করতে পারে যাতে বোঝা যায় যে ভিউটি একটি সম্ভাব্য ড্রপ টার্গেট।ACTION_DRAG_LOCATIONঅ্যাকশন টাইপ সহ ইভেন্টে টাচ পয়েন্টের লোকেশন অনুযায়ীgetX()ওgetY()-এর জন্য সঠিক ডেটা আছে। লিসেনার এই তথ্য ব্যবহার করে টাচ পয়েন্টেView-এর উপস্থিতি পরিবর্তন করতে পারে অথবা ব্যবহারকারী ঠিক কোন পজিশনে কন্টেন্ট ড্রপ করতে পারবেন তা নির্ধারণ করতে পারে।ACTION_DRAG_EXITED-এর উত্তরে, লিসেনারকে অবশ্যইACTION_DRAG_ENTEREDবাACTION_DRAG_LOCATION-এর উত্তরে প্রয়োগ করা যেকোনও চেহারা পরিবর্তন রিসেট করতে হবে। এটি ব্যবহারকারীকে দেখায় যেViewআর ড্রপ টার্গেট নয়।
ড্রপের উত্তর দিন
ব্যবহারকারী View-এর উপর ড্র্যাগ শ্যাডো রিলিজ করলে এবং View আগে
রিপোর্ট করলে যে এটি ড্র্যাগ করা কন্টেন্ট গ্রহণ করতে পারে, সিস্টেম ACTION_DROP অ্যাকশন টাইপ সহ
View-এ ড্র্যাগ ইভেন্ট পাঠায়।
ড্র্যাগ ইভেন্ট লিসনারকে অবশ্যই নিম্নলিখিত কাজগুলি করতে হবে:
ClipDataঅবজেক্ট পেতেgetClipData()কল করুন যা মূলতstartDragAndDrop()কলের মাধ্যমে সরবরাহ করা হয় এবং ডেটা প্রসেস করুন। টেনে এনে ছেড়ে দেওয়ার অপারেশন যদি ডেটা মুভমেন্টকে না বোঝায়, তাহলে এটি অপ্রয়োজনীয়।ড্রপ সফলভাবে প্রসেস করা হয়েছে কিনা তা নির্দেশ করতে বুলিয়ান
trueরিটার্ন করুন, অথবা না হলেfalseরিটার্ন করুন। ফেরত দেওয়া ভ্যালু,getResult()-এর মাধ্যমেACTION_DRAG_ENDEDইভেন্টের জন্য ফেরত দেওয়া ভ্যালু হয়ে যায়। সিস্টেম যদিACTION_DROPইভেন্ট না পাঠায়, তাহলেACTION_DRAG_ENDEDইভেন্টের জন্যgetResult()-এর মাধ্যমে ফেরত পাঠানো ভ্যালু হলfalse।
ACTION_DROP ইভেন্টের জন্য, getX() এবং getY() View-এর কোঅর্ডিনেট সিস্টেম ব্যবহার করে
যা ড্রপ করার মুহূর্তে টাচ পয়েন্টের X এবং Y পজিশন ফিরিয়ে আনতে
ড্রপ রিসিভ করে।
ব্যবহারকারী যদি এমন কোনও View-এর উপর ড্র্যাগ শ্যাডো রিলিজ করেন যার ড্র্যাগ ইভেন্ট
লিসনার ড্র্যাগ ইভেন্ট পাচ্ছে না, আপনার অ্যাপের UI-এর খালি জায়গা বা এমনকি
আপনার অ্যাপ্লিকেশনের বাইরের কোনও জায়গা, Android সেক্ষেত্রে ACTION_DROP অ্যাকশন
টাইপ সহ কোনও ইভেন্ট পাঠাবে না এবং শুধুমাত্র একটি ACTION_DRAG_ENDED ইভেন্ট পাঠাবে।
টেনে নিয়ে যাওয়া শেষ করার ইভেন্টে প্রতিক্রিয়া জানানো
ব্যবহারকারী ড্র্যাগ শ্যাডো রিলিজ করার সাথে সাথেই, সিস্টেম আপনার অ্যাপ্লিকেশনের সব ড্র্যাগ ইভেন্ট লিসনারকে
ACTION_DRAG_ENDED অ্যাকশন টাইপ সহ একটি ড্র্যাগ
ইভেন্ট পাঠায়। এটি থেকে বোঝা যায় যে ড্র্যাগ অপারেশন শেষ হয়েছে।
প্রতিটি ড্র্যাগ ইভেন্ট লিসনারকে অবশ্যই নিম্নলিখিতগুলি করতে হবে:
- অপারেশন চলাকালীন লিসেনার তার চেহারা পরিবর্তন করলে, অপারেশন শেষ হয়েছে তা ব্যবহারকারীকে ভিজ্যুয়াল ইঙ্গিত হিসেবে দেখানোর জন্য এটি তার ডিফল্ট চেহারায় রিসেট করা উচিত।
- শ্রোতা চাইলে
getResult()নম্বরে কল করে অপারেশন সম্পর্কে আরও জানতে পারেন। অ্যাকশন টাইপACTION_DROP-এর ইভেন্টের উত্তরে কোনও লিসনারtrueরিটার্ন করলে,getResult()বুলিয়ানtrueরিটার্ন করে। অন্য সব ক্ষেত্রে,getResult()বুলিয়ানfalseরিটার্ন করে, যার মধ্যে সিস্টেমACTION_DROPইভেন্ট না পাঠালে কী হয় তা অন্তর্ভুক্ত। - ড্রপ অপারেশন সফলভাবে সম্পূর্ণ হয়েছে তা বোঝাতে, লিসনারকে
সিস্টেমে বুলিয়ান
trueরিটার্ন করতে হবে।falseফিরিয়ে না দিয়ে, ড্রপ শ্যাডো তার সোর্সে ফিরে যাচ্ছে এমন ভিজ্যুয়াল ইঙ্গিত ব্যবহারকারীকে সাজেশন দিতে পারে যে অপারেশনটি সফল হয়নি।
ড্র্যাগ ইভেন্টের উত্তর দেওয়া: একটি উদাহরণ
আপনার ড্র্যাগ ইভেন্ট পদ্ধতি বা লিসনারের মাধ্যমে সব ড্র্যাগ ইভেন্ট পাওয়া যায়। নিম্নলিখিত কোড স্নিপেটটি ড্র্যাগ ইভেন্টের উত্তর দেওয়ার একটি উদাহরণ:
Kotlin
val imageView = ImageView(this) // Set the drag event listener for the View. imageView.setOnDragListener { v, e -> // Handle each of the expected events. when (e.action) { DragEvent.ACTION_DRAG_STARTED -> { // Determine whether this View can accept the dragged data. if (e.clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { // As an example, apply a blue color tint to the View to // indicate that it can accept data. (v as? ImageView)?.setColorFilter(Color.BLUE) // Invalidate the view to force a redraw in the new tint. v.invalidate() // Return true to indicate that the View can accept the dragged // data. true } else { // Return false to indicate that, during the current drag and // drop operation, this View doesn't receive events again until // ACTION_DRAG_ENDED is sent. false } } DragEvent.ACTION_DRAG_ENTERED -> { // Apply a green tint to the View. (v as? ImageView)?.setColorFilter(Color.GREEN) // Invalidate the view to force a redraw in the new tint. v.invalidate() // Return true. The value is ignored. true } DragEvent.ACTION_DRAG_LOCATION -> // Ignore the event. true DragEvent.ACTION_DRAG_EXITED -> { // Reset the color tint to blue. (v as? ImageView)?.setColorFilter(Color.BLUE) // Invalidate the view to force a redraw in the new tint. v.invalidate() // Return true. The value is ignored. true } DragEvent.ACTION_DROP -> { // Get the item containing the dragged data. val item: ClipData.Item = e.clipData.getItemAt(0) // Get the text data from the item. val dragData = item.text // Display a message containing the dragged data. Toast.makeText(this, "Dragged data is $dragData", Toast.LENGTH_LONG).show() // Turn off color tints. (v as? ImageView)?.clearColorFilter() // Invalidate the view to force a redraw. v.invalidate() // Return true. DragEvent.getResult() returns true. true } DragEvent.ACTION_DRAG_ENDED -> { // Turn off color tinting. (v as? ImageView)?.clearColorFilter() // Invalidate the view to force a redraw. v.invalidate() // Do a getResult() and display what happens. when(e.result) { true -> Toast.makeText(this, "The drop was handled.", Toast.LENGTH_LONG) else -> Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_LONG) }.show() // Return true. The value is ignored. true } else -> { // An unknown action type is received. Log.e("DragDrop Example", "Unknown action type received by View.OnDragListener.") false } } }
জাভা
View imageView = new ImageView(this); // Set the drag event listener for the View. imageView.setOnDragListener( (v, e) -> { // Handle each of the expected events. switch(e.getAction()) { case DragEvent.ACTION_DRAG_STARTED: // Determine whether this View can accept the dragged data. if (e.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { // As an example, apply a blue color tint to the View to // indicate that it can accept data. ((ImageView)v).setColorFilter(Color.BLUE); // Invalidate the view to force a redraw in the new tint. v.invalidate(); // Return true to indicate that the View can accept the dragged // data. return true; } // Return false to indicate that, during the current drag-and-drop // operation, this View doesn't receive events again until // ACTION_DRAG_ENDED is sent. return false; case DragEvent.ACTION_DRAG_ENTERED: // Apply a green tint to the View. ((ImageView)v).setColorFilter(Color.GREEN); // Invalidate the view to force a redraw in the new tint. v.invalidate(); // Return true. The value is ignored. return true; case DragEvent.ACTION_DRAG_LOCATION: // Ignore the event. return true; case DragEvent.ACTION_DRAG_EXITED: // Reset the color tint to blue. ((ImageView)v).setColorFilter(Color.BLUE); // Invalidate the view to force a redraw in the new tint. v.invalidate(); // Return true. The value is ignored. return true; case DragEvent.ACTION_DROP: // Get the item containing the dragged data. ClipData.Item item = e.getClipData().getItemAt(0); // Get the text data from the item. CharSequence dragData = item.getText(); // Display a message containing the dragged data. Toast.makeText(this, "Dragged data is " + dragData, Toast.LENGTH_LONG).show(); // Turn off color tints. ((ImageView)v).clearColorFilter(); // Invalidate the view to force a redraw. v.invalidate(); // Return true. DragEvent.getResult() returns true. return true; case DragEvent.ACTION_DRAG_ENDED: // Turn off color tinting. ((ImageView)v).clearColorFilter(); // Invalidate the view to force a redraw. v.invalidate(); // Do a getResult() and displays what happens. if (e.getResult()) { Toast.makeText(this, "The drop was handled.", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_LONG).show(); } // Return true. The value is ignored. return true; // An unknown action type is received. default: Log.e("DragDrop Example","Unknown action type received by View.OnDragListener."); break; } return false; });
ড্র্যাগ শ্যাডো কাস্টমাইজ করা
View.DragShadowBuilder-এ পদ্ধতি ওভাররাইড করে আপনি কাস্টমাইজ করা myDragShadowBuilder নির্ধারণ করতে পারবেন। নিম্নলিখিত কোড স্নিপেট TextView-এর জন্য একটি ছোট,
আয়তাকার, ধূসর রঙের ড্র্যাগ শ্যাডো তৈরি করে:
Kotlin
private class MyDragShadowBuilder(view: View) : View.DragShadowBuilder(view) { private val shadow = ColorDrawable(Color.LTGRAY) // Define a callback that sends the drag shadow dimensions and touch point // back to the system. override fun onProvideShadowMetrics(size: Point, touch: Point) { // Set the width of the shadow to half the width of the original // View. val width: Int = view.width / 2 // Set the height of the shadow to half the height of the original // View. val height: Int = view.height / 2 // The drag shadow is a ColorDrawable. Set its dimensions to // be the same as the Canvas that the system provides. As a result, // the drag shadow fills the Canvas. shadow.setBounds(0, 0, width, height) // Set the size parameter's width and height values. These get back // to the system through the size parameter. size.set(width, height) // Set the touch point's position to be in the middle of the drag // shadow. touch.set(width / 2, height / 2) } // Define a callback that draws the drag shadow in a Canvas that the system // constructs from the dimensions passed to onProvideShadowMetrics(). override fun onDrawShadow(canvas: Canvas) { // Draw the ColorDrawable on the Canvas passed in from the system. shadow.draw(canvas) } }
জাভা
private static class MyDragShadowBuilder extends View.DragShadowBuilder { // The drag shadow image, defined as a drawable object. private static Drawable shadow; // Constructor. public MyDragShadowBuilder(View view) { // Store the View parameter. super(view); // Create a draggable image that fills the Canvas provided by the // system. shadow = new ColorDrawable(Color.LTGRAY); } // Define a callback that sends the drag shadow dimensions and touch point // back to the system. @Override public void onProvideShadowMetrics (Point size, Point touch) { // Define local variables. int width, height; // Set the width of the shadow to half the width of the original // View. width = getView().getWidth() / 2; // Set the height of the shadow to half the height of the original // View. height = getView().getHeight() / 2; // The drag shadow is a ColorDrawable. Set its dimensions to // be the same as the Canvas that the system provides. As a result, // the drag shadow fills the Canvas. shadow.setBounds(0, 0, width, height); // Set the size parameter's width and height values. These get back // to the system through the size parameter. size.set(width, height); // Set the touch point's position to be in the middle of the drag // shadow. touch.set(width / 2, height / 2); } // Define a callback that draws the drag shadow in a Canvas that the system // constructs from the dimensions passed to onProvideShadowMetrics(). @Override public void onDrawShadow(Canvas canvas) { // Draw the ColorDrawable on the Canvas passed in from the system. shadow.draw(canvas); } }