listview项,按钮不响应onListItemClick

时间:2013-11-25 08:35:49

标签: android listview android-arrayadapter custom-adapter

我正在开发一个列表项很复杂的应用程序,TextView和两个ImageButtons。我已经看了周围的解决方案,并尝试了所有我见过的,仍然没有。

列表是我在Override onListItemClick上的ListFragment的一部分。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF" >

    <TextView
        android:id="@+id/medcine_info_txt"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:padding="3dp"
        android:textColor="@color/black" />

    <ImageButton
        android:id="@+id/item_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/medcine_info_txt"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/item_edit"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@android:drawable/ic_menu_edit" />

    <ImageButton
        android:id="@+id/item_history"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/medcine_info_txt"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/item_history"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@android:drawable/btn_star" />

</RelativeLayout>

这是我的适配器getView,我处理按钮单击,它实现了OnClickListener

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflator = (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View listItem = inflator.inflate(R.layout.medcince_list_item, null);

    ImageButton mEdit = (ImageButton)listItem.findViewById(R.id.item_edit);
    mEdit.setOnClickListener(this);
    mEdit.setTag(getItem(position));

    ImageButton mHistory = (ImageButton)listItem.findViewById(R.id.item_history);
    mHistory.setOnClickListener(this);
    mHistory.setTag(getItem(position));

    return listItem;
}

有关为什么onListItemClick没有处理点击的任何想法?

4 个答案:

答案 0 :(得分:2)

我认为你的ImageButton正在偷走onItemClickLister事件。将此属性添加到您的布局

机器人:descendantFocusability = “blocksDescendants”

<TextView
    android:id="@+id/medcine_info_txt"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:padding="3dp"
    android:textColor="@color/black" />

<ImageButton
    android:id="@+id/item_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    .......

由于

答案 1 :(得分:1)

这是ListView的常见问题。我阅读了有关此内容的源代码。
问题:为什么onListItemClick不被召唤?
答案:

  • AbsListView类覆盖onTouchEvent方法。
  • 剪辑代码来自onTouchEvent方法。
     
    if (inList && !child.hasFocusable()) {
                        if (mPerformClick == null) {
                            mPerformClick = new PerformClick();
                        }
    .....
    }
    

    如果是孩子,则会调用PerformClick。hasFocusable()返回false,哪个孩子是ListView项目视图;
  • 剪辑代码来自hasFocusable方法。
 
@Override
    public boolean hasFocusable() {
        if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {return false; }
        if (isFocusable()) {return true;}
        final int descendantFocusability = getDescendantFocusability();
        if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) {
            final int count = mChildrenCount;
            final View[] children = mChildren;
            for (int i = 0; i 


So solution:
Solution A,set ListView item descendantFocusability property, let its getDescendantFocusability() is not equal FOCUS_BLOCK_DESCENDANTS.
Solution B, ListView item all child views is not hasFocusable( hasFocusable()
返回false)。

答案 2 :(得分:0)

使ListFragment实现View.OnClickListener接口,并在方法OnClick(View view)中按下按钮时实现要调用的代码,即@Override

答案 3 :(得分:0)

你可以创建View.OnClickListener对象,它可以在getView中监听你的图像按钮。 onListItemClick通常用于处理行单击事件而不是行中的项目。

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflator = (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View listItem = inflator.inflate(R.layout.medcince_list_item, null);

    ImageButton mEdit = (ImageButton)listItem.findViewById(R.id.item_edit);
    mEdit.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        // HERE YOU CAN HANDLE BUTTON CLICK. POSITION YOU CAN HAVE FROM getView already.
    }

    });
    mEdit.setTag(getItem(position));

    ImageButton mHistory = (ImageButton)listItem.findViewById(R.id.item_history);
    mHistory.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        // HERE YOU CAN HANDLE BUTTON CLICK. POSITION YOU CAN HAVE FROM getView already.
    }

    });
    mHistory.setTag(getItem(position));

    return listItem;
}