Android how to make EditText editable inside ListView

I have a ListView with edit text in each element, and I want to know how I can make an editable EditText . It seems to work fine if I set android:windowSoftInputMode="adjustPan" in the manifest, but only on some devices. On some devices, the soft keyboard closes the edittext.

On the other hand, if I do not put android:windowSoftInputMode="adjustPan" in the manifest, EditText loses focus after displaying the soft keyboard, and I have to touch EditText several times to select it and write in it.

Please help me solve this problem.

Note: I saw this post: Focused EditText inside the ListView , and nothing helped from it, and I have another problem,

+6
source share
2 answers

I made a workaround for this, maybe maybe help someone

 public class EditTextListAdapter extends BaseAdapter { /* notes list */ private ArrayList<SomeData> mSomeData = null; /* layout inflater instance */ private LayoutInflater mInflater; /* activity context */ private Context mContext; /* ensure that this constant is greater than the maximum list size */ private static final int DEFAULT_ID_VALUE = -1; /* used to keep the note edit text row id within the list */ private int mNoteId = DEFAULT_ID_VALUE; /** * EditTextListAdapter adapter class constructor * * @param context activity context */ public FirstTimesListAdapter(Context context) { /* get a layout inflater instance */ mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); /* load the data */ mSomeData = SomeData.instance().getData(); /* keep the context */ mContext = context; } /** * Returns the selected item * * @return selected item */ public int getSelectedItem() { return mSelectedItem; } public int getCount() { return mSomeData.size(); } public Object getItem(int i) { return mSomeData.get(i); } public long getItemId(int i) { return i; } public View getView(final int index, View recycledView, ViewGroup viewGroup) { ViewHolder viewHolder; if (recycledView == null) { /* inflate the list item */ recycledView = mInflater.inflate(R.layout.listview_item_with_edit_text, viewGroup, false); /* get link to the view holder views */ viewHolder = new ViewHolder(); viewHolder.note = (EditText) recycledView.findViewById(R.id.first_times_note); recycledView.setTag(viewHolder); } else { /* reuse the same views we load the first time */ viewHolder = (ViewHolder) recycledView.getTag(); } /* display some notes */ viewHolder.note.setText(mSomeData.getNotes()); /* if the last id is set, the edit text from this list item was pressed */ if (mNoteId == index) { /* make the edit text recive focus */ viewHolder.note.requestFocusFromTouch(); /* make the edit text cursor to appear at the end of the text */ viewHolder.note.setSelection(viewHolder.note.getText().length()); /* reset the last id to default value */ mNoteId = DEFAULT_ID_VALUE; } /* set a touch listener on the edit text just to record the index of the edit text that was pressed */ viewHolder.note.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { /* get the index of the touched list item */ mNoteId = index; } return false; } }); return recycledView; } static class ViewHolder { /* note input */ EditText note; } } 
0
source

After an unsuccessful attempt to create an interface with EditTexts inside a ListView , all I can tell you is "Do not!". You will have more problems when you have enough items that you need to scroll through the ListView , the focus will jump here and there, you need to save the EditText state and so on. It seems like the general idea is that using EditText in a ListView not worth it.

After a little research, I can suggest the following method that helped me: I inherited the ListView and redefined the layoutChildren method, inside it I do the following:

 @Override protected void layoutChildren() { super.layoutChildren(); final EditText tv = (EditText)this.getTag(); if (tv != null) { Log.d("RUN", "posting delayed"); this.post(new Runnable() { @Override public void run() { Log.d("RUN", "requesting focus in runnable"); tv.requestFocusFromTouch(); tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , tv.getWidth(), tv.getHeight(), 0)); tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , tv.getWidth(), tv.getHeight(), 0)); } }); } } 

When I know which EditText should get focus (I know this when my getView adapters are called), I set this particular EditText as a ListView tag. Then the ListView pops up, and my post column is queued. It starts and asks for focus, however, since this was not enough in my case, I also create two MotionEvents that simply simulate taps. Apparently, this is enough to display a soft keyboard. The reasons for this are explained in the answer here: Android action bar tabs and keyboard focus

+4
source

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


All Articles