ヘッダー要素とヘッダー以外の要素を含む listView を生成します。ヘッダーはクリック時に onItemClickListener を呼び出しますが、ヘッダー以外の要素は呼び出しません。非ヘッダー要素がクリックされると強調表示されますが、onItemClickListener への呼び出しはありません。
ヘッダーは Line Separator と 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 の と の設定を試みましたが、何も機能しないようです。誰が問題が何であるか知っていますか?