android - 在可扩展 ListView 上调用 setOnItemSelectedListener

标签 android listview expandablelistview

我有一个 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/

相关文章:

java - 自定义 ListView 适配器中的 NullPointer 异常

android - ExpandableListView 仅在特定 Button 上展开?

java - 想要通过单击而不是两次单击来打开我的 android webview 网站内的横向链接

android - android中的多行单选按钮?

android - 如何将 Java.IO.FileOutputStream 写入 photo.Compress(Bitmap.CompressFormat.Png, 100, outputstream)?

android - 自定义 ExpandableListview,其中包含子项中的另一个展开按钮

android - 如何从可扩展 ListView 中获取更新值

android - Material 设计库需要 oldnineandroids 2.4.+ : build failed

java - 滚动后 ListView 项目保持突出显示

listview - Scrollview 中的 Xamarin Forms Listview 不滚动