问题一:Android 6.0+ RecyclerView嵌套在ScrollView显示不全
解决方式:修改布局,在RecyclerView的外面嵌套一层RelativeLayout,代码如下:
<!--包一层RelativeLayout解决在6.0及以上系统中RecyclerView显示不全的问题-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/home_rlv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
亲测,该方法可以有效解决RecyclerView显示不全的问题,而android:descendantFocusability=”blocksDescendants”属性好像是可有可无的,试了一下去掉也没问题。
问题二:Android 7.0+ PopupWindow位置显示不对的问题解决
出现的问题大概张这样,如下:
正确显示的样子(这个图是我偷别人的,希望原作者不会追究呀,我会在文章末尾给出几篇解决这个问题的其他文章):
错误显示的样子:
估计大家遇到的也是这个问题吧,下面给出解决方式,我们要做的就是重写PopupWindow的showAsDropDown方法,如下:
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by zhagnlei on 2018/3/30.
* 这个popupWindow是用来解决在7.0以及8.0中位置显示不正确的
*/
public class PopupWindow extends android.widget.PopupWindow {
public PopupWindow(Context context) {
super(context);
}
public PopupWindow(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public PopupWindow() {
}
public PopupWindow(View contentView) {
super(contentView);
}
public PopupWindow(int width, int height) {
super(width, height);
}
public PopupWindow(View contentView, int width, int height) {
super(contentView, width, height);
}
public PopupWindow(View contentView, int width, int height, boolean focusable) {
super(contentView, width, height, focusable);
}
@Override
public void showAsDropDown(View anchorView, int xoff, int yoff) {
if (Build.VERSION.SDK_INT >= 24) {
Rect visibleFrame = new Rect();
anchorView.getGlobalVisibleRect(visibleFrame);
int height = anchorView.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
super.setHeight(height);
super.showAsDropDown(anchorView, xoff, yoff);
} else {
super.showAsDropDown(anchorView, xoff, yoff);
}
}
@Override
public void showAsDropDown(View anchorView) {
if (Build.VERSION.SDK_INT >= 24) {
Rect visibleFrame = new Rect();
anchorView.getGlobalVisibleRect(visibleFrame);
int height = anchorView.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
super.setHeight(height);
super.showAsDropDown(anchorView);
} else {
super.showAsDropDown(anchorView);
}
}
}
嘿嘿,代码虽说是这么写的,但是我还没有测试,如果有小伙伴看到这篇文章也可以帮我测试一下,谢谢啦。
这是一篇类似的文章(图片我就是从这里偷的),点击查看