ListView ArrayAdapter, hiding children inside Row?

蓝咒 提交于 2019-12-08 09:48:45

问题


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...

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

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...

My Adapter

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;
    }
}

My Binding 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));

Thanks in advance!


回答1:


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.

Just do the:

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

P.S. If you still haven't, watch a presentation about ListViews from google. Tons of usefult info there. ListViews



来源:https://stackoverflow.com/questions/4897480/listview-arrayadapter-hiding-children-inside-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!