无法在Android 4.1弹出窗口中选中ListView中的行,但在Android 5.0上可以正常工作。

11

我的页面中有一个弹出窗口,其中包含一个列表视图。我已经在单独的XML文件中创建了弹出窗口的设计,并在主页面上某个按钮点击时加载它。该弹出窗口有一个列表视图,每一行都有一个图像和一个文本视图。但在Android 4.1中,我无法获得列表视图中的行选择,但在5.0中可以正常工作。请问有人可以建议我解决方案吗?
Listview:

<ListView android:id="@+id/lstSites"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fastScrollEnabled="true"
        android:layout_margin="5dp"
        android:descendantFocusability="blocksDescendants"></ListView>
</LinearLayout>

列表视图项:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
    android:id="@+id/thumbImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:focusable="false"
    android:focusableInTouchMode="false"/>
<TextView
    android:id="@+id/tvSite"
    android:textSize="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="Name"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

添加行点击监听器:

lstSiteMenu = (ListView) popupView.findViewById(R.id.lstSites);

            adapter = new MenuItemsAdapter(SiteActivity.this, arrMenu);
            // Attach the adapter to a ListView
            lstSiteMenu.setAdapter(adapter);
            lstSiteMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        final int position, long id) {
            }

Logcat 里有任何错误吗?当你说“行被选中”时,是指 onItemClick 没有起作用还是你没有得到位置? - virendrao
1
ListView中删除此android:descendantFocusability="blocksDescendants" - Piyush
4个回答

1
尝试所有解决方案:

popupwindow.setFocusale(true);
listView xml : "android:focusable="true"

请勿使用 lstSiteMenu.setOnItemClickListener,而是进入您的适配器 getView 并添加。
convertView.setOnClickListener

希望它对你有所帮助!

1

从listview中删除此属性,android:descendantFocusability="blocksDescendants"

android:focusable="false"
android:focusableInTouchMode="false"

来自文本和图像视图。

尝试这个示例样本,

String names[] ={"A","B","C","D"};
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View convertView = (View) inflater.inflate(R.layout.row_file, null);
        alertDialog.setView(convertView);
        alertDialog.setTitle("List");
        ListView lv = (ListView) convertView.findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
        lv.setAdapter(adapter);
        alertDialog.show();

否则你的代码很好。它可以工作。

请查看此链接以获取更多详细信息。

同样还有官方链接


0

您需要从您的ListView中删除android:descendantFocusability="blocksDescendants"

还要从ImageViewTextView中的行布局中删除android:focusable="false"android:focusableInTouchMode="false"

列表项应该是可点击的,因此请取消禁用它获取焦点。


-1
在适配器类中设置行的点击监听器。
public class MenuItemsAdapter extends Base Adapter{
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
             /*
                          Paste your adapter code
             */
              customView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("log", "You clicked at pos "+position);
                }
            });
        }
}

这段代码在所有的SDK版本中都有效。

同时,从XML文件中删除这些属性。

android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"

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