Problems with ListView inside a PopupWindow

后端 未结 4 2133
温柔的废话
温柔的废话 2021-01-06 20:10

I have a ListView in a PopupWindow. The PopupWindow is initialized like this

    window.setContentView(root);
    wind         


        
4条回答
  •  情歌与酒
    2021-01-06 20:51

    I finally used a custom adapter to store the selected value and use it from there to mark it:

    public class FileExplorerAdapter extends ArrayAdapter {
    
        /** File names */
        private List values = new ArrayList();
    
        /** Currently selected position */
        private int selected = -1;
    
        public FileExplorerAdapter(Context context, List values) {
            super(context, R.layout.explorer_row, values);
            this.values = values;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            // I know that my layout is always a TextView
            TextView row = (TextView) convertView;
            if (row == null) {
                row = (TextView) ViewHelper.inflateViewById(getContext(),
                        R.layout.explorer_row);
            }
    
            // More code...
    
            // Set up the background for selected element
            if (selected == position) {
                row.setBackgroundColor(Color.LTGRAY);
    
            // Override background selector
            } else {
                row.setBackgroundColor(Color.TRANSPARENT);
            }
    
            // More code...
    
            return row;
        }
    
        /** This sets the selected position */
        public void setSelected(int position) {
            selected = position;
        }
    }
    

    And on the class that implements the OnItemClickListener for the associated ListView, I set up the currently selected item in the adapter.

    @Override
    public void onItemClick(AdapterView adapter, View v, int pos, long id) {
        FileExplorerAdapter fileadapter = (FileExplorerAdapter) fileList
                .getAdapter();
        fileadapter.setSelected(pos);
    }
    

提交回复
热议问题