2

我生成一个带有标题元素和非标题元素的 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>

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

4

0 回答 0