在Fragment中,从布局中删除空白:CollapsingToolbarLayout + NestedScrollView + ViewPager

时间:2018-06-21 12:20:41

标签: android scroll android-viewpager

问题: 无法找到删除空白空间布局的解决方案 图片:

Check this image link

GIF图片: GIF Image link

主屏幕是我的HomeFragment,在其中我使用CollapsingToolbarLayoutNestedScrollViewViewPager,并且在ViewPager中,下面是Load 3 Fragments代码

private void setupViewPager(ViewPager viewPager) {
        HomeFragment.ViewPagerAdapter adapter = new HomeFragment.ViewPagerAdapter(getChildFragmentManager());
        adapter.addFragment(new InstantFragment(), "Instant");
        adapter.addFragment(new LimitFragment(), "Limit");
        adapter.addFragment(new StopFragment(), "Stop");
        viewPager.setAdapter(adapter);
    }

这是我的 fragment_home.xml 代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_home_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_app_background">

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleGravity="top"
            app:expandedTitleMarginStart="10dp"
            app:expandedTitleMarginTop="20dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <LinearLayout
                android:id="@+id/llChild"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <RelativeLayout
                    android:id="@+id/llview_relative"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <Spinner
                        android:id="@+id/spinner_currency"
                        android:layout_width="200dp"
                        android:layout_height="40dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="20dp" />


                    <Button
                        android:id="@+id/btndeposit"
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:layout_alignParentRight="true"
                        android:layout_marginLeft="@dimen/margin10"
                        android:layout_marginRight="@dimen/margin20"
                        android:background="@color/pink"
                        android:text="Deposit"
                        android:textAllCaps="false"
                        android:textColor="@color/white"
                        android:textSize="16dp" />


                </RelativeLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="256dp"
                    android:layout_margin="@dimen/margin10"
                    android:layout_marginTop="@dimen/margin10"
                    android:orientation="vertical"
                    android:background="@mipmap/ic_app_background">

                    <com.github.mikephil.charting.charts.CandleStickChart
                        android:id="@+id/chart1"
                        android:visibility="gone"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />

                    <WebView

                    android:id="@+id/webView"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:fitsSystemWindows="true"
                    android:scrollbars="none" />


                </LinearLayout>
                <TextView
                    android:id="@+id/txt_my_opn_order"
                    android:layout_marginTop="@dimen/margin10"
                    android:text="@string/my_open_orders_title"
                    android:textSize="17sp"
                    android:fontFamily="@font/poppins_light"
                    android:textColor="@color/notification_title"
                    android:layout_marginLeft="@dimen/margin5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <FrameLayout
                    android:id="@+id/sub_frame_container"
                    android:layout_width="match_parent"
                    android:visibility="gone"
                    android:animateLayoutChanges="true"
                    android:layout_height="wrap_content"/>


            </LinearLayout>

        </android.support.design.widget.CollapsingToolbarLayout>

        <TextView
            android:layout_marginTop="@dimen/margin10"
            android:text="@string/new_orders_title"
            android:textSize="17sp"
            android:fontFamily="@font/poppins_light"
            android:textColor="@color/notification_title"
            android:layout_marginLeft="@dimen/margin5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">



        <com.bacancy.kryptonight.customviews.CustomViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

这是CustomViewPager

的代码
public class CustomViewPager extends ViewPager {
public CustomViewPager(Context context) {
    super(context);
}

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int height = 0;
    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if (h > height) height = h;
    }
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

这是第一个片段 fragment_instant.xml 代码

<?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"
    android:background="#2C2f33"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_margin="@dimen/margin20"
                android:layout_marginTop="@dimen/margin10"
                android:gravity="center"
                android:text="@string/spend_title"
                android:textColor="@color/white"
                android:textSize="@dimen/margin20" />

            <EditText
                android:id="@+id/edtspend"
                android:layout_width="match_parent"
                android:layout_height="@dimen/edittext_height"
                android:layout_marginLeft="@dimen/margin20"
                android:layout_marginRight="@dimen/margin20"
                android:textColor="@color/white"
                android:background="@color/edittext_background"
                android:hint="Amount"
                android:maxLength="20"
                android:inputType="number"
                android:paddingLeft="@dimen/margin5" />

            <TextView
                android:id="@+id/txtsubtotal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin20"
                android:layout_marginRight="@dimen/margin20"
                android:layout_marginTop="@dimen/margin10"
                android:visibility="gone"
                android:text="@string/subtotal"
                android:textColor="@color/textcolor" />


            <TextView
                android:id="@+id/txtfee"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin20"
                android:visibility="gone"
                android:text="@string/fee"
                android:textColor="@color/textcolor" />

            <CheckBox
                android:layout_margin="@dimen/margin20"
                android:id="@+id/chkisPartialBuy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin20"
                android:buttonTint="@color/button_buy"
                android:text="@string/is_partial_title"
                android:textColor="@color/textcolor"
                android:textSize="16dp" />


            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_marginLeft="@dimen/margin20"
                android:layout_marginRight="@dimen/margin20"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/approx"
                    android:textColor="@color/textcolor" />
                <TextView
                    android:layout_marginLeft="@dimen/margin10"
                    android:id="@+id/txtapprox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:text="@string/approx_def_price_buy"
                    android:textColor="@color/textcolor" />

            </LinearLayout>

            <Button
                android:id="@+id/btnbuy"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin20"
                android:layout_marginRight="@dimen/margin20"
                android:layout_marginTop="@dimen/margin30"
                android:background="@color/button_buy"
                android:text="@string/buy"
                android:textColor="@color/white" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="@dimen/margin20"
                android:background="@color/line" />


            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_margin="@dimen/margin20"
                android:layout_marginTop="@dimen/margin10"
                android:gravity="center"
                android:text="@string/sell_title"
                android:textColor="@color/white"
                android:textSize="@dimen/margin20" />

            <EditText
                android:id="@+id/edtsell"
                android:layout_width="match_parent"
                android:layout_height="@dimen/edittext_height"
                android:layout_marginLeft="@dimen/margin20"
                android:textColor="@color/white"
                android:inputType="numberDecimal"
                android:layout_marginRight="@dimen/margin20"
                android:maxLength="20"
                android:background="@color/edittext_background"
                android:hint="Amount"
                android:paddingLeft="@dimen/margin5" />

            <TextView
                android:id="@+id/txtsubtotal_sell"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin20"
                android:layout_marginRight="@dimen/margin20"
                android:layout_marginTop="@dimen/margin10"
                android:text="@string/subtotal"
                android:visibility="gone"
                android:textColor="@color/textcolor" />


            <TextView
                android:id="@+id/txtfee_sell"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin20"
                android:visibility="gone"
                android:text="@string/fee"
                android:textColor="@color/textcolor" />

            <CheckBox
                android:layout_margin="@dimen/margin20"
                android:id="@+id/chkisPartialSell"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/margin20"
                android:buttonTint="@color/button_sell"
                android:text="@string/is_partial_title"
                android:textColor="@color/textcolor"
                android:textSize="16dp" />
            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_marginLeft="@dimen/margin20"
                android:layout_marginRight="@dimen/margin20"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/approx"
                    android:textColor="@color/textcolor" />
                <TextView
                    android:id="@+id/txtapprox_sell"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/margin10"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:text="@string/approx_def_price_sell"
                    android:textColor="@color/textcolor" />

            </LinearLayout>

            <Button
                android:id="@+id/btnsell"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/margin60"
                android:layout_marginLeft="@dimen/margin20"
                android:layout_marginRight="@dimen/margin20"
                android:layout_marginTop="@dimen/margin30"
                android:background="@color/button_sell"
                android:text="@string/sell"
                android:textColor="@color/white" />


        </LinearLayout>

    </ScrollView>
</RelativeLayout>
  

我检查CustomViewPager类,其中高度设置了如何将当前高度视图设置为WRAP_CONTENT

0 个答案:

没有答案