不同的"能见度和QUOT;在ListView值在每一行中 [英] Different "visibility" values in each row in a ListView

查看:94
本文介绍了不同的"能见度和QUOT;在ListView值在每一行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了使用自定义适配器一个ListView一个非常简单的应用程序。

I have developed a very simple app that uses a custom adapter for a ListView.

每行有两个TextViews:

Each row has two TextViews:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:visibility="gone" />
</LinearLayout>

第一TextView的被命名为文本1,而第二个text2中。
正如你看到的,文本2是隐藏的(知名度=水涨船高)

此外,该名单有一个只包含一个EditText小部件的标题:

Also, the list has a header that only contains an EditText widget:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

每个行再由名为项有2个属性(文本1和文本),一个非常简单的对象,它的getter / setter方法​​psented $ P $。此外,它有一个hasText2的方法,只是检查,如果该对象具有文本2属性值的长度> 0

Each row is represented by a very simple object named "Item" that has 2 properties (text1 and text2), and its getters/setters. Also, it has a "hasText2" method that just checks if the object has the text2 property value's length > 0:

public class Item {
    private String text1;
    private String text2;

    public String getText1() {
        return text1;
    }
    public void setText1(String text1) {
        this.text1 = text1;
    }
    public boolean hasText2() {
        return text2.length() > 0;
    }
    public String getText2() {
        return text2;
    }
    public void setText2(String text2) {
        this.text2 = text2;
    }
}

好吧,我会在我的主要的应用程序文件初始化仅有2项的列表:

Okay, I'll initialize the list with just 2 items in my main app file:

Item item1 = new Item();
item1.setText1("Item 1");
item1.setText2("optional 1");
Item item2 = new Item();
item2.setText1("Item 2");
item2.setText2("");

getListView().addHeaderView(getLayoutInflater().inflate(R.layout.list_header, null), false, false);

m_items = new ArrayList<Item>();
m_adapter = new CustomListAdapter(this, R.layout.list_row, m_items);
setListAdapter(m_adapter);

m_items.add(item1);
m_items.add(item2);
m_adapter.notifyDataSetChanged();

这是我的自定义适配器的getView方法(扩展ArrayAdapter):

This is the getView method of my custom adapter (that extends ArrayAdapter):

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.list_row, null);
    }

    // get the two text widgets of this row layout
    TextView text1 = (TextView) convertView.findViewById(R.id.text1);
    TextView text2 = (TextView) convertView.findViewById(R.id.text2);

    // get the Item object of this row
    Item list_item = items.get(position);

    // we set the text1 property of this Item to the text1 widget
    text1.setText(list_item.getText1());

    // if this Item has a text2 (value length > 0), then set it to the text2 widget and make it visible 
    if (list_item.hasText2()) {
        text2.setText(list_item.getText2());
        text2.setVisibility(0);
    }

    return convertView;
}

所以,我要的是显示只在项目对象已为其定义的文本2小部件(值的长度> 0)

这是运行应用程序后的结果:

And this is the result after running the app:

这是很好的,它只是如我所料! :)

That's good, it works just as I expected!! :)

但是,如果我点击一下列表标题的EditText上? (所以我强迫更新列表):

But what if I tap the EditText of the list header? (so I force to update the list):

这里发生了什么?这不可能。第二排已没有text2的定义,和Android刚刚已采取文本2从第一行!为什么?

What happened here? That's impossible. The second row has no text2 defined, and Android just has taken the text2 from the first row! Why??

我能想象到的唯一原因是,我不能使用不同的可见性的行...但随后,为什么Android的让我做时,​​我只是运行应用程序?看来,只有当键盘出现(列表更新)失败。

The only reason I could imagine is that I cannot use rows with different visibility... but then, why Android let me do it when I just run the app? It seems to fail only when the keyboard appears (list update).

推荐答案

这对我在笔记上的previous问题描述的确切同样的原因的问题。该视图被回收,所以在从句中进行定制如果(list_item.hasText2()){永久在该视图设置;即使在回收到的这该条款不会是真正的美景。

This has a problem for the exact same reason I described in notes on your previous question. The view is being recycled, so the customization performed in the clause if (list_item.hasText2()) { is permanently set on that view; even when recycled to a view for which that clause would not be true.

在这种情况下,以下修改可能会解决这个问题:

In this case the following modification would probably fix the issue:

 if (list_item.hasText2()) {
    text2.setText(list_item.getText2());
    text2.setVisibility(View.VISIBLE);
 } else {
    text2.setVisibility(View.GONE);
 }

这篇关于不同的&QUOT;能见度和QUOT;在ListView值在每一行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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