ISchwarz23 / SortableTableView

An Android library containing a simple TableView and an advanced SortableTableView providing a lot of customisation possibilities to fit all needs.
Apache License 2.0
1.05k stars 239 forks source link

How to focus on last row #186

Open laatahzaan opened 5 years ago

laatahzaan commented 5 years ago

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.

ISchwarz23 commented 5 years ago

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)
}
laatahzaan01 commented 5 years ago

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);
    }
}