Edittext losing focus in ListView header

0

I put header on my ListView because I wanted it to roll along with it. In this header there are two EditText , when I click on the second, the focus comes back first in less than one second. You do not even have time to write anything in the second field, and every time I click on it, does anyone have any idea what it is? (I took the general activity code to get 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 the config.xml there is only the ListView and in the Adapter there is nothing related to the 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>
    
asked by anonymous 16.02.2018 / 21:31

1 answer

1

What is probably happening is that when you click on the ListView (either the click to scroll or the click actually), his observable, which is underneath the cloths, will do that whole process to warn all the items who are under his control that "something happened". This "warning that something happened" causes everything to be recharged. And that's the problem. Everything will return to default, which in your case is the first EditText with focus.

What I would do would be to remove those editText files from there. There is a way to do this that you want with the AppBarLayout and CoordinatorLayout, using the layout_scrollFlags attribute.

An example of activitiy would be: Where, container would commit a fragment 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: link

link

Good Luck!

    
18.02.2018 / 06:18