如何禁用以编程方式添加的ListView的默认选择

时间:2022-09-24 19:43:17

I am dynamically adding a listview in my activity layout. The problem I am facing is : when the listview is populated, it shows first item as selected and since I have used a selector, it highlights the default selection. I wish to know ,what needs to be added in code, so that none of the items is selected by default.

我在我的活动布局中动态添加listview。我面临的问题是:当列表视图被填充时,它显示第一个项目为选中,因为我使用了选择器,它突出显示默认选择。我想知道,需要在代码中添加什么,以便默认情况下不会选择任何项目。

Here is the code snippet of how I am adding listview dynamically.

这是我如何动态添加listview的代码片段。

            ListView dynamicListView = new ListView(this);  
    HierarchicalListAdapter hierarchicalListAdapter = new HierarchicalListAdapter(this, hierarchicalList);
    dynamicListView.setAdapter(hierarchicalListAdapter);
    dynamicListView.setOnItemClickListener(hierarchicalListAdapter);
    hierarchicalListAdapter.setItems(items);

    LayoutParams params = new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    dynamicListView.setCacheColorHint(0);
    dynamicListView.setDivider(null);
    dynamicListView.setDividerHeight(1);    
    dynamicListView.setSelector(R.color.list_item_higlight_color);
    dynamicListView.setLayoutParams(params);    

    mainListViewLayout.addView(dynamicListView, params);

1 个解决方案

#1


0  

dynamicListView.setItemChecked(0,false);

#1


0  

dynamicListView.setItemChecked(0,false);