Search code examples
androidandroid-layoutlistviewandroid-linearlayout

How to change Linear layout's items visibility in a ListView


I have a linear layout in a Listview and need to change some items' visibility within the linear layout when the Linear Layout gets selected. Due to the design, the Listview has no idea what the linear layout is and thus can't explicitly refer to any item in this layout. In this case, what will be the best solution to trigger the visibility change in the Linear layout?

 listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(
                    final AdapterView<?> adapterView,
                    final View view,
                    final int position,
                    final long id) {
                        //the View is a linearlayout
                        //will need to trigger its items visibility 
                    }
                }

Thanks!


Solution

  • Well, one possible solution is to call a method on the selected view, that will do what you want. Considering that you are using a custom view, it will look like this:

    list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            MyCustomView entry = (MyCustomView) parent.getAdapter().getItem(position);
    
            // Now you call some method that changes the visibility of whatever you want
            entry.doSomeStuff();
        }
    });
    

    Ps: in this example I used a custom view, but of course it can be your LinearLayout, if that's what you only have, although it would be more appropriate to use a custom class, for better encapsulation.