How to make the first item of a ListView to be selected as default at startup?

前端 未结 13 1019
甜味超标
甜味超标 2020-12-11 16:08

There is a ListView in my App and the ListView has a selector. I want to make the first item of this ListView to be selected as default at the very

相关标签:
13条回答
  • 2020-12-11 16:33

    You can do this by

    yourListView.setSelection(0);
    yourListView.getSelectedView().setSelected(true);
    

    I hope this will help you

    0 讨论(0)
  • 2020-12-11 16:34
    if(position == 0){
            // This is the first item, you need to select this
            //rowview which u inflate under getview 
            rowView.performClick();
    
        }
    

    i think this will help you . it works for me

    0 讨论(0)
  • 2020-12-11 16:41

    THIS WORKS FOR ME.You have to make custom adapter.

         First Create Modal Class
    
            public class FilterBean {
    
             String filter_catagory;
                     boolean isSelected;
    
                public boolean isSelected() {
                    return isSelected;
                }
    
                public void setSelected(boolean selected) {
                    isSelected = selected;
                }
    
              public String getFilter_catagory() {
                    return filter_catagory;
                }
    
                public void setFilter_catagory(String filter_catagory) {
                    this.filter_catagory = filter_catagory;
                }
    
            }
    
    
        In Adapter Class
    
        @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                convertView = layoutInflater.inflate(R.layout.size_name_indicator, null);
                txt = (TextView) convertView.findViewById(R.id.key_textView);
                txt.setText(getItem(position).getFilter_catagory());
    
                if(getItem(position).isSelected()){
                    txt.setBackgroundColor(context.getResources().getColor(R.color.transparent));
                }else {
    
                    txt.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
                }
    
    
                return convertView;
            }
    
         public void updateNameBean(String key,boolean checked)
            {
    
                for(int i=0;i<getCount();i++)
                {
                    FilterBean filterBean =  getItem(i);
                    if(filterBean.getFilter_catagory().equals(key))
                    {
                        filterBean.setSelected(checked);
                    }else {
                        filterBean.setSelected(false);
                    }
                }
                notifyDataSetChanged();
            }
    
    In Filter Activity or Fragment
    
     filter_listView.setAdapter(filterNameAdapter);
                   filterNameAdapter.updateNameBean(filterNameAdapter.getItem(0).getFilter_catagory(),true);
            filter_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    String key = filterNameAdapter.getItem(position).getFilter_catagory();
                    filterNameAdapter.updateNameBean(filterNameAdapter.getItem(position).getFilter_catagory(),true);
    
                }
            }); 
    
    0 讨论(0)
  • 2020-12-11 16:44

    Create an onItemClickListener then use this snippet, filling in the appropriate values:

    listView.performItemClick(View view, int position, long id);
    

    See the docs for further detail.

    0 讨论(0)
  • 2020-12-11 16:44

    This works if you want to click it to get additional event handling done:

    listView.getAdapter().getView(position, null, null).performClick();
    
    0 讨论(0)
  • 2020-12-11 16:45

    I think this will work as we have given 0 index of array to listView

    ListView.setSelection(0);
    
    0 讨论(0)
提交回复
热议问题