I'm experiencing unusual behaviour with my RecyclerView which is displayed inside a PopupWindow. On my large screen device (Galaxy Note 10.1), it displays fine immediately:
However, on my two smaller screen devices (Nexus 6P and Galaxy Note 8), the padding is gone at first (sorry about the zoom-level/size, the padding should be clear) and there's extra white space below the RecyclerView:
But when I "activate" the item somehow, such as long pressing a 'document' item or normal-clicking a 'category' item (clicking a 'category' item shows or hides its document children items), the padding returns to what it should be:
I've had this happen with Spinners when opening one for the second time, and the hack-fix for that is to re-initialize the adapter when closing the adapter, so it appears to display correctly every time it's opened, instead of just the first.
I don't have different resource files for small and large screens for this layout and I've worked with custom RecyclerView adapters quite a bit. I'm wondering if it's due to it being in a PopupWindow, if that somehow affects it.
Here's where I init the RecyclerView and PopupWindow:
final RecyclerView rv = (RecyclerView) activity.getLayoutInflater().inflate(R.layout.recyclerview, null, false);
rv.setLayoutManager(new DomaLinearLayoutManager(activity));
rv.setAdapter(new DocumentsAdapter(activity, docCategories));
dropdownTV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
popupWindow = new PopupWindow(rv, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, false);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(true);
popupWindow.setContentView(rv);
popupWindow.showAsDropDown(dropdownTV, -5, 0);
}
});
The onBindViewHolder method:
@Override
public void onBindViewHolder(final DocumentsAdapter.ViewHolder holder, int position) {
if (items.get(holder.getAdapterPosition()) instanceof DocumentsCategory) {
TextUtils.setTextAppearance(activity, holder.nameTV, R.style.regularTextLargeBold);
holder.nameTV.setPadding(activity.dpToPx(12), activity.dpToPx(12), activity.dpToPx(5), activity.dpToPx(5));
holder.nameTV.setText(ViewUtils.paintTextEnd(activity, "",
((DocumentsCategory) items.get(holder.getAdapterPosition())).getName(), R.color.domacare_blue, true));
} else { //is a document
holder.nameTV.setPadding(activity.dpToPx(12), activity.dpToPx(12), activity.dpToPx(12), activity.dpToPx(12));
holder.nameTV.setText(((Document) items.get(holder.getAdapterPosition())).getProperName());
holder.nameTV.setTextColor(ContextCompat.getColor(activity, R.color.holo_dark_green));
}
holder.nameTV.setBackgroundResource(R.drawable.action_bar_selector);
holder.nameTV.setSingleLine(false);
if (items.get(holder.getAdapterPosition()) instanceof DocumentsCategory || holder.getAdapterPosition() == items.size() - 1) {
holder.divider.setVisibility(View.GONE);
} else {
holder.divider.setVisibility(View.VISIBLE);
}
}
And the dropdown layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_selector"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
style="@style/blueTextMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_selector"
android:textColor="@drawable/selector_blue_to_accent_blue"
android:ellipsize="marquee"
android:maxWidth="300dp"
android:padding="12dp"
android:singleLine="true"
android:text="" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/light_gray"/>
</LinearLayout>
Updated below with xml files:
recyclerview.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
style="@style/MyListView"/>
spinner_dropdown_tv.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_selector"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
style="@style/blueTextMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_selector"
android:textColor="@drawable/selector_my_blue_to_accent_blue"
android:ellipsize="marquee"
android:maxWidth="300dp"
android:padding="12dp"
android:singleLine="true"
android:text="" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/light_gray"/>
</LinearLayout>
styles.xml:
<style name="MyListView" parent="android:Widget.ListView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:cacheColorHint">@null</item>
<item name="android:background">@color/gray_tasklist</item>
<item name="android:divider">@null</item>
<item name="android:dividerHeight">0dp</item>
<item name="android:fadingEdge">none</item>
<item name="android:focusable">false</item>
<item name="android:focusableInTouchMode">false</item>
<item name="android:descendantFocusability">blocksDescendants</item>
<item name="android:listSelector">@drawable/list_selector_blue</item>
<item name="android:drawSelectorOnTop">true</item>
<item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
<item name="android:scrollbarStyle">outsideOverlay</item>
<item name="android:scrollbars">vertical</item>
<item name="android:fadeScrollbars">false</item>
<item name="android:scrollbarThumbVertical">@drawable/scrollbar_vertical_thumb</item>
<item name="android:scrollbarTrackVertical">@drawable/scrollbar_vertical_track</item>
<item name="android:scrollbarSize">3dp</item>
</style>
<style name="blueTextMedium" parent="android:Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/my_blue</item>
</style>