XML with NestedScrollView + Recyrcleview

0

I have this layout and I want to do a collapsing effect with my header bar .. it's happening more because I'm using a NestedScrollView + Recyrcleview the scrolling gets strange I tried to put android: nestedScrollingEnabled="false" in my Recyrcleview but no It worked, does anyone have any ideas?

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    android:nestedScrollingEnabled="false">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false">

        <!-- image -->
        <FrameLayout
            android:id="@+id/image_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/txtAtleta"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text=" ATLETA : ANDERSON SCHERDOVSKI"
                        android:textColor="@color/colorBlack"
                        android:textSize="18dp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/txtModalidade"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text=" MODALIDADE - MINI RIFLE : SEMI-AUTO-OPEN"
                        android:textColor="@color/colorBlack"
                        android:textSize="16dp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/txtFator"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="FATOR : MENOR"
                        android:textColor="@color/colorBlack"
                        android:textSize="18dp"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/linha">

                    <TextView
                        android:id="@+id/txtPista"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="PISTA : PISTA 2"
                        android:textColor="@color/colorBlack"
                        android:textSize="18dp"
                        android:textStyle="bold" />

                    <CheckBox
                        android:id="@+id/ckbZerar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="ZERAR"
                        android:textColor="@color/colorBlack"
                        android:textSize="18dp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="TEMPO TOTAL"
                        android:textColor="@color/colorBlack"
                        android:textSize="20sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text="HIT FACTOR"
                        android:textColor="@color/colorBlack"
                        android:textSize="20sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp">

                    <TextView
                        android:id="@+id/txtTempo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="10px"
                        android:layout_weight="1"
                        android:background="#DCDCDC"
                        android:gravity="center"
                        android:text="0.00"
                        android:textColor="@color/colorBlack"
                        android:textSize="30dp" />

                    <TextView
                        android:id="@+id/txtHitFactor"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="10px"
                        android:layout_weight="1"
                        android:background="#DCDCDC"
                        android:gravity="center"
                        android:text="0"
                        android:textColor="@color/colorBlack"
                        android:textSize="30dp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <Button
                        android:id="@+id/bt7"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="-10dp"
                        android:layout_weight="1"
                        android:text="7"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/bt8"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="8"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/bt9"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="9"
                        android:textSize="30sp" />

                    <ImageButton
                        android:id="@+id/btD"
                        android:layout_width="match_parent"
                        android:layout_height="62dp"
                        android:layout_marginRight="-10dp"
                        android:layout_weight="1"
                        android:src="@mipmap/ic_backspace" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="-7px">

                    <Button
                        android:id="@+id/bt4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="-10dp"
                        android:layout_weight="1"
                        android:text="4"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/bt5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="5"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/bt6"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="6"
                        android:textSize="30sp" />

                    <ImageButton
                        android:id="@+id/btZ"
                        android:layout_width="match_parent"
                        android:layout_height="62dp"
                        android:layout_marginRight="-10dp"
                        android:layout_weight="1"
                        android:src="@mipmap/ic_clean" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="-7px">

                    <Button
                        android:id="@+id/bt1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="-10dp"
                        android:layout_weight="1"
                        android:text="1"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/bt2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="2"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/bt3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="3"
                        android:textSize="30sp" />

                    <Button
                        android:id="@+id/bt0"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="-10dp"
                        android:layout_weight="1"
                        android:text="0"
                        android:textSize="30sp" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5px"
                        android:layout_weight="1"
                        android:background="@drawable/arrendondar"
                        android:onClick="lancarResultado"
                        android:radius="10dp"
                        android:text="SALVAR"
                        android:textColor="#fff"
                        android:textSize="25dp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="5px"
                        android:layout_weight="1"
                        android:background="@drawable/arrendondar"
                        android:onClick="lancarResultado"
                        android:radius="10dp"
                        android:text="SALVAR E IMPRIMIR"
                        android:textColor="#fff"
                        android:textSize="25dp" />
                </LinearLayout>
            </LinearLayout>
        </FrameLayout>

        <LinearLayout
            android:id="@+id/content_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20px"
            android:background="@color/colorBackground"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/reciclador"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="true"
                android:nestedScrollingEnabled="false" />

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

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5px"
                    android:layout_weight="1"
                    android:background="@drawable/arrendondar"
                    android:onClick="lancarResultado"
                    android:radius="10dp"
                    android:text="SALVAR"
                    android:textColor="#fff"
                    android:textSize="25dp" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5px"
                    android:layout_weight="1"
                    android:background="@drawable/arrendondar"
                    android:onClick="lancarResultado"
                    android:radius="10dp"
                    android:text="SALVAR E IMPRIMIR"
                    android:textColor="#fff"
                    android:textSize="25dp" />
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/toolbar_container"
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:orientation="horizontal">

            <TableLayout style="@style/matchAndWrap">

                <TableRow style="@style/matchAndWrap">

                    <Button
                        android:layout_marginRight="3px"
                        android:layout_weight="1"
                        android:background="@drawable/cell_shape"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="ALVOS" />

                    <Button
                        android:layout_marginLeft="3px"
                        android:layout_weight="1"
                        android:background="@drawable/cell_shape"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="A" />

                    <Button
                        android:layout_weight="1"
                        android:background="@drawable/cell_shape"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="C" />

                    <Button
                        android:layout_marginRight="3px"
                        android:layout_weight="1"
                        android:background="@drawable/cell_shape"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="D" />

                    <Button
                        android:layout_marginLeft="3px"
                        android:layout_weight="1"
                        android:background="@drawable/cell_shape"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="M" />

                    <Button
                        android:layout_weight="1"
                        android:background="@drawable/cell_shape"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="NS" />

                    <Button
                        android:layout_marginRight="6px"
                        android:layout_weight="1"
                        android:background="@drawable/cell_shape"
                        android:gravity="center"
                        android:padding="10dip"
                        android:text="PROC" />
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </FrameLayout>
</android.support.v4.widget.NestedScrollView>

