简体   繁体   中英

Collapsing Toolbar Layout not working in Fragment - Android

When i try to impliment collapsingbar in fragment it's not collapsing, but when i try the same code in a simple activity it works fine, and i want the same design in a fragment(ProfileFragment), there is no error occurs it just simply displayed but its not collapsing, and i want to make it working fine please help me, look on my code i want to impliment.

This is my XML

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    tools:context="com.turbo.ashish.hexon.BottomNavFragment.ProfileFragment">

    <!-- TODO: Update blank fragment layout -->

    <android.support.design.widget.AppBarLayout
        android:id="@+id/idProfileWallappbar"
        android:layout_width="match_parent"
        android:layout_height="238dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/idcollapsbarlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorAccent"
            app:expandedTitleMarginBottom="20dp"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="20dp"
            app:layout_scrollFlags="exitUntilCollapsed|scroll"
            app:title="@string/title_profile">

            <ImageView
                android:id="@+id/idImageViewCollapsingToolbarLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/idtoolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/custColtransparante"
                app:layout_collapseMode="pin">

            </android.support.v7.widget.Toolbar>

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

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

    <Button
        android:id="@+id/idFragBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="304dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

My Fragment Java File..

package com.turbo.ashish.hexon.BottomNavFragment;


import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.turbo.ashish.hexon.R;

import static android.widget.Toast.LENGTH_LONG;


public class ProfileFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        Log.d("Working","Wasd");
        View fragView = inflater.inflate(R.layout.fragment_profile,container,false);
        fragView.findViewById(R.id.idFragBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"asd",Toast.LENGTH_LONG).show();
            }
        });

        return fragView;
    }

}

But Collapsing bar layout always stay constant in Fragment, i want to make this collapsingbar working on fragment (ProfileFragment)..

My Platform activity where i load fragments like:

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @SuppressLint("ResourceType")
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();


            switch (item.getItemId()){
                case R.id.idNavContacts:
                    toolbar.setTitle("Contacts");
                    ft.replace(R.id.idFrameContainer, new ContactsFragment(), "Contacts Ftagment").commit();
                    return true;

                case R.id.idNavPerson:
                    toolbar.setTitle("Profile");
                    ft.replace(R.id.idFrameContainer,new ProfileFragment(), "Profile Fragment").commit();
                    return true;

                case R.id.idNavGroups:
                    toolbar.setTitle("Groups");
                    ft.replace(R.id.idFrameContainer,new GroupsFragment(), "Groups Fragment").commit();
                    return true;

                case R.id.idNavFeeds:
                    toolbar.setTitle("Feeds");
                    ft.replace(R.id.idFrameContainer, new FeedskFragment(), "Feeds Fragment").commit();
                    return true;
            }
            return false;
        }
    };

There is no problem in loading fragment but the Collapsing bar not responds (No sliding), i want to happen in fragment.

Activity XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.turbo.ashish.hexon.Platform">

    <FrameLayout
        android:id="@+id/idFrameContainer"
        android:layout_width="match_parent"
        android:layout_height="454dp">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/idBottomNav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@color/WHITE"
        android:foreground="?attr/selectableItemBackground"
        app:itemIconTint="@color/deepViolate"
        app:itemTextColor="@color/deepViolate"
        app:menu="@menu/navigation">
    </android.support.design.widget.BottomNavigationView>

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

Why my Collapsing Toolbar Layout not working ?

You expect your collapsing toolbar to work in Constraint Layout ?

Firstly, to make the collapsing toolbar work make

android.support.design.widget.CoordinatorLayout

as your root layout.

在下面的代码中将android.support.constraint.ConstraintLayout的代码理解为XML文件,并将其更改为房间xml标签..

<android.support.design.widget.CoordinatorLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM