在notifyDataSetChanged()之后焦点丢失

时间:2013-10-26 15:21:27

标签: android listview android-listview focus

我有一个listview,每行包含一个edittext。当我在listview的最后一行添加文本时,我想在listview中添加一个空行。所以我改变了我的列表并调用notifyDataSetChanged()。不幸的是,焦点转移到listview之外的地方。如何将焦点保持在当前的edit_text中?我尝试过edit_text.requestFocus() - 但它没有用。

代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView=inflater.inflate(R.layout.design_listview, parent, false);

    EditText edit_text=(EditText) rowView.findViewById(R.id.list_edittext);

    edit_text.setText(values.get(position).text);

    edit_text.addTextChangedListener(new TextWatcher()
    {
        public void beforeTextChanged(CharSequence charSequence, int i, int j, int k) { }

        public void onTextChanged(CharSequence charSequence, int i, int j, int k) { }

        public void afterTextChanged(Editable editable)
        {
            values.get(position).text=editable.toString();

            if(position == values.size()-1 && editable.toString().length() > 0) {
                listaddItem();
                notifyDataSetChanged();
                // How to keep the focus? edit_text.requestFocus(); did not help
            }
        }
        // ..
    return rowView;
    }
}

THX!

1 个答案:

答案 0 :(得分:0)

也许是edit_text.setSelection(0);作品?