Custom BaseAdapter ListView onItemClickListener in DrawerLayout Not Called

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

I have a Custom Base Adapter I am using with my ListView in a DrawerLayout. The ListView inflates ok, but I cannot call the onItemClickListener.

Here is the onCreate portion of the MainAcitivty, where I set the adapter and the 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");      } } 

Here is my Base Adapter Code

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;        } } 

Here are my XML Layouts.

Header Layout:

   <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> 

Each line item:

<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> 

Finally, here is the 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> 

Here is the result of the layout inflation: http://imgur.com/yWPp9ec

Please let me know if you have any advice.

回答1:

I found the issue. It was in the style portion of my listViewItem.

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

Changing android:clickable to false fixed the issue.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!