DrawerLayout CoordinatorLayout AppBarLayout Fragment 没有在 Scroll 上显示和隐藏

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

我想做一些 post 中描述的事情,但我无法让隐藏和显示滚动起作用。一切都在显示,抽屉菜单、header 视图和列表视图,但是当我上下滚动列表视图时,header 视图停留在那里,不会像列表视图那样隐藏和显示正在滚动。我已经查找了其他一些帖子来寻找解决方案,但 none 有帮助。

Android design library CoordinatorLayout, AppBarLayout and DrawerLayout

我有一个 Activity,其中包含一个抽屉菜单和一个片段。在片段中,有 CoordinatorLayout 和 AppBarLayout,我想在列表视图滚动时显示和隐藏 header 视图。

以 DrawerLayout 作为根视图的 Activity 布局包含主要内容的 FrameLayout 和抽屉菜单内容的 RelativeLayout。

<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>

我想在列表视图向上滚动时隐藏 header 视图并在列表视图向下滚动时显示 header 视图的 Fragment 布局。

<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>

应用主题是

Theme.AppCompat.Light.DarkActionBar

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;
        }
    }
}

片段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;
        }
    }


}

根据这个 post:CoordinatorLayout 仅适用于 RecyclerViewNestedScrollView,所以我建议您更改 ListViewRecyclerView