Show last item in ListiView when you click on edit text

前端 未结 4 523
臣服心动
臣服心动 2021-01-01 01:07

currently I\'m doing this to get the focus of the last item in a listview after clicking on an edit text:

    bodyText.setOnFocusChangeListener(new OnFocusCh         


        
4条回答
  •  情深已故
    2021-01-01 01:19

    I made my own Custom ListView like this:

    public class CustomListView extends ListView {
    
    public CustomListView (Context context) {
        super(context);
    }
    
    public CustomListView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomListView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
        super.onSizeChanged(xNew, yNew, xOld, yOld);
    
        setSelection(getCount());
    
    }
    }
    

    You must include all 3 constructors or an exception will be thrown.

    Then in the XML where you usually put

    ListView

    android:id="@+id/android:list"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    

    Change ListView to com.blah.blah.CustomListView.

    Sorry for the messy layout, for some reason I can't get the formatter to properly work.

    Run your application. Now when you click on the EditText, it shows the last item AFTER the soft keyboard shows! Note that there are some limitations like when the auto-complete function appears when you type text in the EditText, it will show the last item as well due to a change in size of the ListView.

    Enjoy!

提交回复
热议问题