简体   繁体   English

如何使布局从底部向上滑动?

[英]How to make layout slide up from bottom?

Click the button to get LinearLayout from the bottom.点击按钮从底部获取LinearLayout。 But there's a problem.但是有一个问题。

  1. After the layout is finished, the VideoView doesn't work with one click.布局完成后,VideoView 不能一键使用。 Works on the second click.在第二次点击时工作。

  2. I want to make the layout disappear if I touch anywhere other than the layout on top.如果我触摸顶部布局以外的任何地方,我想让布局消失。 (Of course, the VideoView at the rear should not work when clicked.) (当然,后面的 VideoView 在点击时应该不起作用。)

  3. If I click EditText, I want to place the EditText and the buttons on the soft keyboard.如果我单击 EditText,我想将 EditText 和按钮放在软键盘上。 (It is now obscured by the softkeyboard.) (它现在被软键盘遮住了。)

this is my code.这是我的代码。

Java code Java码

public class PlayActivity extends AppCompatActivity {

public void onCreate(){

  ...

videoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(videoView.isPlaying()){      
                    videoView.pause();
                    playimg.setVisibility(View.VISIBLE);
                }else{                          
                    videoView.start();
                    playimg.setVisibility(View.INVISIBLE);
                }
            }
        });

...

comm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(!CommShown()){

                    Animation bottomUp = AnimationUtils.loadAnimation(PlayActivity.this,R.anim.bottom_up);
                    comm_layout.startAnimation(bottomUp);
                    comm_layout.setVisibility(View.VISIBLE);
                    viewGroup.setEnabled(false);
                    //enableDisableViewGroup(viewGroup, false);
                }


            }
        });


        comm_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(CommShown()){
                        Animation bottomUp = AnimationUtils.loadAnimation(PlayActivity.this,R.anim.bottom_down);
                        comm_layout.startAnimation(bottomUp);
                        comm_layout.setVisibility(View.INVISIBLE);
                    enableDisableViewGroup(viewGroup, true);
                }
            }
        });

} // onCreate


    //comm Visible or Invisivle check

    private boolean CommShown() {
        return comm_layout.getVisibility() == View.VISIBLE;
    }

    //Comm Open and disable rear view
    public static void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
        int childCount = viewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {


            View view = viewGroup.getChildAt(i);
            view.setEnabled(enabled);

            if (view instanceof ViewGroup) {
                enableDisableViewGroup((ViewGroup) view, enabled);
            }
        }
   }
}

xml code xml代码


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/viewGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <VideoView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/playimg"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:visibility="invisible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/ic_media_play" />


            <ImageView
                android:id="@+id/comm"
                android:layout_width="50dp"
                android:layout_height="50dp"
                app:srcCompat="@android:drawable/sym_def_app_icon"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>


    <LinearLayout
        android:id="@+id/comm_layout"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:orientation="vertical"
        android:background="@color/white"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="30dp">

            <ImageButton
                android:id="@+id/comm_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/transparent"
                app:srcCompat="@android:drawable/btn_dialog"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="430dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/editText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="9"
                android:ems="10"
                android:inputType="textPersonName"
                android:hint="comment" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                app:srcCompat="@android:drawable/sym_def_app_icon" />
        </LinearLayout>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

anim动画

bottom_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0%p"
        android:fillAfter="true"
        android:duration="500" />
</set>


bottom_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%p"
        android:toYDelta="70%p"
        android:fillAfter="true"
        android:interpolator="@android:anim/linear_interpolator"
        android:duration="500" />
</set>

I am sorry that I am not good at English.对不起,我英语不好。 And thank you for your help.并感谢您的帮助。

For sliding a layout up from the bottom of the screen, you can use BottomSheet provided by Android.要从屏幕底部向上滑动布局,您可以使用 Android 提供的BottomSheet

And for keyboard hiding edit text in the bottom layout, you can check out this post here对于在底部布局中隐藏编辑文本的键盘,您可以在此处查看此帖子

I hope it helps!我希望它有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM