1. 程式人生 > >Android 6.0+ RecyclerView巢狀在ScrollView顯示不全以及Android 7.0+ PopupWindow位置顯示不對的問題解決

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

}

嘿嘿,程式碼雖說是這麼寫的,但是我還沒有測試,如果有小夥伴看到這篇文章也可以幫我測試一下,謝謝啦。

這是一篇類似的文章(圖片我就是從這裡偷的),點選檢視