Skip to content

How to focus on last row #186

Description

@laatahzaan

Select last a row:

public void selectLastRow() {
    if (tableDataView.getCount() > 0) {
        int rowIndex = tableDataView.getCount() - 1;
        AdapterView<?> parent = tableDataView;
        View view = new LinearLayout(tableDataAdapter.getContext());
        view.setLayoutParams(tableDataView.getLayoutParams());
        InternalDataLongClickListener longClickListener = new InternalDataLongClickListener();
        longClickListener.onItemLongClick(parent, view, rowIndex, rowIndex);
        forceRefresh();
        tableDataView.setSelection(rowIndex);
    }
}

Add a new row:

bankDataAdapter.add(new Bank(bankLastID, ""));
bankDataAdapter.notifyDataSetChanged();

Focus on the the new row:

bankTableView.selectLastRow();

The result is not expected, lost the blink cursor on last row when selected:
https://drive.google.com/open?id=1beJ9QYTQu1Qe3iCqDKcRWcJ_-1IIZCAU

Expected result:
https://drive.google.com/open?id=1D2ncW-T7gO8rny_2IDZTeCyWI1hS7DSH

How to resolved my problems?

Thanks.

Activity

  1. ISchwarz23 commented on Sep 9, 2019

    @ISchwarz23
    Owner

    Hi,

    have you tried to get the longClickListener from the tableDataView instead of creating a new one?

    OnLongClickListener longClickListener = tableDataView.getOnLongClickListener();
    if( longClickListener != null ) {
        longClickListener.onItemLongClick( tableDataView, view, index, id)
    }
    
  2. laatahzaan01 commented on Sep 11, 2019

    @laatahzaan01

    I have solved my problem. I forgot, that the focus is not given to item of the listview, but to editext. Thank you for the help.

    public void focusToEditText(ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            final View child = parent.getChildAt(i);
            if (child instanceof ViewGroup) {
                focusToEditText((ViewGroup) child);
            } else {
                if (child != null) {
                    if(child instanceof EditText) {
                        EditText editText = (EditText) child;
                        editText.setFocusable(true);
                        editText.requestFocus();
                    }
                }
            }
        }
    }
    
    public void enterEditMode() {
        if (tableDataView.getCount() > 0) {
            int rowNumber = tableDataView.getCount() - 1;
            tableDataView.setSelectionFromTop(rowNumber, 0);
            tableDataView.invalidate();
    
            new Handler().postDelayed(() -> {
                View rowView = tableDataAdapter.getView(rowNumber, null, tableDataView);
                AdapterView.OnItemLongClickListener onItemLongClickListener = tableDataView.getOnItemLongClickListener();
                if (onItemLongClickListener != null) {
                    onItemLongClickListener.onItemLongClick(tableDataView, rowView, rowNumber, rowNumber);
                }
    
                new Handler().postDelayed(() -> {
                    focusToEditText(tableDataView);
                }, 250);
    
            }, 250);
        }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions