Collapsing/Expanding view coordinated with Sliding RecyclerView

前端 未结 2 1528
暖寄归人
暖寄归人 2020-12-30 08:41

I am trying to achieve the following affect (also see image below):

  • the app opens with a view (map) partially visible and the RecyclerView at a de
2条回答
  •  星月不相逢
    2020-12-30 08:44

    To create this we need 1 Activity and 3 Fragments.

    The Activity will host a TabLayout and a ViewPager like so:

    
    
        
    
        
    
    
    

    Since we only need to do the sliding behavior for the 1st Fragment the first Fragment gets an XML layout like so:

    
    
        
    
            
    
                
    
            
    
        
    
        
    
    
    
    

    You can make the other Fragments however you like I just created fake data and a simple RecyclerView in the other Fragments.

    Then call these views in your Activity and Fragment like so:

    Activity

    public class MainActivity extends AppCompatActivity {
    
        private TabLayout mTabLayout;
        private ViewPager mViewPager;
        private SampleViewPagerAdapter mViewPagerAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.another_activity);
    
            mTabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
            mViewPager = (ViewPager) findViewById(R.id.viewpager);
            mViewPagerAdapter = new SampleViewPagerAdapter(getSupportFragmentManager());
            mViewPager.setAdapter(mViewPagerAdapter);
            mTabLayout.setupWithViewPager(mViewPager);
        }
    
    }
    

    ViewPager Adapter

    public class SampleViewPagerAdapter extends FragmentPagerAdapter {
    
        public SampleViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new MapFragment();
                case 1:
                    return new ScrollFragment();
                case 2:
                    return new ScrollFragment();
                default:
                    return new ScrollFragment();
            }
        }
    
        @Override
        public int getCount() {
            return 3;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            String[] tabNames = {"Stops", "Planner", "Alerts"};
            return tabNames[position];
        }
    
    }
    

    Map Fragment with Sliding RecyclerView

    public class MapFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View root = inflater.inflate(R.layout.activity_main, null);
    
            initCollapsingToolbar(root);
            // Initialize map
            initFragment();
            return root;
        }
    
        private void initCollapsingToolbar(View root) {
            CollapsingToolbarLayout collapsingToolbarLayout =
                (CollapsingToolbarLayout) root.findViewById(R.id.collapsingToolbar);
            collapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.cyan_500));
        }
    
        private void initFragment() {
            FakeDataFragment fragment = new FakeDataFragment();
            getChildFragmentManager().beginTransaction()
                    .replace(R.id.content, scrollFragment)
                    .commit();
        }
    
    }
    

    You should get something like this then:

    Setting the position:

    You can programmatically collapse the toolbar (CollapsingToolbarLayout) using the following code:

    public void collapseToolbar(){
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFrameLayout.getLayoutParams();
        AppBarLayout.ScrollingViewBehavior behavior = (AppBarLayout.ScrollingViewBehavior) params.getBehavior();
        if (behavior != null) {
            behavior.onNestedFling(rootLayout, appbarLayout, null, 0, 10000, true);
        }
    }
    

    This means when the User first sees the map the map is partially collapsed to your Default State.

提交回复
热议问题