Android中可展开列表视图中的按钮

13

我有一个带有自定义适配器的可扩展列表活动(ExpandableListActivity)。

如果适配器的自定义行包含imageView,则一切正常,但如果我将此视图从imageView更改为imageButton,则可扩展列表视图不会展开。

有没有什么方法可以放置一个按钮,可以单击该按钮而可扩展列表不失去展开功能?

2个回答

19
Button button = (Button) view.findViewById(R.id.yourButton); 
//where button is the button on listView, and view is the view of your item on list

button.setFocusable(false);   /// THIS IS THE SOLUTION

button.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        //button functionalty   ...
    }
});

(Buttom)应该是(Button)。 - pahan
1
安卓又出现了一个令人费解的时刻。 - George Pligoropoulos
非常感谢。你有关于这种行为的任何解释吗?在我看来,它似乎与扩展功能无关... - Kikiwa
在我的情况下,我有一个ScrollView,在ListView中具有相同的行为...当可见时,列表的OnItemClickListener不再起作用(可能是焦点问题,或双重滚动问题...不清楚) - Giudark

1

您不仅可以在类中设置“focusable”属性,还可以在xml中设置,至少对我而言是有效的。

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接