Android: ListView reverses the items when soft
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.
Can anyone help me?
This is the layout:
<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>
This is the custom adapter:
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>
EDIT: 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: 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?
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;
}
链接地址: http://www.djcxy.com/p/93426.html
上一篇: 推进布局或隐藏操作栏的软键盘