简体   繁体   中英

Hide toolbar on scroll when the using a recyclerview inside a Fragment instead of an Activity

I'm trying to adapt the strategy for hiding / showing a toolbar (or any visual element) from the well explained and great article: http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/

But in my case I'm using a Fragment to hold the recycleview instead of the activity. My problem is that the padding is not being applied so the first element is under the toolbar, and I have also another strange behavior, as the toolbar is also under the statusbar. I don't know what is happening here. The following are my "moving pieces":

BasicActivity.java: based on the one given on the previous post, but moving away the recycleview part as is going to be on the Fragment piece. Also it exposes the show and hide methods to allow the fragment to access it:

public class BasicActivity extends ActionBarActivity {

    private Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic);
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container,new RecycleFragment())
                .commit();
        overridePendingTransition(0, 0);
        initToolbar();
    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        setTitle(getString(R.string.app_name));
        mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
    }

    public void hideViews() {
        mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2));

    }

    public void showViews() {
        mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));   
    }

}

My activiy_basic.xml is the following:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
    <include layout="@layout/toolbar_actionbar" />
</FrameLayout>

The layout toolbar_actionbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:clipToPadding="false"/>

The Fragment RecycleFragment.java: public class RecycleFragment extends Fragment {

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


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initRecyclerView(view);

}

private void initRecyclerView(View view) {
    RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    RecyclerAdapter recyclerAdapter = new RecyclerAdapter(createItemList());
    recyclerView.setAdapter(recyclerAdapter);

    recyclerView.setOnScrollListener(new HidingScrollListener() {
        @Override
        public void onHide() {
            ((BasicActivity)getActivity()).hideViews();
        }

        @Override
        public void onShow() {
            ((BasicActivity)getActivity()).showViews();
        }
    });
}

private List<String> createItemList() {
    List<String> itemList = new ArrayList<>();
    for(int i=0;i<20;i++) {
        itemList.add("Item "+i);
    }
    return itemList;
}

} And the layout for the fragment is just a recyclerview fragment_recycler.xml:

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

The adapter and the viewholder for the recycler are the same as the article, and they doesn't affect the behavior.

What is wrong with the code?

UPDATE: A Michał Z. below pointed out. What was missing is the paddingTop and clipptoPadding on the Recyclerview view So the final xml should be:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"/>

And to solve the statusbar overlapping problem, it is needed to add a "fitsystemwindows" = "true" element on the activity layout. So it must be as the following:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">        
    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
    <include layout="@layout/toolbar_actionbar" />
</FrameLayout>

UPDATE2 The fitSystemWindows is only needed if the theme is setting the statusbar as translucent

Your fragment_recycler.xml file is missing paddingTop and clipToPadding attributes. It should look like this:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"/>

And also remove clipToPadding from your toolbar_actionbar.xml .

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