2014-09-26 78 views
0

我有一个2 LinearLayout的布局。第一个用作包含图形的容器,第二个包含几个按钮。隐藏视图draggin up

1

当应用程序启动时,在第一个实例,其包含图形的LinearLayout1隐藏View.GONE

2

后来,当我从LinearLayout2轻按一键,使用翻译动画这样的布局可以追溯到它原来的地方。

3

最后,我不得不再次隐藏LinearLayout1的能力。我想通过拖动LinearLayout2实现此目的,所以当用户向上移动LinearLayout2时,LinearLayouy1将再次被View.GONE隐藏。

4

最后一部分是我需要一些帮助的人。我尝试了一些使用OnTochListener()的东西,但我没有太多工作,我不知道如何去做。这是代码snnipet,我这样做:

/*Layout views*/ 
private View graphContainer; //This is the LinearLayout1 
private View valuesContainer; //This is the LinearLayout2 
private float oldY; 
private float newY; 

... 

valuesContainer.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       float y = event.getY(); 
       oldY = y; 
       break; 
      case MotionEvent.ACTION_MOVE: 
       float y2 = event.getRawY(); 
       newY = y2; 
       if (oldY < newY) { 
        graphContainer.setVisibility(View.GONE); 
       } 
       break; 
     } 
     return true; 
    } 
}); 

不同的地方我触摸到做运动,我得到设置知名度了,但动作并不像我想,我没有得到移动LinearLayout2。

回答

1

上面所做的是当用户将手指向上移动时隐藏layout2。

你说“你不能移动LinearLayout2” - >为了移动一个视图,你需要更新它的LayoutParams。你可以在这里看到一个例子:How to move a view in Android?

这样你可以“推”layout2,并在某些时候隐藏layout1(使用动画,或者推layout1)。希望这可以帮助。

编辑(所请求的代码示例):

BTW - animations transitions是更好的办法时,视图的PARAMS需要改变去。这不是你的问题的答案,因为你想要一个真正的“拖动”感觉(当上升)。 此外 - android L has some beautiful animations(我还没有使用),我们应该留意。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MyActivity"> 

    <LinearLayout 
     android:id="@+id/first_layout" 
     android:background="@android:color/darker_gray" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/second_layout" 
     android:background="@android:color/holo_green_dark" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" > 
    </LinearLayout> 
</LinearLayout> 

和相应的活动如下:

public class MyActivity extends Activity { 

    int yDown = 0; 
    int initialTopMargin = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 

     LinearLayout layout2 = (LinearLayout)findViewById(R.id.second_layout); 

     layout2.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent event) 
      { 
       LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 

       switch (event.getAction()) 
       { 
        case MotionEvent.ACTION_MOVE: 
         params.topMargin = initialTopMargin - (yDown - (int)event.getRawY()); 
         view.setLayoutParams(params); 
         break; 

        case MotionEvent.ACTION_DOWN: 
         yDown = (int)event.getRawY(); 
         initialTopMargin = params.topMargin; 
         break; 
       } 

       return true; 
      } 
     }); 
    } 
} 
+0

嗯,我不知道该怎么说,但是你做到了,我真正需要的是将布局2向上推,并在某个时刻隐藏layout1,将其设置为GONE的可见性。如果您可以提供一个示例,我会接受您编辑的答案 – masmic 2014-09-26 08:57:09

+0

。请注意,我实际上并不隐藏layout1(这是可能的),也不会将其推高(也可能) - 但这应该足以让您开始。 – 2014-09-26 10:09:39

0

如何使用onDragListener

valuesContainer.setOnDragListener(new OnDragListener() { 
    @Override 
    public boolean onDrag(View v, MotionEvent event) { 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       float y = event.getY(); 
       oldY = y; 
       break; 
      case MotionEvent.ACTION_MOVE: 
       float y2 = event.getRawY(); 
       newY = y2; 
       if (oldY < newY) { 
        graphContainer.setVisibility(View.GONE); 
       } 
       break; 
     } 
     return true; 
    } 
}); 
+0

我改变了我的方法与你,但Linearlayout2不动既不linearLayout1使用布局如下

所以... get的GONE – masmic 2014-09-26 08:49:10