Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with ListView inside a PopupWindow

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

    window.setContentView(root);
    window.setTouchable(true);
    window.setFocusable(true);
    window.setOutsideTouchable(true);
    window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

Then the ListView:

    fileList = (ListView) root.findViewById(R.id.explorer_list);
    fileList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    fileList.setSelector(android.R.drawable.screen_background_light_transparent);
    fileList.setOnItemClickListener(this);

    [...]

    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
        selected = (File) fileList.getItemAtPosition(pos);      
    }

Like this, everything works correctly except that the selector will not show up on selection until ListView is scrolled (nothing shows visually as selected until the list is scrolled, although the item is correctly selected).

If I set the PopupWindow not focusable, then the visual selection works correctly (the item is visually selected right when clicked into) but onItemClick() is never called and thus I cannot get the selected item.

ListView.getSelectedItem() always returns null in both cases, even if there's a selected item.

Any idea on how to solve this situation? Thanks in advance.

like image 591
m0skit0 Avatar asked May 22 '26 07:05

m0skit0


2 Answers

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

    /** File names */
    private List<File> values = new ArrayList<File>();

    /** Currently selected position */
    private int selected = -1;

    public FileExplorerAdapter(Context context, List<File> 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);
}
like image 154
m0skit0 Avatar answered May 23 '26 21:05

m0skit0


//Set listview focusable properties to false

android:focusable="false"

like image 21
Nas Avatar answered May 23 '26 22:05

Nas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!