Edittext losing focus on Listview header

Asked

Viewed 87 times

0

I put a header in my ListView because I wanted him to roll along with her. In this header there are two EditText, When I click on the second, the focus goes back to the first one in less than a second. There’s not even time to write anything in the second field, and whenever I click this, does anyone have any idea what it is? (I took the general code from Activity to make it smaller)

class ConfigActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.config)

        var header = layoutInflater.inflate(R.layout.config_header, null)
        lvOptions.addHeaderView(header)

        var adapter = OptionsAdapter(this, ArrayList<Option>()) //só pra criar o adapter da listview, vazia mesmo, só com o header
        lvOptions.adapter = adapter
    }
}

here config_header.xml (In config.xml you only have Listview and in Adapter you have nothing relating to Header)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText
        android:id="@+id/eTNome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Nome"
        android:inputType="textPersonName"
        android:maxLines="1" />
    <EditText
        android:id="@+id/eTRendaM"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Renda mensal"
        android:inputType="number"
        android:maxLines="1" />
</LinearLayout>

1 answer

1


What is probably happening is that when you click on Listview (whether it’s the scroll click or the actual click), its observable, which is under the covers, will do that whole process to warn all the items that are under its control that "something happened". This "warning that something happened" causes everything to be reloaded. And there’s the problem. Everything will go back to default, which in your case is the first Edittext with focus.

What I would do is get these editText out of there. There is a way to do this that you want with Appbarlayout and Coordinatorlayout, using the attribute layout_scrollFlags.

An example of activitiy would be: Where, container would be commited a Ragment that has its Listview.

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                app:layout_scrollFlags="algumaFlagAqui">

            <!-- Seus dois editText vão aqui dentro desse layout -->

            </FrameLayout>

        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        </FrameLayout>

</android.support.design.widget.CoordinatorLayout>

These links can help you see the solution: https://stackoverflow.com/questions/31328695/what-is-the-difference-between-appbarlayout-vs-toolbar

http://karthikraj.net/2016/12/24/scrolling-behavior-for-appbars-in-android/

Good Luck!

  • In case I put my Edittext as a "Toolbar" that would "hide" when the listview der scroll down and appear again when scroll up?

  • 1

    Exactly that. Probably the flag you will use will be the app:layout_scrollFlags="scroll|enterAlways". But there are some others that might suit your case.

  • Are you sure Listview works with Appbarlayout? tried here and it didn’t work, also tried with Scrollview and did not, already with Nestedscrollview worked, I think it should only work with Recyclerview instead of Listview, I will try to implement it

  • It does. But since you’re using Recyclerview, this example here is good: https://mobikul.com/hideshow-toolbar-scrolling/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.