Listview with edittext - auto scroll on “next”

后端 未结 2 430
走了就别回头了
走了就别回头了 2020-12-20 14:39

I have a ListView with one EditText on each row (in addition to a couple of non-editable TextView\'s). When I\'m editing the text in the EditText, the soft keyboard has \"N

相关标签:
2条回答
  • 2020-12-20 15:31

    Ok, after struggling for a long time, I finally found a hack (not a proper solution) that works for my case. In the getView of my adapter, I add the onEditorActionListener and inside it:

    ediField.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            ListView lv = (ListView)parent;
            if(actionId == EditorInfo.IME_ACTION_NEXT &&
               lv != null &&
               position >= lv.getLastVisiblePosition() &&
               position != audit.size() - 1) {  //audit object holds the data for the adapter
                    lv.smoothScrollToPosition(position + 1);
                    lv.postDelayed(new Runnable() {
                        public void run() {
                            TextView nextField = (TextView)holderf.qtyf.focusSearch(View.FOCUS_DOWN);
                            if(nextField != null) {
                                nextField.requestFocus();
                            }
                        }
                    }, 200);
                    return true;
            }
            return false;
        }
    });
    
    0 讨论(0)
  • 2020-12-20 15:42

    I have no idea about holder, so I found a way to do without it. Inside getView add onEditorActionListener.

    EditText editView = (EditText)convertView;
    editView.editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(final TextView view, int actionId, KeyEvent event){
               if(actionId == EditorInfo.IME_ACTION_NEXT){
                int lastPos = lv.getLastVisiblePosition();
                lv.smoothScrollToPosition(position + 1);
                if(position >= lastPos){
                    lv.postDelayed(new Runnable(){
                       @Override
                        public void run(){
                             final int posTop = lv.getFirstVisiblePosition();
                             TextView nextView = (EditText)lv.getChildAt(position - posTop + 1);
                             nextView.requestFocus();
                        }
                     }, 200);
                }
                //Delay is not required
                else{              
                    final int posTop = lv.getFirstVisiblePosition();
                    TextView nextView = (EditText)lv.getChildAt(position - posTop + 1);
                    nextView.requestFocus();
                }
              }
          }
    });
    
    0 讨论(0)
提交回复
热议问题