ImageView无法在ListView中单击

时间:2015-09-30 12:01:52

标签: android listview onclick

我正在尝试将onClickListener设置为ImageView项内的ListView。 我的适配器: 的 ContactListAdapter.java

public class ContactListAdapter extends ArrayAdapter<ContactItemInterface> {

    private int resource; // store the resource layout id for 1 row
    private boolean inSearchMode = false;

    private ContactsSectionIndexer indexer = null;

    public ContactListAdapter(Context _context, int _resource,
            List<ContactItemInterface> _items) {
        super(_context, _resource, _items);
        resource = _resource;

        // need to sort the items array first, then pass it to the indexer
        Collections.sort(_items, new ContactItemComparator());

        setIndexer(new ContactsSectionIndexer(_items));

    }



    // do all the data population for the row here
    // subclass overwrite this to draw more items
    public void populateDataForRow(View parentView, ContactItemInterface item,
            int position) {
        // default just draw the item only

    }

    // this should be override by subclass if necessary
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewGroup parentView;
        ContactItemInterface item = null;
        if (getCount() > position) {
            item = getItem(position);
        }
        // Log.i("ContactListAdapter", "item: " + item.getItemForIndex());



        parentView = (ViewGroup) LayoutInflater.from(getContext()).inflate(resource, parent, false);

        // for the very first section item, we will draw a section on top
        // showSectionViewIfFirstItem(parentView, item, position);

        // set row items here
        if (getCount() > position) {
            populateDataForRow(parentView, item, position);
        }
        return parentView;

    }

    public boolean isInSearchMode() {
        return inSearchMode;
    }

    public void setInSearchMode(boolean inSearchMode) {
        this.inSearchMode = inSearchMode;
    }

    public ContactsSectionIndexer getIndexer() {
        return indexer;
    }

    public void setIndexer(ContactsSectionIndexer indexer) {
        this.indexer = indexer;
    }

}

AdapterContactList.java

public class AdapterContactList extends ContactListAdapter {

    private OnClickListener listener;
    private OnClickListener onInfoClickListener;
    private OnClickListener onItemClickListener;
    private ArrayList<User> select = null;

    public AdapterContactList(Context _context, int _resource,
            List<ContactItemInterface> _items) {
        super(_context, _resource, _items);
    }

    public void setSelectArray(ArrayList<User> select) {
        this.select = select;
        this.notifyDataSetChanged();
    }

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public void setOnInfoClickListener(OnClickListener l){
        this.onInfoClickListener = l;
    }

    public void setOnItemClickListenerCustom(OnClickListener l){
        this.onInfoClickListener = l;
    }

    // override this for custom drawing
    public void populateDataForRow(View parentView, ContactItemInterface item,
            int position) {
        // default just draw the item only
        View infoView = parentView.findViewById(R.id.infoRowContainer);
        infoView.setTag(position);
        TextView fullNameView = (TextView) infoView.findViewById(R.id.tv_name);
        View tv_grey = infoView.findViewById(R.id.tv_grey);
        View tv_green = infoView.findViewById(R.id.tv_green);
        View tv_red = infoView.findViewById(R.id.tv_red);
        View tv_yellow = infoView.findViewById(R.id.tv_yellow);
        ImageView iv_user = (ImageView) infoView.findViewById(R.id.im_calls);
        View pending_cont = infoView.findViewById(R.id.pending_cont);
        ImageView btnInfo = (ImageView)infoView.findViewById(R.id.btn_info);
        if(btnInfo != null){
            btnInfo.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(), "Button", Toast.LENGTH_SHORT).show();
                }
            });
        }


        if (tv_green != null) {
            if(tv_grey != null){
                tv_grey.setVisibility(View.GONE);
            }
            tv_green.setVisibility(View.GONE);
            tv_red.setVisibility(View.GONE);
            tv_yellow.setVisibility(View.GONE);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View line_end = infoView.findViewById(R.id.line_end);
            line_end.setVisibility(View.VISIBLE);
        }

        if (item instanceof ContactListItem) {
            ContactListItem contactItem = (ContactListItem) item;
            fullNameView.setText(contactItem.getFullName());

            if (contactItem.getAvatar() != null) {
                MyProject.loadImage(contactItem.getAvatar(), iv_user);
            } else {
                User us1 = MyProject.getTempUser(contactItem.getMyProjectid());
                if (us1 != null) {
                    MyProject.loadImage(us1.getAvatar(), iv_user);
                }
            }

            if (tv_green != null) {
                int status = MyProject.getUserStatus(contactItem.getMyProjectid());
                MyProject.debug("" + status);
                if (status != MyProject.STATUS_NONE) {
                    if (status == MyProject.STATUS_ONLINE) {
                        tv_green.setVisibility(View.VISIBLE);
                    }
                    if (status == MyProject.STATUS_AWAY) {
                        tv_yellow.setVisibility(View.VISIBLE);
                    }
                    if (status == MyProject.STATUS_DND) {
                        tv_red.setVisibility(View.VISIBLE);
                    }
                }else{
                    if(tv_grey != null) {
                        tv_grey.setVisibility(View.VISIBLE);
                    }
                }
            }

            if (infoView.findViewById(R.id.checkbox) != null) {
                CheckBox checkbox = (CheckBox) infoView
                        .findViewById(R.id.checkbox);
                if (select != null) {
                    checkbox.setChecked(select.indexOf(contactItem.getUser()) >= 0);
                } else {
                    checkbox.setChecked(false);
                }
            }

            if (listener != null) {
                View button_add = infoView.findViewById(R.id.add_cont);
                if (contactItem.getUser().isPending()) {
                    pending_cont.setVisibility(View.VISIBLE);
                    button_add.setVisibility(View.GONE);
                    infoView.setOnClickListener(listener);
                } else {
                    pending_cont.setVisibility(View.GONE);
                    button_add.setVisibility(View.VISIBLE);
                    button_add.setOnClickListener(listener);
                    infoView.setOnClickListener(listener);
                }
            }

            infoView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "Avatar", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }
}

