11

我的页面中有一个弹出窗口,其中有一个列表视图。我在单独的 xml 中创建了弹出窗口的设计,并在我的主页中单击某个按钮时将其加载。弹出窗口有一个列表视图,每一行都有一个图像和一个文本视图。我无法在 android 4.1 的 listview 中获得行选择,但它在 5.0 中工作。谁能建议我解决方案?列表显示:

<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) {
            }
4

4 回答 4

1

尝试所有解决方案:


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

不要使用lstSiteMenu.setOnItemClickListener
,而是转到您的适配器getView并添加

convertView.setOnClickListener

希望对您有所帮助!

于 2015-12-29T08:14:00.560 回答
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();

否则你的代码很好。有用。

通过此链接了解更多详细信息。

同样的另一个官方链接

于 2015-12-22T10:54:41.250 回答
0

您需要android:descendantFocusability="blocksDescendants"从您的ListView.

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

列表项应该是可点击的,禁用它获得焦点

于 2015-12-22T10:13:23.313 回答
-1

在适配器类中设置行 onclick 侦听器

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"
于 2015-12-30T06:41:07.120 回答