Android 6.0+ RecyclerView嵌套在ScrollView显示不全以及Android 7.0+ PopupWindow位置显示不对的问题解决

问题一: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);
        }
    }

}

嘿嘿,代码虽说是这么写的,但是我还没有测试,如果有小伙伴看到这篇文章也可以帮我测试一下,谢谢啦。

这是一篇类似的文章(图片我就是从这里偷的),点击查看

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值