安卓:ListView的反转项目时,软键盘出现 [英] Android: ListView reverses the items when soft-keyboard appears

查看:259
本文介绍了安卓:ListView的反转项目时,软键盘出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框和两个EditTexts一个自定义的ListView。一个奇怪的事情发生了,当我点击的EditText和出现软键盘:在ListView的项目反转原来的顺序。当我调试,我看到自定义ArrayAdapter的getView()软键盘出现时被调用。其次,我认为ListView控件重绘每次出现软键盘。这没关系,但为什么它颠倒项目的顺序?
还有一件事需要说的是,当我点击第一个时间的的EditText失去焦点。我第二次单击One的EditText,没有什么奇怪的事情发生,但在ListView继续反转。驳回软键盘后,ListView控件又回到原来的顺序。
我已经把机器人:可调焦=造假问题继续发生

I have a custom ListView with a CheckBox and two EditTexts. A strange thing happens when I click a EditText and the soft-keyboard appears: the items in the ListView reverses the original order. When I debugged, I saw that getView() of the custom ArrayAdapter is called when the soft-keyboard appears. Then I think the ListView is redrawn everytime the soft-keyboard appears. It's ok, but why do it reverses the order of items? One more thing a need to say is that the focus is lost when I click one EditText at first time. Second time I click one EditText, nothing strange happens, but the ListView continues inverted. After dismissing the soft-keyboard, the ListView comes back to the original order. I already put the android:focusable="false" and the problem continues happening.

谁能帮我?

这是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <TextView
            android:id="@+id/tv_nome_lista"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="@string/nome_lista_compra" />

        <TextView
            android:id="@+id/tv_local_lista"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tv_nome_lista"
            android:text="@string/local_lista_compra" />

    </RelativeLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:focusable="false" >
    </ListView>

    <TextView
        android:id="@+id/tv_preco_total_lista"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/preco_total_lista" />

</LinearLayout>

这是自定义适配器:

私有类ProdutosAdapter扩展ArrayAdapter {

private class ProdutosAdapter extends ArrayAdapter {

    public ProdutosAdapter() {
        super(CompraActivity.this, R.layout.compra_item, lista.getProdutos());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.compra_item, parent, false);


            final Produto produto = CompraActivity.this.lista.getProdutos().get(position);

            CheckBox cbCompradoProduto = (CheckBox) convertView.findViewById(R.id.cb_comprado_produto);
            TextView tvNomeProduto = (TextView) convertView.findViewById(R.id.tv_nome_produto);
            EditText etQuantidadeProduto = (EditText) convertView.findViewById(R.id.et_quantidade_produto);
            EditText etPrecoProduto = (EditText) convertView.findViewById(R.id.et_preco_produto);
            etPrecoProduto.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(CompraActivity.this.currencyFormat.getCurrency().getDefaultFractionDigits())});

            //TextView tvVezes = (TextView) convertView.findViewById(R.id.tv_vezes);
            //tvVezes.setText(getString(R.string.vezes) + " " + currencyFormat.getCurrency().getSymbol());

            cbCompradoProduto.setChecked(produto.isComprado());
            tvNomeProduto.setText(produto.getNome());
            etQuantidadeProduto.setText(produto.getQuantidade().toString());
            etPrecoProduto.setText(decimalFormat.format(produto.getPreco()));

            /*cbCompradoProduto.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    produto.setComprado(isChecked);
                    atualizarPrecoTotal();
                    produtosModificados.add(produto);
                }
            });

            etQuantidadeProduto.addTextChangedListener(new TextWatcher(){
                public void afterTextChanged(Editable s) {
                    if(!s.toString().equals("")){
                        produto.setQuantidade(Integer.parseInt(s.toString()));
                    }
                    atualizarPrecoTotal();
                    produtosModificados.add(produto);
                }
                public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                public void onTextChanged(CharSequence s, int start, int before, int count){}
            });

            etPrecoProduto.addTextChangedListener(new TextWatcher(){
                public void afterTextChanged(Editable s) {
                    try {
                        produto.setPreco(decimalFormat.parse(s.toString()).doubleValue());
                    } catch (ParseException e) {

                    }
                    atualizarPrecoTotal();
                    produtosModificados.add(produto);
                }
                public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                public void onTextChanged(CharSequence s, int start, int before, int count){}
            });

            etPrecoProduto.setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(!hasFocus){
                        ((EditText)v).setText(decimalFormat.format(produto.getPreco()));
                    }
                }
            });*/
        }
        return convertView;
    }

