如何将图像滑块放在Webview内容下方?

时间:2018-06-29 00:08:32

标签: android android-layout android-viewpager android-imageview android-view

我正在使用自定义视图传呼机将多个图像显示为滑块。它可以正常工作,但会在加载Webview内容(Webview内容为空)之前加载图像大约一秒钟,然后在布局中移至下方。如何避免在空的Webview内容中加载图片?我已经搜索了stackoverflow,但是找不到合适的答案。

主要布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true">

    <include
        android:id="@+id/newsPageToolbar"
        layout="@layout/toolbar" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/newsPageToolbar"
        android:background="@android:color/white"
        android:fillViewport="true"
        android:fitsSystemWindows="true"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                    <TextView
                        android:id="@+id/newsTitleSinglePage"
                        style="@style/Base.TextAppearance.AppCompat.Body2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:includeFontPadding="false"
                        android:paddingBottom="0dp"
                        android:paddingLeft="8dp"
                        android:paddingRight="8dp"
                        android:paddingTop="15dp"
                        android:textSize="24sp" />

                    <TextView
                        android:id="@+id/publishDateSinglePage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="10dp"
                        android:paddingLeft="8dp"
                        android:paddingRight="8dp"
                        android:paddingTop="5dp"
                        android:textSize="14sp" />

                    <ImageView
                        android:id="@+id/imageSinglePage"
                        android:layout_width="match_parent"
                        android:layout_height="230dp"
                        android:adjustViewBounds="true"
                        android:paddingLeft="8dp"
                        android:paddingRight="8dp"
                        android:scaleType="centerCrop" />

                    <TextView
                        android:id="@+id/newsSourceSinglePage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="10dp"
                        android:paddingLeft="8dp"
                        android:paddingRight="8dp"
                        android:paddingTop="2dp"
                        android:textSize="14sp" />

                    <WebView
                        android:id="@+id/newsBody"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"></WebView>

                    <WebView
                        android:id="@+id/videoView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"></WebView>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                            <com.eastnews.huseyn.eastnews.WrapContentViewPager
                                android:id="@+id/imagePager"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
                            <me.relex.circleindicator.CircleIndicator
                                android:id="@+id/imageIndicator"
                                android:layout_width="match_parent"
                                android:layout_height="48dp"
                                android:layout_alignParentBottom="true"/>
                    </RelativeLayout>

                    <TextView
                        android:id="@+id/newsLink"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="10dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="5dp"
                        android:text="@string/link" />
            </LinearLayout>
    </ScrollView>

   </RelativeLayout>

图像布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageSlider"
    android:layout_width="match_parent"
    android:layout_height="230dp"
    android:adjustViewBounds="true"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:scaleType="centerCrop" />
</RelativeLayout>

ViewPagerAdapter类的onMeasure方法

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED;

    if(wrapHeight) {
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        widthMeasureSpec = MeasureSpec.makeMeasureSpec(width,MeasureSpec.EXACTLY);

        if(getChildCount() > 0) {
            View firstChild = getChildAt(0);

            firstChild.measure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(height,MeasureSpec.UNSPECIFIED));
            height = firstChild.getMeasuredHeight();
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    }
}

1 个答案:

答案 0 :(得分:0)

我已经解决了使用可见性消失的问题。首先,将可见性设置在布局xml中。然后使用webview onPageFinished(WebView view, String url){...}函数使视图可见。