DrawerLayout の ListView で使用しているカスタム ベース アダプターがあります。ListView は正常に膨張しますが、onItemClickListener を呼び出すことができません。
これは MainActivty の 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
何かアドバイスがあれば教えてください。