Helper

public class MovingToolbarHelper implements NestedScrollView.OnScrollChangeListener {

private NestedScrollView nestedScrollView;

private ViewGroup imageContainer;

private ViewGroup toolbarContainer;

private ViewGroup contentContainer;

public void bind(@NonNull NestedScrollView nestedScrollView,  @NonNull ViewGroup imageContainer, @NonNull ViewGroup toolbarContainer, @NonNull ViewGroup contentContainer) {
    this.nestedScrollView = nestedScrollView;

    this.imageContainer = imageContainer;
    this.toolbarContainer = toolbarContainer;
    this.contentContainer = contentContainer;

    setup();
}

@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
    // choose appropriate y
    float newY = Math.max(430, scrollY);

    // translate image and toolbar
    ViewCompat.setTranslationY(toolbarContainer, newY);
    ViewCompat.setTranslationY(imageContainer, scrollY * 0f);
}

private void setup() {
    nestedScrollView.setOnScrollChangeListener(this);

    ViewTreeObserver viewTreeObserver = nestedScrollView.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // get size
                int toolbarLinearLayoutHeight = toolbarContainer.getHeight();
                int imageHeight = 430;

                // adjust image frame layout height
                ViewGroup.LayoutParams layoutParams = imageContainer.getLayoutParams();
                if (layoutParams.height != imageHeight) {
                    layoutParams.height = imageHeight;
                    imageContainer.setLayoutParams(layoutParams);
                }

                // adjust top margin of content linear layout
                ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) contentContainer.getLayoutParams();
                if (marginLayoutParams.topMargin != toolbarLinearLayoutHeight + imageHeight) {
                    marginLayoutParams.topMargin = toolbarLinearLayoutHeight + imageHeight;
                    contentContainer.setLayoutParams(marginLayoutParams);
                }

                // call onScrollChange to update initial properties.
                onScrollChange(nestedScrollView, 0, 0, 0, 0);
            }
        });
    }
}

}

Main

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    helper = new MovingToolbarHelper();
    helper.bind(
            binding.nestedScrollView,
            binding.imageContainer,
            binding.toolbarContainer,
            binding.contentContainer);

}
    
asked by anonymous 19.04.2018 / 14:19

0 answers