Search code examples
androidandroid-fragmentsandroid-coordinatorlayout

DrawerLayout CoordinatorLayout AppBarLayout Fragment are not doing show and hide on Scroll


enter image description here I want to do something described in this post, but I couldn't get the hide and show on scroll to work. Everything is showing, the drawer menu, the header view and the listview, but when I scroll the listview up and down, the header view stays there, doesn't hide and show as the list view is being scrolled. I've looked up few other posts such as these for a solution, but none of them helped.

Android design library CoordinatorLayout, AppBarLayout and DrawerLayout

DrawerLayout + CollapsingToolbar + Fragments; Toolbar won't collapse or show image

I have an Activity which contains a drawer menu and a fragment. In the Fragment, there is the CoordinatorLayout and AppBarLayout that I want to do show and hide the header view on list view scrolling.

The Activity layout with the DrawerLayout as the root view containing a FrameLayout for the main content and the RelativeLayout for the drawer menu content.

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:clickable="true"/>


    <RelativeLayout
        android:id="@+id/left_drawer"
        android:layout_height="match_parent"
        android:layout_width="280dp"
        android:layout_gravity="start"
        android:background="#eee">

        <RelativeLayout
            android:id="@+id/sliding_menu_logo_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#eee">
            <ImageView
                android:id="@+id/sliding_menu_logo"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="20dp"
                android:layout_centerInParent="true"
                android:scaleType="centerInside"
                android:src="@drawable/ic_launcher" />
        </RelativeLayout>
        <ListView
            android:id="@+id/list"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_below="@id/sliding_menu_logo_container"
            android:clipToPadding="true"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:drawSelectorOnTop="false"
            android:fastScrollEnabled="false"
            android:scrollbarStyle="outsideOverlay" />
    </RelativeLayout>

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

The Fragment layout where I want to hide the header view when list view is scrolling up and show the header view when the list view is scrolling down.

<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.support.design.widget.AppBarLayout
        android:id="@+id/my_appbar_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#eee"
            app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"
                    android:text="First Name"/>
                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"
                    android:text="Last Name"/>
                <EditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"/>
            </LinearLayout>
        </LinearLayout>
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <ListView
            android:id="@+id/rv_numbers"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

The app theme is

Theme.AppCompat.Light.DarkActionBar

The Activity class

public class ScrollingActivity4 extends AppCompatActivity {

    protected DrawerLayout drawerLayout;
    RelativeLayout leftDrawerView;
    protected ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling4);

        ScrollingActivity4Fragment scrollingActivity4Fragment = new ScrollingActivity4Fragment();
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.content_frame, scrollingActivity4Fragment, "tag_scrollingActivity4Fragment")
                .addToBackStack(null)
                .commit();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_launcher);


        if (drawerToggle == null) {
            drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.drawer_open, R.string.drawer_close) {
                public void onDrawerClosed(View view) {
                }

                public void onDrawerOpened(View drawerView) {

                }

                public void onDrawerSlide (View drawerView, float slideOffset) {
                }

                public void onDrawerStateChanged(int newState) {

                }

            };
            drawerLayout.setDrawerListener(drawerToggle);
        }

        drawerToggle.syncState();

        leftDrawerView = (RelativeLayout) findViewById(R.id.left_drawer);
        ListView rvNumbers = (ListView) findViewById(R.id.list);

        String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
        ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(this, R.layout.list_item, numbers);
        rvNumbers.setAdapter(itemArrayAdapter);
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch (item.getItemId()) {
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public class ItemArrayAdapter extends ArrayAdapter<String> {
        String[] itemList;
        private int listItemLayout;
        public ItemArrayAdapter(Context context, int layoutId, String[] itemList) {
            super(context, layoutId, itemList);
            listItemLayout = layoutId;
            this.itemList = itemList;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            int pos = position;
            String item = getItem(pos);

            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(listItemLayout, parent, false);
                viewHolder.item = (TextView) convertView.findViewById(R.id.tv_number);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.item.setText(item);
            return convertView;
        }
        class ViewHolder {
            TextView item;
        }
    }
}

The Fragment Class

public class ScrollingActivity4Fragment extends Fragment {
    LinearLayout llHeader;
    ListView rvNumbers;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_scrolling4_fragment, container, false);

        llHeader = (LinearLayout) view.findViewById(R.id.ll_header);
        rvNumbers = (ListView) view.findViewById(R.id.rv_numbers);

        String [] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};

        ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(getActivity(), R.layout.list_item, numbers);
        rvNumbers.setAdapter(itemArrayAdapter);

        return view;
    }

    public class ItemArrayAdapter extends ArrayAdapter<String> {
        String[] itemList;
        private int listItemLayout;
        public ItemArrayAdapter(Context context, int layoutId, String[] itemList) {
            super(context, layoutId, itemList);
            listItemLayout = layoutId;
            this.itemList = itemList;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            int pos = position;
            String item = getItem(pos);

            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(listItemLayout, parent, false);
                viewHolder.item = (TextView) convertView.findViewById(R.id.tv_number);
                convertView.setTag(viewHolder); // view lookup cache stored in tag
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.item.setText(item);
            return convertView;
        }
        class ViewHolder {
            TextView item;
        }
    }


}

Solution

  • According to this post: ScrollingViewBehavior for ListView, CoordinatorLayout works only with RecyclerView and NestedScrollView, so I suggest you to change the ListView to RecyclerView