Android onItemClickListener没有使用ImageButton调用listView

时间:2013-08-11 21:51:51

标签: android listview imagebutton onitemclicklistener

我生成一个带有标题元素和非标题元素的listView。标题在单击时调用onItemClickListener,但非标题元素不会。单击非标题元素时,它会突出显示,但不会调用onItemClickListener。

标题是具有行分隔符和TextView的RelativeLayout,而非标题元素具有其他ImageButton。我猜这个ImageButton会以某种方式导致这个问题吗?

自定义BaseAdapter:

private class MenuListAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{
        public int textWidth;

        public MenuListAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return LIST.size();
        }

        @Override
        public boolean areAllItemsEnabled() {
            return true;
        }

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

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String headerText = getHeader(position);
            if(headerText != null) {


                View item = convertView;
                if(convertView == null || convertView.getTag() == LIST_ITEM) {

                    item = LayoutInflater.from(mContext).inflate(
                            R.layout.lv_header_layout, parent, false);
                    item.setTag(LIST_HEADER);

                }
                Typeface tf = Typeface.createFromAsset(mContext.getAssets(),
                        "Roboto-Bold.ttf");
                TextView headerTextView = (TextView)item.findViewById(R.id.lv_list_hdr);
                headerTextView.setTypeface(tf);
                headerTextView.setText(headerText);
                return item;
            }

            View item = convertView;
            if(convertView == null || convertView.getTag() == LIST_HEADER) {
                item = LayoutInflater.from(mContext).inflate(
                        R.layout.lv_layout, parent, false);
                item.setTag(LIST_ITEM);
            }

            ImageButton image = (ImageButton)item.findViewById(R.id.button);
            image.setFocusable(false);
            image.setClickable(false);
            image.setFocusableInTouchMode(false);

            TextView header = (TextView)item.findViewById(R.id.lv_item_header);
            Typeface tf = Typeface.createFromAsset(mContext.getAssets(),
                    "Roboto-Light.ttf");
            header.setText(LIST.get(position % LIST.size()));
            header.setTypeface(tf);

            // Measures the width of the text in the textView.
            textWidth = MenuUtilities.getTextWidth(LIST.get(position % LIST.size()), header.getPaint());
            // The text size is decreased until the text is under the specified width or the textSize
            // is under a certain size, inc which case the text will wrap to the next line.
            while (textWidth > MenuUtilities.menuItemTextWidth && header.getTextSize() > 33) {
                header.setTextSize((header.getTextSize() - 1)/2); // Black magic happening here.
                textWidth = MenuUtilities.getTextWidth(LIST.get(position % LIST.size()), header.getPaint());
            }
            // The textView is recentered if the textView wraps to a second line.
            if (header.getTextSize() == 33.0) {
                header.setPadding(header.getPaddingLeft(), 0, header.getPaddingRight(), 0);
            }

            //Set last divider in a sublist invisible
            View divider = item.findViewById(R.id.item_separator);
            if(position == HDR_POS2[day] -1 || position == LIST.size() - 1) {
                divider.setVisibility(View.INVISIBLE);
            }

            return item;
        }

        private String getHeader(int position) {

            if(position == HDR_POS1  || position == HDR_POS2[day]) {
                return LIST.get(position);
            }

            return null;
        }

        private final Context mContext;


        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // arg2 = the id of the item in our view (List/Grid) that we clicked
            // arg3 = the id of the item that we have clicked
            // if we didn't assign any id for the Object (Book) the arg3 value is 0
            // That means if we comment, aBookDetail.setBookIsbn(i); arg3 value become 0
            Log.d("You clicked on position : " + arg2 + " and id : " + arg3, "CLICKITEM");
            Toast.makeText(mContext, "You clicked on position : " + arg2 + " and id : " + arg3, Toast.LENGTH_SHORT).show();
        }

    }

标题xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/lvHdrItemHeight"
>

<View
    android:id="@+id/item_separator"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="@dimen/lvDividerHeight"
    android:background="@color/lvHeaderDividerColor"
    android:layout_marginTop="@dimen/lvSectionDividerMarginTop"
    />

<TextView
    android:textIsSelectable="false"
    android:id="@+id/lv_list_hdr"
    android:textColor="@color/lvHeaderTextColor"
    android:gravity="bottom|left"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@id/item_separator"
    android:layout_alignParentLeft="true"
    style="@style/listViewHeaderItem"
    />
</RelativeLayout>

非标题xml:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
style="@style/listViewItem"
android:descendantFocusability="blocksDescendants"
android:background="@android:drawable/list_selector_background"
android:clickable="true"
android:focusable="true"
>

<View
    android:id="@+id/item_separator"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="@dimen/lvDividerHeight"
    android:background="@color/lvDividerColor"/>

<ImageButton
    style="@style/listViewItemButtonStyle"
    android:background="@android:drawable/list_selector_background"
    android:src="@drawable/ic_details"
    android:id="@+id/button"
    android:focusable="false"
    android:clickable="false"
    android:focusableInTouchMode="false"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:contentDescription="@string/cd"
    />

<TextView
    android:id="@+id/lv_item_header"
    style="@style/listViewPrimaryDetail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/button"
    android:ellipsize="marquee"
    android:singleLine="false"
    android:textIsSelectable="false" />

</RelativeLayout>

我尝试在xml中设置android:descendantFocusability="blocksDescendants"以及以编程方式设置listView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);,并为ImageButton设置android:clickable="false"android:focusable="false",但似乎没有任何效果。有谁知道问题是什么?

0 个答案:

没有答案