Keep focusing on TextView after notifiedDatasetChanged () is called in a custom listview?

I have a listview adapter which, when I change my TextView, call notifyDataSetChanged()on addTextChangeListener(). But my TextView loses focus. How can I maintain focus by overriding notifyDataSetChanged()?

I do it but did not work

@Override
public void notifyDataSetChanged(){
    TextView txtCurrentFocus = (TextView) getCurrentFocus();
    super.notifyDataSetChanged();
    txtCurrentFocus.requestFocus();
}
+4
source share
2 answers

You can extend the class ListViewand override the method requestLayout(). This method is called when ListViewthe update completes and steals focus. So, at the end of this method, you can return the focus to yours TextView.

public class ExampleListView extends ListView {

    private ListViewListener mListener;

    public ExampleListView(Context context) {
        super(context);
    }

    public ExampleListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExampleListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void requestLayout() {
        super.requestLayout();
        if (mListener != null) {
            mListener.onChangeFinished();
        }
    }

    public void setListener(ListViewListener listener) {
        mListener = listener;
    }

    public interface ListViewListener {
        void onChangeFinished();
    }
}

and set for this listener ListView

ExampleListView listView = (ExampleListView) view.findViewById(R.id.practice_exercises_list);
listView.setListener(new ExampleListView.ListViewListener() {
            @Override
            public void onChangeFinished() {
                txtCurrentFocus.requestFocus();
            }
        });
0

EditText RecyclerView , RecyclerView Adapter .

 adapter.setHasStableIds(true);
0

All Articles