请查看btnInfo。我设置了他onClickListener并且在调试模式下我确信它已经设置,但它不起作用。它只是被忽略了。

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/infoRowContainer"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:background="@drawable/dialog_buttons_background"
    android:descendantFocusability="blocksDescendants">

    <com.myproject.custom.CFRoundImageView
        android:id="@+id/im_calls"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="6dp"
        android:contentDescription="@string/temp"
        android:scaleType="fitXY"
        android:src="@drawable/fb_placeholder"/>

    <com.myproject.custom.CFTextView
        android:id="@+id/tv_grey"
        android:layout_width="@dimen/status_side_size"
        android:layout_height="@dimen/status_side_size"
        android:layout_alignBottom="@+id/im_calls"
        android:layout_alignRight="@+id/im_calls"
        android:background="@drawable/ic_not_in_contact"/>

    <com.myproject.custom.CFTextView
        android:id="@+id/tv_green"
        android:layout_width="@dimen/status_side_size"
        android:layout_height="@dimen/status_side_size"
        android:layout_alignBottom="@+id/im_calls"
        android:layout_alignRight="@+id/im_calls"
        android:background="@drawable/ic_status_online"/>

    <com.myproject.custom.CFTextView
        android:id="@+id/tv_yellow"
        android:layout_width="@dimen/status_side_size"
        android:layout_height="@dimen/status_side_size"
        android:layout_alignBottom="@+id/im_calls"
        android:layout_alignRight="@+id/im_calls"
        android:background="@drawable/ic_status_not_presents"/>

    <com.myproject.custom.CFTextView
        android:id="@+id/tv_red"
        android:layout_width="@dimen/status_side_size"
        android:layout_height="@dimen/status_side_size"
        android:layout_alignBottom="@+id/im_calls"
        android:layout_alignRight="@+id/im_calls"
        android:background="@drawable/ic_status_offline"/>

    <com.myproject.custom.CFTextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="44dp"
        android:layout_marginRight="30dp"
        android:ellipsize="end"
        android:gravity="left|center_vertical"
        android:maxLines="1"
        android:text="@string/text_contacts"
        android:textColor="@color/color_grey5"
        android:textSize="16sp"
        app:typeFace="OpenSans-Light"/>

    <TextView
        android:id="@+id/line_end"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginRight="36dp"
        android:layout_marginTop="43dp"
        android:background="@color/color_gray2"
        android:visibility="gone"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_info"
            android:src="@drawable/ic_info"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:clickable="true"
            android:padding="8dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="30dp"
            />

</RelativeLayout>

ImageView从不点击! 我尝试将clcikable=true设置为ImageView

我尝试为所有项目的子项设置'focusable = false focusableInTouchMode = false'。

我尝试在项目根视图上使用descendantFocusability="blocksDescendants"

我试图将这种方法与另一种方法结合起来。

ImageView仍无法点击。

2 个答案:

答案 0 :(得分:0)

Try this (things in *** .... ****)
......
public void populateDataForRow(View parentView, ContactItemInterface item,
            int position) {
        // default just draw the item only
        View infoView = parentView.findViewById(R.id.infoRowContainer);
        infoView.setTag(position);
        TextView fullNameView = (TextView) infoView.findViewById(R.id.tv_name);
        View tv_grey = infoView.findViewById(R.id.tv_grey);
        View tv_green = infoView.findViewById(R.id.tv_green);
        View tv_red = infoView.findViewById(R.id.tv_red);
        View tv_yellow = infoView.findViewById(R.id.tv_yellow);
        ImageView iv_user = (ImageView) infoView.findViewById(R.id.im_calls);
        View pending_cont = infoView.findViewById(R.id.pending_cont);
        ImageView btnInfo = (ImageView)infoView.findViewById(R.id.btn_info);
        ****btnInfo.setFocusable(false);****
        if(btnInfo != null){
            btnInfo.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(), "Button", Toast.LENGTH_SHORT).show();
                }
            });
        }
....... // Your code 

答案 1 :(得分:0)

将此添加到您的行xml: android:clickable =&#34; false&#34; android:focusable =&#34; false&#34;

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/infoRowContainer"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@drawable/dialog_buttons_background"
android:clickable="false"
android:focusable="false">
....