的ListView ArrayAdapter,躲在里面排的孩子吗? [英] ListView ArrayAdapter, hiding children inside Row?

查看:134
本文介绍了的ListView ArrayAdapter,躲在里面排的孩子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得有点愚蠢,因为我无法找到这个问题的答案,这让我觉得我其实要求的错误的问题。然而,这里去...

I feel a bit stupid as i can't find the answer to this question, which makes me think i'm actually asking the wrong question. However, here goes...

我有一个列表视图,并在XML定义的ListViewItem的,与一对夫妇领域,没什么特别的。所有设置为可见。

I have a list view, and a listviewitem defined in xml, with a couple of fields, nothing special. All set to visible.

然后我绑定使用自定义ArrayAdapter我的ListView,想隐藏我的文字视图之一,第5行。不过,这似乎是躲在项目0和项目5这是一个有点奇怪我的TextView?我已经简化了code,重现的问题的,希望有人能够帮助我......

Then I bind to my ListView using a custom ArrayAdapter, and want to hide one of my text views, on row 5. However, it seems to be hiding my TextView on item 0 and item 5. Which is a bit odd? I've simplified the code, to reproduce the problem and hopefully someone will be able to help me...

我的适配器

public class MenuScreenAdapter extends ArrayAdapter<String>
{
    private List<String> _items;
    private Context _context;

    public MenuScreenAdapter(Context context, List<String> items)
    {
        super(context, R.layout.list_menu_item, items);

        _context = context;
        _items = items;
    }

    private MenuScreenAdapter(Context context, int textViewResourceId)
    {
        super(context, textViewResourceId); 
    }

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

        if (v == null)
        {
            LayoutInflater vi = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_menu_item, null);
        }
        String o = _items.get(position);    
        if (o != null)
        {
            TextView tt = (TextView) v.findViewById(R.id.list_menu_item_name);
            if (tt != null)
                tt.setText(o);

            if (position == 5)
                tt.setVisibility(View.GONE);
        }
        return v;
    }
}

我绑定code

    // Load everything up that we need
    List<String> items = new ArrayList<String>();
    items.add("One");
    items.add("Two");
    items.add("Three");
    items.add("Four");
    items.add("Five");
    items.add("Six");
    items.add("Seven");
    items.add("Eight");
    items.add("Nine");
    items.add("Ten");

    // Get the ListView, and set it's adapter. The HomeScreenAdapter
    // takes care of the rest
    ListView homeScreenListView = (ListView) _mainActivity.findViewById(R.id.view_home_list);
    homeScreenListView.setOnItemClickListener(ItemSelected);
    homeScreenListView.setAdapter(new MenuScreenAdapter(_mainActivity.getBaseContext(), items));

在此先感谢!

推荐答案

由于行视图由ArrayAdapter重用,一旦View.GONE被设定,它会卡里到下一行,其中该视图将被重复使用。在你的情况,你设置View.GONE第五行中的TextView,移动列表中的一点,arrayadapter决定重用你的第五行布局以显示第一行,因为没有变化做了,TextView的仍然隐藏。

Since row views are reused by ArrayAdapter, once the View.GONE is set, it will cary on to the next row, where this view will be reused. In your case, you set View.GONE to textview in the fifth row, moved list a little and arrayadapter decided to reuse your fifth row layout to display the first row, since no changes were done to it, the textView still remains hidden.

只要做到了:

if (position == 5) {
            tt.setVisibility(View.GONE);
} else {
            tt.setVisibility(View.VISIBLE);
}

P.S。如果你还没有,观看有关谷歌从一个列表视图presentation。的usefult信息有吨。 列表视图

这篇关于的ListView ArrayAdapter,躲在里面排的孩子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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