【问题标题】:How to put an image slider below webview content?如何在 webview 内容下方放置图像滑块?
【发布时间】:2018-06-29 00:08:31
【问题描述】:

我正在使用自定义视图寻呼机将多个图像显示为滑块。它工作正常,但它在 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);
    }
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 2014-07-09
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多