Edittext keeps focus when keyboard is up and scroll list

I am using listview. Each item in the list contains one editor. When I click on edittext, a soft keyboard appears, the edittext that I clicked on quickly gains focus and then loses it. I have to click a second time to really get the focus (why?)

( focus on editext and keyboard up ) Scrolling through the list ...

Since the list items are redesigned, the focus reappears on the edittext in other positions in the list, and I assume that this is normal, BUT why doesn’t Android remove that damn focus ! How can I tell Android in order? I don't want to focus on edittext anymore if my user starts scrolling?

Here is the action code:

public class SimpleList extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user_picker); ListView listview = (ListView)this.findViewById(R.id.user_picker_list); new UserPickerAdapter(this, listview); } 

And adapter code:

 public class UserPickerAdapter extends BaseAdapter { private final LayoutInflater mInflater; private String[] mUsers = {"Guib", "Jonas", "Fred", "Denis", "Arnold", "Francois", "David", "Alex", "Saskia", "Verner", "Anatole"}; public UserPickerAdapter(Context context, ListView listview) { mInflater = LayoutInflater.from(context); listview.setAdapter(this); } @Override public int getCount() { return mUsers.length; } @Override public Object getItem(int position) { return mUsers[position]; } @Override public long getItemId(int position) { return position; } static private class Holder { TextView username; } @Override public View getView(int position, View convertView, ViewGroup arg2) { Holder holder; if (convertView != null) { holder = (Holder) convertView.getTag(); } else { holder = new Holder(); convertView = mInflater.inflate(R.layout.user_picker_list_item,null); holder.username = (TextView) convertView.findViewById(R.id.user_picker_list_item_name); convertView.setTag(holder); } String str = (String)getItem(position); holder.username.setText(str); return convertView; } 

Just in case, here is the code in 'user_picker.xml'

 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" android:padding="0dp"><ListView android:background="@color/color_grey1" android:cacheColorHint="@color/color_grey1" android:id="@+id/user_picker_list" android:divider="@color/color_blueGrey1" android:layout_height="match_parent" android:layout_width="match_parent"/> 

And the one that is in 'user_picker_list_item.xml'

 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent"><EditText android:layout_height="wrap_content"android:id="@+id/user_picker_list_item_name" android:layout_width="wrap_content" android:minWidth="200dp" android:layout_gravity="center"></EditText></LinearLayout> 
+4
source share
4 answers

I tried the Gilbou solution, but it did not work for me because I had EditTexts in all lines. But with some changes, I could make it work:

 EditText currentEdit; public View getView(int q, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView==null) { convertView = (LinearLayout)inflater.inflate(R.layout.grade_row, null); holder = new ViewHolder(); //Inflate holder convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } if (listView.getSelectedItemPosition() == ListView.INVALID_POSITION) { if (currentEdit!=null) currentEdit.requestFocus(); } //update row items return convertView; } 

Also, add a focus change listener for each EditText inside the list:

 myEditText.setOnFocusChangeListener(new OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus) currentEdit = myEditText; } } 
+1
source

I think I found a solution ...

I replaced part of the code in the getView () method of my adapter as follows:

 if (convertView != null) { holder = (Holder) convertView.getTag(); if (((ListView)arg2).getSelectedItemPosition() == ListView.INVALID_POSITION) { arg2.requestFocus(); } } 
0
source

You need to add

In XML

Android: descendantFocusability = "afterDescendants"

In java

listView.setDescendantFocusability (ViewGroup.FOCUS_AFTER_DESCENDANTS);

I think it works.

0
source

Maybe I'm late, but I hope this helps someone. Sangit Kumar's answer is on the right track, but he needs to add another line of code to the manifest.

The problem here is that when the soft keyboard pops up, the content on the screen is forced to scroll and resize. When the ListView (or ExpandableListView ) scrolls, all views are rendered again, so the object of the EditText field in the element row that used to be focused is now a completely different object.

Therefore, to solve this problem, you need to add two lines of code:

  1. Add the following line to the XML of the ListView:

    android:descendantFocusability="beforeDescendants"

  2. Add the following line to the activity tag containing your ListView (or ExpandableListView) in the application manifest

    android:windowSoftInputMode="adjustPan"

Hope this helps :)

0
source

Source: https://habr.com/ru/post/1334642/


All Articles