如何在ListView中更改线性布局的项目可见性

时间:2016-11-30 00:47:53

标签: android android-layout listview android-linearlayout

我在Listview中有一个线性布局,需要在选择线性布局时更改线性布局中某些项目的可见性。由于设计原因,Listview不知道线性布局是什么,因此无法明确引用此布局中的任何项目。在这种情况下,什么是触发线性布局中可见性变化的最佳解决方案?

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

谢谢!

1 个答案:

答案 0 :(得分:1)

嗯,一种可能的解决方案是在所选视图上调用一个方法,它将执行您想要的操作。考虑到您使用的是自定义视图,它将如下所示:

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:在这个例子中我使用了一个自定义视图,但当然它可以是你的LinearLayout,如果你只有你所拥有的,尽管使用自定义类更合适,以获得更好的封装。

相关问题