【问题标题】:OnItemClickListener does not working properly when adapter contains a button with onClickListener当适配器包含带有 onClickListener 的按钮时,OnItemClickListener 无法正常工作
【发布时间】:2023-04-11 04:45:01
【问题描述】:

我已经为我的ListView 实现了一个适配器,它扩展了 BaseAdapter。 我的列表项包含每个按钮都有 OnClickListener。

为每个项目添加 OnclickLister 后,列表的 OnItemClickListener 停止工作。

如何解决?

代码

在我的活动中 -

        ListView lv = (ListView) findViewById(R.id.list);
    lv.setTextFilterEnabled(true);  
    lv.setItemsCanFocus(true); 
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String debString = "position = " + position + " id = " + id;                
            Log.d(TAG, debString);
            Toast.makeText(getApplicationContext(), debString, Toast.LENGTH_SHORT).show();
            Contact selectedContact = dataVector.elementAt(position);
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.POSITION, position);
            bundle.putString(Constants.NAME, selectedContact.getName());
            bundle.putString(Constants.MDN, selectedContact.getMdn());
            bundle.putString(Constants.STATUS, selectedContact.getStatus());
            String filePath = null;
            if(contactsImagesProperties != null || !Utils.isNullOrEmpty((String) contactsImagesProperties.get(selectedContact.getMdn()))) {
                filePath = (String) contactsImagesProperties.get(selectedContact.getMdn());
            }
            bundle.putString(Constants.IMAGE, filePath);
            Intent intent = new Intent(context, ChildDisplayActivity.class);
            intent.putExtras(bundle);
            getParent().startActivityForResult(intent, 10);
        }           

在 getView() 中的 myBaseAdapter 中

        bitmap = Bitmap.createScaledBitmap(bitmap, Constants.CHILD_ICON_WIDTH, Constants.CHILD_ICON_HEIGHT, false);
    imageView.setImageBitmap(bitmap);
    statusView.setText(Constants.StatusCodeHandler.getStatusDesc(dataVector.elementAt(position).getStatus(), context));
    ImageButton imageButton = (ImageButton) view.findViewById(viewIds[3]);
    imageButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.ACTION, Constants.CONTACT_LOCATION_CODE);
            bundle.putString(Constants.MDN, dataVector.elementAt(position).getMdn());
            MainActivity.bundle = bundle;
            TabActivity mainActivity = (TabActivity) ((UsersListActivity)context).getParent().getParent();
            TabHost tabHost = mainActivity.getTabHost();
            tabHost.setCurrentTab(Constants.MAP_TAB_INDEX);
        }
    });

在 myListRaw.xml -

<ImageView android:src="@drawable/icon" 
    android:id="@+id/childListImageView" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:focusable="false"
    android:clickable="false"
    android:focusableInTouchMode="false"
    android:layout_alignParentRight="true"/>

<TextView android:id="@+id/childListTextView" 
    android:layout_marginRight="5dp" 
    android:layout_width="wrap_content" 
    android:text="TextView" 
    android:layout_height="wrap_content" 
    android:focusable="false"
    android:clickable="false"
    android:focusableInTouchMode="false"
    android:layout_toLeftOf="@+id/childListImageView" 
    android:layout_centerVertical="true"/>

<TextView android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp"
    android:text="Child Status" 
    android:id="@+id/childListStatus" 
    android:layout_width="wrap_content"         
    android:layout_toLeftOf="@+id/childListTextView" 
    android:layout_marginRight="15dp"
    android:focusable="false"
    android:clickable="false"
    android:focusableInTouchMode="false"
    android:layout_centerVertical="true"/>

<ImageButton android:id="@+id/childListButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Loc"  
    android:layout_marginTop="5dp"      
    android:layout_alignParentLeft="true"
    android:focusable="false" 
    android:clickable="false"
    android:focusableInTouchMode="false"/>

【问题讨论】:

  • 一些代码会更好理解........
  • 您允许使用 lv.setItemsCanFocus(true); 关注您的代码 - 尝试注释掉该行或将其设置为 false。
  • 我已根据您的评论更改了它,但仍然存在问题。

标签: android listview onitemclicklistener baseadapter


【解决方案1】:

如果您将行的部分设置为可聚焦 (android:focusable="true"),则 ListView 的 OnItemClickListener 不会响应。看看吧

【讨论】:

  • 所以给我们一些代码。 its hard to get the problem when we cant 看看你做了什么
  • eyal,除非您已明确调用 setFocusable ( false ),否则是的,它们是可聚焦的。您需要禁用按钮的焦点,但是您必须管理按钮的处理点击。我已经在我自己的应用程序中使用了这个 - 一旦你处理了点击,你就会隐式地使控件成为焦点。
  • @RivieraKid - 如果我是正确的,您还可以在行布局的 xml 文件中设置 android:focusable = "false"
  • 您可能是对的 Fixus,我不确定创建单击侦听器是否也会使控件默认成为焦点。不管怎样,我认为你的答案是正确的。
  • 我正在使用带 android:focusable = "false" 的 RelativeLayout 仍然有这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
相关资源
最近更新 更多