其他分享
首页 > 其他分享> > android – 在自定义列表视图上调用notifyDatasetChanged()后,继续关注TextView?

android – 在自定义列表视图上调用notifyDatasetChanged()后,继续关注TextView?

作者:互联网

我有一个listview适配器,当我修改我的TextView时,我在addTextChangeListener()方法上调用notifyDataSetChanged().但我的TextView失去了焦点.
如何保持焦点,覆盖notifyDataSetChanged()?

我这样做但没有奏效

@Override
public void notifyDataSetChanged(){
    TextView txtCurrentFocus = (TextView) getCurrentFocus();
    super.notifyDataSetChanged();
    txtCurrentFocus.requestFocus();
}

解决方法:

您可以扩展ListView类并覆盖requestLayout()方法.当ListView完成更新并窃取焦点时,将调用该方法.因此,在此方法的最后,您可以将焦点返回到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();
    }
}

并将侦听器设置为此ListView

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

标签:android,listview,android-arrayadapter,notifydatasetchanged
来源: https://codeday.me/bug/20190709/1407708.html