Sarudu Matra
Sarudu Matra

Reputation: 151

BottomSheetBehaviour can't collapse on view pager

I have 3 Fragment's page inside viewpager. In Fragment contains BottomSheetBehaviour. The default state that i set is STATE_EXPANDED.

In every page, I do dragging the panel to make it collapse. Here the scenario :

In page 1 : dragging the panel to bottom (The state changes to STATE_COLLAPSE)

In page 2 : dragging the panel to bottom (The state changes to STATE_COLLAPSE)

In page 3 : dragging the panel to bottom (The state changes to STATE_COLLAPSE)


The problem

when i go back to page 1 the panel changes becoming Expanded (STATE_EXPANDED) by its self. It should be still STATE_COLLAPSE in page 1.

conversely, if i go to the page 3 from page 1, the panel of page 3 becoming up (STATE_EXPANDED) by its self.

so i think the panel becoming up by its self in every 2 page,


My question is : from my problem above how to make the panel still collapse (STATE_COLLAPSE) before i dragging up by my self?

Here my current code :

private void initListenerDragging(){

    ivButtonUpDown.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
    });

    bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged( View bottomSheet, int newState) {

            if(newState==BottomSheetBehavior.STATE_EXPANDED){
                ivButtonUpDown.setImageResource(R.drawable.ic_1491218023_double_arrow_bottom);
                ivButtonUpDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    }
                });
            }else if(newState==BottomSheetBehavior.STATE_COLLAPSED){
                ivButtonUpDown.setImageResource(R.drawable.ic_1491217689_double_arrow_top);
                ivButtonUpDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    }
                });
            }
        }

        @Override
        public void onSlide( View bottomSheet, float slideOffset) {
        }
    });
}

Upvotes: 2

Views: 393

Answers (2)

Nilabja
Nilabja

Reputation: 4276

by default viepager only loads the one fragment which is to each side of the current fragment. To overcome this you could manually set the number of fragments to load by simply putting it like this

viewPager.setOffscreenPageLimit(2);

here 2 can be changed as per your need

Upvotes: 1

Aldrin Joe Mathew
Aldrin Joe Mathew

Reputation: 482

The view pager keeps those fragments that are immediate left or right of the current fragment in memory. That means when you're in the first fragment only second one is in memory, third fragment created only when you swipe right.

The same thing happens when you're at third fragment, only second one is in memory (the one which is immediately close to it, and not the first fragment). The first fragment is recreated and brought to memory only when you swipe left.

Upvotes: 0

Related Questions