Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to focus on last row #186

Open
laatahzaan opened this issue Sep 8, 2019 · 2 comments
Open

How to focus on last row #186

laatahzaan opened this issue Sep 8, 2019 · 2 comments

Comments

@laatahzaan
Copy link

laatahzaan commented Sep 8, 2019

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
Copy link
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)
}

@laatahzaan01
Copy link

laatahzaan01 commented Sep 11, 2019

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants