[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard

at first

RecyclerView is convenient for displaying a list, but if you leave it as the default, you will not be able to fire an event when you touch the margin. I sought an implementation to close the soft keyboard by touching the margin of RecyclerView, so I would like to share it.

32aeddc0c59703301a8fc30cf57374b6.gif

Implementation method

First to RecyclerView

android:touchscreenBlocksFocus="true"

Will be added.

fragment.xml


<LinearLayout
    android:id="@+id/scrollView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:isScrollContainer="false"
    app:layout_constraintBottom_toTopOf="@+id/footerBorder"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/headerBorder">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:touchscreenBlocksFocus="true"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</LinearLayout>

All you have to do now is implement the listener with onCreateView or onCreatedView.

Fragment.java


RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //Hide keyboard
        InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        //Move focus to background
        recyclerView.requestFocus();
        return false;
    }
});

that's all.

I hope it will be helpful to anyone.

Recommended Posts

[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
The story of forgetting to close a file in Java and failing
[Java] Get the dates of the past Monday and Sunday in order
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 2]
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 1]
[Android 9.0 Pie Java] Implement horizontal swipe → dialog display → delete in chat application
What wasn't fair use in the diversion of Java APIs on Android
This and that of the implementation of date judgment within the period in Java
Find the maximum and minimum of the five numbers you entered in Java
Get the result of POST in Java
Folding and unfolding the contents of the Recyclerview
The story of writing Java in Emacs
Java to C and C to Java in Android Studio
Discrimination of Enums in Java 7 and above
I received the data of the journey (diary application) in Java and visualized it # 001
Summarize the life cycle of Java objects to be aware of in Android development
Impressions and doubts about using java for the first time in Android Studio
Regarding the transient modifier and serialization in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
Feel the passage of time even in Java
Represents "next day" and "previous day" in Java / Android
Import files of the same hierarchy in Java
[Android development] Get an image from the server in Java and set it in ImageView! !!
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
[Android / Java] Screen transition and return processing in fragments
Write ABNF in Java and pass the email address
Please note the division (division) of java kotlin Int and Int
[For beginners] DI ~ The basics of DI and DI in Spring ~
The comparison of enums is ==, and equals is good [Java]
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
[Java] Get the file in the jar regardless of the environment
Java classes and instances to understand in the figure
I summarized the types and basics of Java exceptions
Change the storage quality of JPEG images in Java
Use of Abstract Class and Interface properly in Java
Summary of how to implement default arguments in Java
I tried to implement the Euclidean algorithm in Java
Summarize the additional elements of the Optional class in Java 9
[Android] Hook the tap of the anchor link in WebViewClient
A program (Java) that outputs the sum of odd and even numbers in an array
Shout Java at the heart of technology-Themes and elemental technologies that Java engineers should pursue in 2017-