Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 2.90 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why moving of GroupView works bad?
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:id="@+id/stator"
  7. >
  8.  
  9. <RelativeLayout
  10.     android:layout_width="wrap_content"
  11.     android:layout_height="wrap_content"
  12.     android:layout_alignParentLeft="true"
  13.     android:layout_alignParentTop="true"
  14.     android:layout_marginLeft="0dp"
  15.     android:layout_marginTop="0dp"
  16.     android:id="@+id/mover"
  17.     android:background="@android:color/darker_gray"
  18.     >
  19.  
  20. </RelativeLayout>
  21.  
  22. </RelativeLayout>
  23.        
  24. public class SymbolPadActivity extends Activity {
  25.  
  26. private RelativeLayout mover;
  27. private RelativeLayout stator;
  28.  
  29. /** Called when the activity is first created. */
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32.     super.onCreate(savedInstanceState);
  33.  
  34.     setContentView(R.layout.main);
  35.  
  36.     mover = (RelativeLayout) findViewById(R.id.mover);
  37.  
  38.     RelativeLayout.LayoutParams labelParams;
  39.     TextView textView;
  40.     for(int leftMargin = 0; leftMargin<1500; leftMargin += 200) {
  41.         for(int topMargin=0; topMargin<800; topMargin += 80) {
  42.  
  43.             // I can't omit these 3 lines
  44.             labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  45.             labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
  46.             labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
  47.  
  48.             labelParams.leftMargin = leftMargin;
  49.             labelParams.topMargin = topMargin;
  50.  
  51.             textView = new TextView(this);
  52.             textView.setText("(" + leftMargin + "," + topMargin + ")");
  53.             mover.addView(textView, labelParams);
  54.  
  55.         }
  56.     }
  57.  
  58.     stator = (RelativeLayout) findViewById(R.id.stator);
  59.  
  60.  
  61.     stator.setOnTouchListener(new View.OnTouchListener() {
  62.  
  63.         private int startleft, starttop;
  64.         private float startx, starty;
  65.         private boolean started;
  66.  
  67.         @Override
  68.         public boolean onTouch(View v, MotionEvent event) {
  69.  
  70.             if( event.getActionMasked() == MotionEvent.ACTION_DOWN ) {
  71.                 started = true;
  72.                 startx = event.getX();
  73.                 starty = event.getY();
  74.                 startleft = mover.getLeft();
  75.                 starttop = mover.getTop();
  76.                 return true;
  77.             }
  78.  
  79.             else if( event.getActionMasked() == MotionEvent.ACTION_UP ) {
  80.                 started = false;
  81.                 startx = starty = 0;
  82.                 return true;
  83.             }
  84.  
  85.             else if( event.getActionMasked() == MotionEvent.ACTION_MOVE ) {
  86.                 mover.setLeft(  startleft + (int)(event.getX() - startx) );
  87.                 mover.setTop(  starttop + (int)(event.getY() - starty) );
  88.                 return true;
  89.             }
  90.  
  91.             else {
  92.                 return false;
  93.             }
  94.  
  95.         }
  96.     });
  97. }
  98. }