How to hold and drag (re-position) a layout along with its associated layouts in android

后端 未结 1 577
孤街浪徒
孤街浪徒 2021-02-10 05:22

I am making an android app. In which I have a scenario. First watch screen shot below so any one come here to answer has some clear picture that what I want.

1条回答
  •  梦毁少年i
    2021-02-10 06:09

    1. Material way

      You can archive it(or, at least, very close to what you want) with CoordinatorLayout and keeping your map layout inside CollapsingToolbarLayout.

      Your layout can look like this:

      
      
          
      
              
              
                  .....
              
              
              
      
          
      
         
          
      
      
      
      
      

      You can read more here Android Design Support Library and here Design Support Library (III): Coordinator Layout

    2. View.OnTouchListener way

      (This is the one you use)

      Idea is simple:

      • Set Map android:layout_above red separator & ListView - android:layout_below red separator
      • Once moving action done - translate separator respectively up or down and set proper top&bottom mangins

      Here's how it looks:

          public class MainActivity extends AppCompatActivity  implements View.OnTouchListener {
              private int _yDelta;
              @Override
              protected void onCreate(Bundle arg0) {
                  super.onCreate(arg0);
                  setContentView(R.layout.activity_main);
                  findViewById(R.id.separator).setOnTouchListener(this);
              }
      
              @Override
              public boolean onTouch(View view, MotionEvent event) {
                  final int Y = (int) event.getRawY();
                  switch (event.getAction() & MotionEvent.ACTION_MASK) {
                      case MotionEvent.ACTION_DOWN:
                          RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                          _yDelta = Y - lParams.bottomMargin;
                          break;
                      case MotionEvent.ACTION_UP:
                          break;
                      case MotionEvent.ACTION_POINTER_DOWN:
                          break;
                      case MotionEvent.ACTION_POINTER_UP:
                          break;
                      case MotionEvent.ACTION_MOVE:
                          RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                          layoutParams.bottomMargin = (Y - _yDelta);
                          layoutParams.topMargin = -layoutParams.bottomMargin;
                          view.setLayoutParams(layoutParams);
                          view.animate().translationY(Y - _yDelta).setDuration(0);
                          break;
                  }
                  findViewById(R.id.root).invalidate();
                  return true;
              }
          }
      

      And activity_main.xml:

      
          
          
          
      
      

    I hope, it helps!

    0 讨论(0)
提交回复
热议问题