【问题标题】:Hide view when draggin up向上拖动时隐藏视图
【发布时间】:2014-09-26 18:20:33
【问题描述】:

我有一个带有 2 个 LinearLayout 的布局。第一个用作包含图表的容器,第二个包含几个按钮。

当应用程序启动时,包含图形的 LinearLayout1 首先是隐藏的View.GONE

然后,当我从 LinearLayout2 中触摸一个按钮时,此布局会使用平移动画返回其原始位置。

最后,我应该能够再次隐藏 LinearLayout1。我想通过向上拖动 LinearLayout2 来实现这一点,因此当用户将 LinearLayout2 向上移动一点时,LinearLayouy1 将再次被View.GONE 隐藏。

最后一部分是我需要帮助的部分。我已经尝试过使用OnTochListener() 的东西,但我并没有为此做太多工作,我不知道该怎么做。这是我这样做的代码片段:

/*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;
    }
});

根据我触摸的位置进行移动,我可以将可见性设置为 GONE,但移动不是我想要的,我无法移动 LinearLayout2。

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    通过这种方式,您可以向上“推动”layout2 并在某个时候隐藏 layout1(使用动画,或者也推动 layout1)。希望这会有所帮助。

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

    顺便说一句-animations transitions 是当视图的参数需要更改时更好的方法。这不是您问题的答案,因为您想要真正的“拖拽”感觉(上升时)。 还有——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>
    

    以及对应的activity如下:

        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;
                }
            });
        }
    }
    

    【讨论】:

    • 好吧,我不知道该怎么说,但你做到了,我真正需要的是向上推动布局 2,并在某个时候隐藏布局 1,将其设置为 GONE 的可见性。如果你能提供一个例子,我会接受你的回答
    • 已编辑。请注意,我实际上并没有隐藏 layout1(这是可能的),也没有将其向上推(也可能) - 但这应该足以让您开始。
    【解决方案2】:

    使用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;
        }
    });
    

    【讨论】:

    • 我用你的方法改变了我的方法,但是 Linearlayout2 没有移动,linearLayout1 也没有移动
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    相关资源
    最近更新 更多