我在使用我的RecyclerView时遇到异常行为,它显示在PopupWindow中。在我的大屏幕设备(Galaxy Note 10.1)上,它立即显示正常:
然而,在我的两个较小的屏幕设备(Nexus 6P和Galaxy Note 8)上,填充物一开始就消失了(抱歉缩放级别/大小,填充应该是清晰的)并且RecyclerView下面有额外的空白区域:
但是,当我以某种方式“激活”该项目时,例如长按“文档”项目或正常单击“类别”项目(单击“类别”项目显示或隐藏其文档子项目),填充返回应该是什么:
我第二次打开时会让Spinners发生这种情况,而修复它的方法是在关闭适配器时重新初始化适配器,因此每次打开它时它都会显示正确只是第一个。
对于这种布局,我没有针对小屏幕和大屏幕的不同资源文件,而且我已经使用了自定义的RecyclerView适配器。我想知道它是否是由于它在PopupWindow中,如果它以某种方式影响它。
这是我启动RecyclerView和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);
}
});
onBindViewHolder方法:
@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);
}
}
下拉列表:
<?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>
下面更新了xml文件:
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>