我有一个 expandableListView
,默认情况下我想要选择第一个 parentgroup
,即位置 0。以便它调用我的 setOnItemSelectedListener
。以下是我设置适配器和选择监听器的方式:
adapter = new ExpandableListAdapter(getApplicationContext(),
lstEpisodes, list);
list.setAdapter(adapter);
list.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"focus on: " + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
这是listview的xml布局:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/exlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:childDivider="#FFFFFF"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:groupIndicator="@null" >
<requestFocus />
</ExpandableListView>
</LinearLayout>
这是我目前尝试过的:
list.setSelection(0);
list.requestFocus();
list.invalidate();
如何选择 listview
的第一行,这是设置适配器时的默认选择......?
最佳答案
这可能是解决问题的一种 hacky 方法,但它会起作用。在局部变量中捕获您的匿名 OnItemSelectedListener
接口(interface),在 setOnItemSelectedListener
方法中分配它。您可以在局部变量上使用 onGroupClick
而不是 setOnGroupClickListener
。您可以访问需要传入的所有变量。这应该作为绝对的最后手段。
关于android - 在可扩展 ListView 上调用 setOnItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23888250/