我只能使用JellyBean设备(Samsung Galaxy S3上的Android 4.1.2)重建此行为。使用KitKat或Lollipop时,没有出现此问题。
我有一个RecyclerView,其中包含可能多选的项目列表。当我在列表中选择一些项目(多选项被激活)并通过单击工具栏左上角的图标再次退出时,列表中的某些项目将丢失其填充。奇怪的是它并不总是发生在同一个项目上,每次都不会发生在同一个项目上。当我将项目滚出显示器并将其向后滚动到视图中时,填充回来并且正确。
在我覆盖了onRebind()方法并将填充重新应用到项目后,问题不再发生了 - 但是 - 它会发生。
@Override
protected void onRebind() {
super.onRebind();
mListItem.setPadding(20, 20, 20, 20);
}
这是我的DocumentHolder类:
public class DocumentHolder extends SwappingHolder implements View.OnClickListener, View.OnLongClickListener {
private final RelativeLayout mListItem;
private final TextView mNameTextView;
private final ImageView mAttachmentView;
private final TextView mTitleTextView;
private final TextView mDateView;
private final ImageView mTypeView;
private Document mDocument;
private boolean mIsAvailableOffline;
@Override
protected void onRebind() {
super.onRebind();
mListItem.setPadding(20, 20, 20, 20);
}
public DocumentHolder(View itemView, MultiSelector multiSelector) {
super(itemView, multiSelector);
mListItem = (RelativeLayout) itemView.findViewById(R.id.list_item_doc);
mNameTextView = (TextView) itemView.findViewById(R.id.list_item_document_name);
mAttachmentView = (ImageView) itemView.findViewById(R.id.list_item_document_attachment);
mDateView = (TextView) itemView.findViewById(R.id.list_item_document_date);
mTitleTextView = (TextView) itemView.findViewById(R.id.list_item_document_title);
mTypeView = (ImageView) itemView.findViewById(R.id.list_item_document_type);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
itemView.setLongClickable(true);
itemView.setLongClickable(true);
}
public void bindDocument(Document document) {
mDocument = document;
mNameTextView.setText(document.getDisplayName(mFolderType));
mAttachmentView.setVisibility(document.isHasAttachments() ? View.VISIBLE : View.INVISIBLE);
mTitleTextView.setText(document.getTitle());
int iconId = IconHelper.getListItemIcon(document, true);
mTypeView.setImageResource(iconId);
mDateView.setText(DateFormatter.getShortFormattedDate(document.getReceivingDate()));
mNameTextView.setTextAppearance(mContext, R.style.ListItemSenderUnread);
mTitleTextView.setTextAppearance(mContext, R.style.ListItemTitleUnread);
mDateView.setTextAppearance(mContext, R.style.ListItemDateUnread);
// offline style
mIsAvailableOffline = mAttachmentService.isAvailableOffline(mDocument.getId());
if (mOfflineMode) {
if (!mIsAvailableOffline) {
mNameTextView.setTextAppearance(mContext, R.style.ListItemSenderOffline);
mTitleTextView.setTextAppearance(mContext, R.style.ListItemTitleOffline);
mDateView.setTextAppearance(mContext, R.style.ListItemDateOffline);
}
}
}
@Override
public void onClick(View v) {
if (!mOfflineMode || mIsAvailableOffline) {
mItemListAdapterCallbacks.onDocumentClick(this);
}
}
@Override
public boolean onLongClick(View v) {
if (!mOfflineMode) {
mItemListAdapterCallbacks.onDocumentLongClick(this);
}
return true;
}
public Document getDocument() {
return mDocument;
}
}
这是列表项的布局XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_doc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="12dp">
<TextView
android:id="@+id/list_item_document_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/list_item_document_attachment"
android:ellipsize="end"
android:gravity="center_vertical"
android:minHeight="24dp"
android:singleLine="true" />
<ImageView
android:id="@+id/list_item_document_attachment"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignBottom="@id/list_item_document_name"
android:layout_toLeftOf="@+id/list_item_document_date"
android:padding="4dp"
android:src="@drawable/ic_attachment_gray" />
<TextView
android:id="@+id/list_item_document_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/list_item_document_name"
android:layout_alignParentRight="true"
android:ellipsize="end"
android:gravity="center_vertical"
android:minHeight="24dp"
android:singleLine="true" />
<TextView
android:id="@+id/list_item_document_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/list_item_document_type"
android:layout_alignParentLeft="true"
android:layout_below="@+id/list_item_document_name"
android:layout_toLeftOf="@+id/list_item_document_type"
android:ellipsize="end"
android:gravity="center_vertical"
android:minHeight="24dp"
android:paddingTop="5dp"
android:singleLine="true" />
<ImageView
android:id="@+id/list_item_document_type"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/list_item_document_date"
android:src="@drawable/ic_folder" />
</RelativeLayout>
退出多选模式后,如何修复填充保留在布局XML文件中定义的12dp的行为?
答案 0 :(得分:0)
终于找到了解决方案!
问题在于布局XML文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_doc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="12dp">
背景属性对于Jelly Bean设备来说似乎并不有效。从
更改值android:background="?android:attr/selectableItemBackground"
到
android:background="@color/myBackgroundColor"
用于预KitKat设备解决了这个问题。