thieumao

Code vuốt View của Nhất

Feb 20th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package com.example.testnhat;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.os.Bundle;
  6. import android.support.annotation.Nullable;
  7. import android.support.v4.app.Fragment;
  8. import android.view.LayoutInflater;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.view.animation.TranslateAnimation;
  13. import android.widget.FrameLayout;
  14. import android.widget.LinearLayout;
  15. import android.widget.RelativeLayout;
  16. import android.widget.ScrollView;
  17. import android.widget.TextView;
  18.  
  19. import com.example.testnhat.R;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22.  
  23.     LinearLayout scrTest;
  24.     TextView txtHandler;
  25.     LinearLayout lnlTest;
  26.     int y, h,saveH;
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);
  32.         init();
  33.     }
  34.  
  35.     void init() {
  36.         scrTest = (LinearLayout) findViewById(R.id.scrTest);
  37.         txtHandler = (TextView) findViewById(R.id.txtHandler);
  38.         lnlTest = (LinearLayout) findViewById(R.id.lnlTest);
  39.         scrTest.setOnTouchListener(new View.OnTouchListener() {
  40.             @Override
  41.             public boolean onTouch(View v, MotionEvent event) {
  42. //                e(event.getAction());
  43.                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
  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 = (int) lnlTest.getY();
  50.                         saveH=scrTest.getHeight();
  51.                     }
  52.                     return true;
  53.                 } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
  54.                     if (y != -1) {
  55. //                        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) scrTest.getLayoutParams();
  56.                         int subtract = (int) event.getRawY() - y;
  57. //                        lp.height = h + subtract;
  58. //                        scrTest.setLayoutParams(lp);
  59. //                        scrTest.fullScroll(ScrollView.FOCUS_DOWN);
  60.                         lnlTest.setY(h+subtract);
  61.                         LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) scrTest.getLayoutParams();
  62.                         lp.height=saveH+subtract;
  63.                         scrTest.setLayoutParams(lp);
  64.                         return true;
  65.                     }
  66.                 } else if (event.getAction() == MotionEvent.ACTION_UP) {
  67.                     y = -1;
  68.                     return true;
  69.                 }
  70.                 return true;
  71.             }
  72.         });
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment