1

我有一个自定义基本适配器,我在 DrawerLayout 中与我的 ListView 一起使用。ListView 膨胀正常,但我不能调用 onItemClickListener。

这是 MainAcitivty 的 onCreate 部分,我在其中设置了适配器和 onItemClickListener

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    isPhone = getResources().getString(R.string.screen_type).toString().equals("phone");
    mTitle = getResources().getString(R.string.app_name);
    mCategoryTitles = getResources().getStringArray(R.array.categories);
    mSubtext = getResources().getStringArray(R.array.subtext);



    mTitle = mDrawerTitle = getTitle();

    // Set the drawer toggle as the DrawerListener

    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    BaseAdapter adapter = new NavigationListAdapter(this, mCategoryTitles, mSubtext);
    mDrawerList.setAdapter(adapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    .....
 }

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        //selectItem(position);

        System.out.println("This is clicked");

    }
}

这是我的基本适配器代码

public class NavigationListAdapter extends BaseAdapter {

    public static final int HDR_POS1 = 0;
    public static final int HDR_POS2 = 9;
    private static final Integer LIST_HEADER = 0;
    private static final Integer LIST_ITEM = 1;

    Context mContext;
    String[] mCategories;
    String[] mSubtext;
    private SideBarPositionClickedCommunicator mCallback;

    public interface SideBarPositionClickedCommunicator{
        public void setListItemPosition(int itemInt);
}




    public NavigationListAdapter(Context context, String[] categories, String[] subtext)
    {

        mContext = context;
        mSubtext = subtext;
        mCategories = categories;

    }

    @Override
    public int getCount() {
        return mCategories.length;
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final int positionClicked = position;
        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);

            }

            TextView headerTextView = (TextView)item.findViewById(R.id.lv_list_hdr);
            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);
        }

        TextView header = (TextView)item.findViewById(R.id.lv_item_header);
        header.setText(mCategories[position % mCategories.length]);

        TextView subtext = (TextView)item.findViewById(R.id.lv_item_subtext);
        subtext.setText(mSubtext[position % mCategories.length]);

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


        return item;
    }


    private String getHeader(int position) {

        if(position == HDR_POS1  || position == HDR_POS2) {
            return mCategories[position].toUpperCase();
        }

        return null;
    }

    @Override
    public boolean isEnabled(int position) {

        if(position == HDR_POS1  || position == HDR_POS2)
            return false;
        else
            return true;



    }
}

这是我的 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/dark_gray"
    android:layout_marginTop="@dimen/lvSectionDividerMarginTop"
    />

<TextView
    android:text="This is a text"
    android:id="@+id/lv_list_hdr"
    android:textColor="@color/dark_gray"
    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>

每个行项目:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    tools:context=".MainActivity"
    style="@style/listViewItem"
    android:background="@android:drawable/list_selector_background"
    >

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



    <TextView
        android:id="@+id/lv_item_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        style="@style/listViewPrimaryDetail"
        android:fontFamily="sans-serif-light"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="This is a test"
        android:layout_alignParentLeft="true"
        />

    <TextView
        android:text="Of the Emergency Broadcast"
        android:id="@+id/lv_item_subtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/lv_item_header"
        style="@style/listViewSecondaryDetail"
        android:layout_above="@id/item_separator"
        android:layout_alignParentLeft="true"
        android:ellipsize="marquee"
        android:singleLine="true"
        />
</RelativeLayout>

最后,这里是activity_main xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="290dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:dividerHeight="1dp"
        android:background="#EEEEEE"/>


</android.support.v4.widget.DrawerLayout>

这是布局膨胀的结果:http: //imgur.com/yWPp9ec

如果您有任何建议,请告诉我。

4

1 回答 1

2

我发现了这个问题。它在我的 listViewItem 的样式部分。

<style name="listViewItem">
    <item name="android:layout_height">@dimen/lvItemHeight</item>
    <item name="android:clickable">true</item>
</style>

将 android:clickable 更改为 false 解决了该问题。

于 2013-10-24T02:24:02.367 回答