android-layoutandroid-listviewandroid-recyclerviewandroid-4.1-jelly-bean

Ending multiselect in a listview removes list item padding


I can only reconstruct this behavior with a JellyBean device (Android 4.1.2 on a Samsung Galaxy S3). With KitKat or Lollipop this problem did not occur.

I have a RecyclerView with a list of items which possible multiselect. When I select a few items in the list (the multiselect gets activated) and exit it again by clicking the icon in the top left of the toolbar some items in the list lose their padding. The weird thing is it does not always happen to the same items an also not to the same ones each time. When I scroll the item out of the display and scroll it back into view the padding is back and correct.

enter image description here

After I overwrote the onRebind() method and re-applied the padding to the item the problem didn't happen as much anymore - BUT - it stll happens.

@Override
protected void onRebind() {
    super.onRebind();
    mListItem.setPadding(20, 20, 20, 20);
}

This iy my DocumentHolder class:

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

And this the layout XML file for the list item:

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

how can I fix the behavior that the padding remains the 12dp defined in the layout XML file after quitting the multiselect mode?


Solution

  • Finally found the solution!

    The problem lays in the layout XML file:

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

    The background attribute apparenly isn't valid for Jelly Bean devices. Changing the value from

    android:background="?android:attr/selectableItemBackground"
    

    to

    android:background="@color/myBackgroundColor"
    

    for pre-KitKat devices solved the problem.