Android Horizontalscrollview A submenu Drag the instance code horizontally _android

Source: Internet
Author: User

Objective

Online listview Drag up and down examples have, the effect is also very good, but the project should be dragged sideways, as long as the bite the bullet oneself write (mainly not found suitable), reference article 1 modified and come, share.

Body

Screenshots

Implementation code:

public class Hodragactivity extends activity {private LinearLayout main;

 Private Gesturedetector Mgesturedetector;
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

  Setcontentview (R.LAYOUT.MAIN_CC);

  Main = (linearlayout) Findviewbyid (R.id.main);
  Binddraplistener (R.ID.MYIMAGE1);
  Binddraplistener (R.id.myimage2);
  Binddraplistener (R.id.myimage3);
  Binddraplistener (R.ID.MYIMAGE4);
  Binddraplistener (R.ID.MYIMAGE5);
  Binddraplistener (R.id.myimage6);
  Binddraplistener (R.id.myimage7);
  Binddraplistener (R.id.myimage8);
  Binddraplistener (R.ID.MYIMAGE9);
  Binddraplistener (R.ID.MYIMAGE10);
  Binddraplistener (R.ID.MYIMAGE11);

  Binddraplistener (R.ID.MYIMAGE12);
 Mgesturedetector = new Gesturedetector (This, new Drapgesturelistener ());

 Private View Mdrapview;
  private void Binddraplistener (int id) {View v = Findviewbyid (id);
  V.setontouchlistener (Montouchlistener);
 V.setondraglistener (Mondraglistener); } PrivaTe ontouchlistener montouchlistener = new Ontouchlistener () {@Override public boolean ontouch (View V, motionevent EV

   ENT) {Mdrapview = v;

   if (Mgesturedetector.ontouchevent (event)) return true;
   Switch (event.getaction () & Motionevent.action_mask) {case MotionEvent.ACTION_UP:break;
  return false;

 }
 }; Private Ondraglistener Mondraglistener = new Ondraglistener () {@Override public boolean Ondrag (View V, dragevent Eve
   NT) {switch (event.getaction ()) {case dragevent.action_drag_started://Does nothing break;
    Case Dragevent.action_drag_entered:v.setalpha (0.5F);
   Break
    Case Dragevent.action_drag_exited:v.setalpha (1F);
   Break
    Case DragEvent.ACTION_DROP:View view = (view) event.getlocalstate (); for (int i = 0, j = main.getchildcount (); i < J; i++) {if (Main.getchildat (i) = v) {//Current position main.re
      Moveview (view);
      Main.addview (view, i);
     Break }} BreaK
   Case Dragevent.action_drag_ended:v.setalpha (1F);
   Default:break;
  return true;

 }
 }; Private class Drapgesturelistener extends Simpleongesturelistener {@Override public boolean onsingletapconfirmed (Moti
  OnEvent e) {return super.onsingletapconfirmed (e);
   @Override public void onlongpress (Motionevent e) {super.onlongpress (e);
   Clipdata data = Clipdata.newplaintext ("", "");
   Mydragshadowbuilder Shadowbuilder = new Mydragshadowbuilder (Mdrapview);
  Mdrapview.startdrag (data, Shadowbuilder, Mdrapview, 0);
  @Override public boolean Ondown (Motionevent e) {return true; } private class Mydragshadowbuilder extends View.dragshadowbuilder {private final weakreference<view> Mview

  ;
   Public Mydragshadowbuilder (view view) {super (view);
  Mview = new weakreference<view> (View);
   @Override public void Ondrawshadow (Canvas Canvas) {Canvas.scale (1.5F, 1.5F);
  Super.ondrawshadow (canvas);

  }@Override public void Onprovideshadowmetrics (Point shadowsize, point shadowtouchpoint) {//Super.onprovideshadow

   Metrics (Shadowsize, shadowtouchpoint);
   Final View view = Mview.get ();
    if (view!= null) {shadowsize.set (int) (View.getwidth () * 1.5F), (int) (View.getheight () * 1.5F));
   Shadowtouchpoint.set (SHADOWSIZE.X/2, SHADOWSIZE.Y/2);
   else {//LOG.E (View.view_log_tag,//"Asked for drag thumb metrics no View"); } } } }

Code description

1, Mydragshadowbuilder is mainly used to zoom drag-and-drop imageview, here magnified 1.5 times times, without affecting the original control, here is magnified twice times

2, here is through the long press to arouse drag and drop operation, so listen to the onlongpress.

3, notice drag Ondraglistener is Android 3.0 just add API.

The above is the Android Horizontalscrollview control horizontal drag and drop instances, welcome to exchange, there is a need for reference.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.