这是该项目的布局:

And this is the item's layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <CheckBox
        android:id="@+id/cb_comprado_produto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/tv_nome_produto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/cb_comprado_produto"
        android:layout_alignBottom="@+id/cb_comprado_produto"
        android:layout_toRightOf="@+id/cb_comprado_produto"
        android:text="@string/nome_produto" />

    <EditText
        android:id="@+id/et_preco_produto"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tv_nome_produto"
        android:layout_alignBottom="@+id/tv_nome_produto"
        android:layout_alignParentRight="true"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/tv_vezes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/et_preco_produto"
        android:layout_alignBottom="@+id/et_preco_produto"
        android:layout_toLeftOf="@+id/et_preco_produto"
        android:text="@string/vezes" />

    <EditText
        android:id="@+id/et_quantidade_produto"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tv_nome_produto"
        android:layout_alignBottom="@+id/tv_nome_produto"
        android:layout_toLeftOf="@+id/tv_vezes"
        android:inputType="number" />

</RelativeLayout>

编辑:
我把的EditText列表视图之外,点击它来测试和同样的问题发生。然后,软键盘出现时,列表视图重绘反转。

I put a EditText outside the listview and clicked it to test and the same problem happens. Then, when the soft-keyboard appears, the listview is redrawn inverted.

EDIT2:另一个测试后,我发现,在ListView正反转的顺序时,它会调整。把机器人:在清单windowSoftInputMode =adjustPan使得它工作正常,但它不会调整大小。不要任何人有,为什么是在调整时,为了改变一些想法?

After another tests, I discovered that the listview is reversing the order when it resizes. Putting android:windowSoftInputMode="adjustPan" in the manifest makes it works fine, but it doesn't adjust the size. Do anyone have some idea about why is the order changing when resizing?

推荐答案

这似乎在从适配器类乌尔getView方法的错误,你是不是想在将您textview.setText()如果( convertView == NULL)块。试试这个。

It seems to have a error in ur getView method from adapter class, you are not suppose to place your textview.setText() within the if(convertView==null) block. Try this.

    @Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.compra_item, parent, false);
    }

    final Produto produto = CompraActivity.this.lista.getProdutos().get(position);

    CheckBox cbCompradoProduto = (CheckBox) convertView.findViewById(R.id.cb_comprado_produto);
    TextView tvNomeProduto = (TextView) convertView.findViewById(R.id.tv_nome_produto);
    EditText etQuantidadeProduto = (EditText) convertView.findViewById(R.id.et_quantidade_produto);
    EditText etPrecoProduto = (EditText) convertView.findViewById(R.id.et_preco_produto);
    etPrecoProduto.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(CompraActivity.this.currencyFormat.getCurrency().getDefaultFractionDigits())});

    //TextView tvVezes = (TextView) convertView.findViewById(R.id.tv_vezes);
    //tvVezes.setText(getString(R.string.vezes) + " " + currencyFormat.getCurrency().getSymbol());

    cbCompradoProduto.setChecked(produto.isComprado());
    tvNomeProduto.setText(produto.getNome());
    etQuantidadeProduto.setText(produto.getQuantidade().toString());
    etPrecoProduto.setText(decimalFormat.format(produto.getPreco()));

    return convertView;
}

这篇关于安卓:ListView的反转项目时,软键盘出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