thieumao

Di Chuyen View trong Android

Feb 16th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package com.example.testdemonhat;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5.  
  6. import android.os.Bundle;
  7. import android.support.annotation.Nullable;
  8. import android.support.v4.app.Fragment;
  9. import android.util.Log;
  10. import android.view.LayoutInflater;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.LinearLayout;
  15. import android.widget.RelativeLayout;
  16. import android.widget.ScrollView;
  17. import android.widget.TextView;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20.  
  21.     ScrollView scrTest;
  22.     TextView txtHandler;
  23.     LinearLayout lnlTest;
  24.     int y, h;//, scrollY;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_main);
  30.  
  31.         init();
  32.     }
  33.  
  34.     void init() {
  35.         scrTest = (ScrollView) findViewById(R.id.scrTest);
  36.         txtHandler = (TextView) findViewById(R.id.txtHandler);
  37.         lnlTest = (LinearLayout) findViewById(R.id.lnlTest);
  38.         scrTest.setOnTouchListener(new View.OnTouchListener() {
  39.             @Override
  40.             public boolean onTouch(View v, MotionEvent event) {
  41.                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
  42.                     Log.e("Mao", "Mao Bang" + event.getRawX());
  43.                     Log.e("Mao", "Mao Bang" + event.getRawY());
  44.                     int[] lc = new int[2];
  45.                     txtHandler.getLocationOnScreen(lc);
  46.                     if (event.getRawX() > lc[0] && event.getRawX() < lc[0] + txtHandler.getWidth() &&
  47.                             event.getRawY() > lc[1] && event.getRawY() < lc[1] + txtHandler.getHeight()) {
  48.                         y = (int) event.getRawY();
  49.                         h = scrTest.getHeight();
  50. //                        scrollY = scrTest.getScrollY();
  51.                     }
  52.                     return true;
  53.                 } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
  54.                     Log.e("Mao", "Mao Khong Bang");
  55.                     if (y != -1) {
  56.                         RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) scrTest.getLayoutParams();
  57.                         int subtract = (int) event.getRawY() - y;
  58.                         lp.height = h + subtract;
  59.                         scrTest.setLayoutParams(lp);
  60. //                        int sy;
  61. //                        if ((subtract < 0))
  62. //                            sy = Math.abs(subtract);
  63. //                        else
  64. //                            sy = scrTest.getScrollY();
  65.                         int sy;
  66.                         if ((subtract < 0))
  67.                             sy = Math.abs(subtract);
  68.                         else
  69.                             sy = scrTest.getScrollY();
  70.                         scrTest.scrollTo(scrTest.getScrollX(), sy);
  71.  
  72.                         scrTest.scrollTo(scrTest.getScrollX(), sy);
  73.                         return true;
  74.                     }
  75.                 } else if (event.getAction() == MotionEvent.ACTION_UP) {
  76.                     y = -1;
  77.                     return true;
  78.                 }
  79.                 return false;
  80.             }
  81.         });
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment