我试图在列表视图中解决默认背景选择器的问题。
我正在使用具有默认样式的ListView。列表项包含一些TextView和一个ImageButton。当图像按钮不可见时(使用适配器将可见性设置为不可见),背景选择器工作正常(有点击动画)。但是,当ImageButton可见时,单击项目时没有onclick动画(不是按钮)..
列出项目:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10:20:30"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/time"
android:layout_marginStart="15dp"
android:layout_toStartOf="@+id/openMapButton"
android:textStyle="bold"/>
<TextView
android:id="@+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Long long bodylong bodylong bodylong bodylong bodylong bodylong bodylong body"
android:layout_below="@+id/type"
android:layout_alignStart="@id/type"
android:layout_toStartOf="@+id/openMapButton"
android:ellipsize="end"
android:maxLines="2"
/>
<ImageButton
android:id="@+id/openMapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/open_map"
android:background="@android:color/transparent"
android:padding="4dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"/>
的ListView:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".events.EventsHistoryFragment">
<ListView
android:id="@android:id/list"
tools:listitem="@layout/notification_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
适配器:
public class EventsAdapter extends CursorAdapter {
public EventsAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.notification_list_item, parent, false);
EventViewHolder viewHolder = new EventViewHolder();
viewHolder.time = (TextView) view.findViewById(R.id.time);
viewHolder.type = (TextView) view.findViewById(R.id.type);
viewHolder.body = (TextView) view.findViewById(R.id.body);
viewHolder.openMapButton = (ImageButton) view.findViewById(R.id.openMapButton);
viewHolder.openMapButtonListener = new ShowMapButtonClickListener();
viewHolder.openMapButton.setOnClickListener(viewHolder.openMapButtonListener);
view.setTag(viewHolder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
EventViewHolder viewHolder = (EventViewHolder) view.getTag();
long timestamp = cursor.getLong(EventData.TIMESTAMP_COLUMN_ID);
String type = cursor.getString(EventData.TYPE_COLUMN_ID);
String body = cursor.getString(EventData.BODY_COLUMN_ID);
double latitude = cursor.getDouble(EventData.LOCATION_LATITUDE_COLUMN_ID);
double longitude = cursor.getDouble(EventData.LOCATION_LONGITUDE_COLUMN_ID);
String humanReadableType = ServiceEvents.getReadableEventName(context, type);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date date = new Date(timestamp);
viewHolder.time.setText(sdf.format(date));
viewHolder.type.setText(humanReadableType);
viewHolder.body.setText(body);
if(latitude == ServiceEvents.UNKNOWN_COORIDATE) {
viewHolder.openMapButton.setVisibility(View.INVISIBLE);
} else {
viewHolder.openMapButton.setVisibility(View.VISIBLE);
viewHolder.openMapButtonListener.bind(context, latitude, longitude, humanReadableType);
}
}
}
答案 0 :(得分:1)
android列表项的默认行为由选择器决定 ?android:attr / selectableItemBackground 所以你的ListItem的RelativeLayout应该是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/selectableItemBackground"
>
注意最后一个属性 android:background =“?android:attr / selectableItemBackground”