Default Selection of a Row in the ListView and HighLight the selected Row Android

前端 未结 3 1450
慢半拍i
慢半拍i 2020-12-21 07:42

I have 2 listview. I want when the activity starts first row of the both the listview to be selected by default.I googled up i found this

onewaydata=new One         


        
3条回答
  •  独厮守ぢ
    2020-12-21 07:46

    Try this, it should do the trick:

    your_list_view.post(new Runnable() {
    
            @Override
            public void run() {
               your_list_view.setSelection(0);          
               }
            });
    

    UPDATE: Please take a look at this

        class YourAdapter extends BaseAdapter{
    
                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return 0;
                }
    
                @Override
                public Object getItem(int arg0) {
                    // TODO Auto-generated method stub
                    return null;
                }
    
                @Override
                public long getItemId(int arg0) {
                    // TODO Auto-generated method stub
                    return 0;
                }
    
                @Override
                public View getView(int position, View convertView, ViewGroup arg2) {
                    View view = convertView;
    
                    if(position == 0){
                        // This is the first item, you need to select this
                        view.setSelected(true);
    
                    }
                    // Do whatever you want here
                    return view;
                }
    
            }
    

    In XML:

    
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
            
            
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"/>
    

    In your item-row-xml:

         
        
        
    

    And last you need to implement your own item_row_select with the state of "selected".

    Hope this helps.

提交回复
热议问题