折叠/展开视图配合滑动RecyclerView [英] Collapsing/Expanding view coordinated with Sliding RecyclerView

查看:1696
本文介绍了折叠/展开视图配合滑动RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下影响(参见下图):

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

  • 应用程序打开一个视图(地图)部分可见和 RecyclerView 在默认的定位点(中间图片)
  • 用户滚动 RecyclerView 起来,在地图坍塌并在不断滚动(右图)
  • 用户滚动 RecyclerView 下来,地图扩展到最高点(注意清单不应滑完全关闭屏幕,但一些锚点)(左图)
  • the app opens with a view (map) partially visible and the RecyclerView at a default anchor point (center image)
  • user scrolls the RecyclerView up, the map collapses and the list continues scrolling (right image)
  • user scrolls the RecyclerView down, the map expands to a maximum point (note the list should not slide completely off screen but to some anchored point) (left image)

输入图像的描述在这里

推荐答案

要创建此,我们需要1 活动 3 片段秒。

To create this we need 1 Activity and 3 Fragments.

活动将举办 TabLayout ViewPager 像这样:

<LinearLayout 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:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

由于我们只需要做的滑动行为第1 片段第一个片段变得像一个XML布局因此:

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

<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/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:orientation="vertical"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <com.google.android.gms.maps.MapView
                android:id="@+id/mapView"
                app:layout_collapseMode="parallax"
                android:layout_height="400dp"
                android:layout_width="match_parent" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


</android.support.design.widget.CoordinatorLayout>

您可以让其他片段取值但是你喜欢我刚刚创建假的数据和一个简单的 RecyclerView 的其他片段秒。

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:

活动

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适配器

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

}

地图碎片与滑动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();
    }

}

您应该得到这样的话:

您可以通过编程方式折叠工具栏( Col​​lapsingToolbarLayout )使用下列code:

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.

这篇关于折叠/展开视图配合滑动RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