【问题标题】:Using a coordinatorlayout to collapse a full screen imageview and replace it with viewpager with headers使用 coordinatorlayout 折叠全屏图像视图并将其替换为带有标题的 viewpager
【发布时间】:2016-08-08 03:02:18
【问题描述】:

我开始玩 coordinatoralyout,我正在尝试实现附加图像上的行为,我希望背景图像为全屏尺寸,并且在滚动时我希望一些文本视图消失,一些作为部分粘贴viewpager(而不是工具栏布局)关于如何实现这一点的任何指导?

【问题讨论】:

  • 第一个状态的标签页可以看到吗?或者你绝对想隐藏它们?
  • 您是否看过新的设计支持小部件 BottomSheet ...我认为您可以使用 BottomSheet ....

标签: android android-viewpager android-animation android-coordinatorlayout


【解决方案1】:

您可以使用 layout_behavior 来处理您希望在滚动时消失的字符串。使用 CoordinatorLayout.Behavior

自定义您的视图行为

ViewBehavior.java

public class ViewBehavior extends CoordinatorLayout.Behavior<RelativeLayout> {

private Context mContext;

public ViewBehavior(Context context, AttributeSet attrs) {
    mContext = context;
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, RelativeLayout child, View dependency) {
    return dependency instanceof AppBarLayout;
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, RelativeLayout child, View dependency) {
    child.measure(View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.AT_MOST));
    int maxScroll = ((AppBarLayout) dependency).getTotalScrollRange();
    float percentage = Math.abs(dependency.getY()) / (float) maxScroll;
    float childPosition = dependency.getHeight()
            + dependency.getY()
            - child.getMeasuredHeight()
            - (getToolbarHeight() - child.getMeasuredHeight()) * percentage / 2;

    CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
    child.setLayoutParams(lp);

    child.setY(childPosition);
    return true;
}

public int getToolbarHeight() {
    int result = 0;
    TypedValue tv = new TypedValue();
    if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        result = TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics());
    }
    return result;
}
}

在布局 xml 中,将您的自定义视图行为设置为您要处理的视图中的 app:layout_behavior

activity_main.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

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

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:id="@+id/llViewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:textColor="@color/red"
            app:pstsShouldExpand="true"
            app:pstsTextAllCaps="true" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.stacktest.ViewBehavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="20dp"
        android:text="Text-1" />

    <TextView
        android:id="@+id/txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="36dp"
        android:layout_marginTop="20dp"
        android:text="Text-2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/txt2"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="36dp"
        android:paddingRight="20dp"
        android:text="Text-3" />

    <TextView
        android:id="@+id/txt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/txt2"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="36dp"
        android:text="Text-4" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

最后,使用布局并在 Activity 类中创建 ViewPager 和 Tabs。

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ((CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar)).setTitle(" ");

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabsStrip.setViewPager(viewPager);
}

public class MyPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 2;
    private String tabTitles[] = new String[] { "Tab1", "Tab2" };

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        return TestFragment.newInstance(position + 1);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles[position];
    }
}
}

build.gradle 中添加以下额外依赖项以及 appcompat 和支持库。

  • com.android.support:design:23.2.1
  • com.astuetz:pagerslidingtabstrip:1.0.1(用于 ViewPager 选项卡)

【讨论】:

  • 几乎正是我所需要的!我在发布此文章后阅读过的一些教程中看到了行为类,但我无法真正将其应用于图像。在我的问题中,绿色应该是图像(即 scale=fitXY),我想将其缩小以“充当”条形布局 - 你知道我该如何实现吗?
  • 使用 scaleType=fitXY 将图像添加到 ImageView(android:src)。从 values-21/styles.xml 中删除 @android:color/transparent
【解决方案2】:

由于我不确定您是否需要特定或一般的解决方案,因此我将为您提供针对您的特定问题的解决方案。关键是使用scrollFlagscollapseMode。如果你真的想在 appBar 展开时隐藏标签,你可以玩一下可见性。

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <RelativeLayout
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="375dp"
                android:src="@drawable/ic_launcher"/>

            <LinearLayout
                android:layout_marginBottom="30dp"
                android:layout_below="@+id/image"
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_marginLeft="30dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:text="textView1"/>

                <TextView
                    android:layout_marginLeft="140dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                    android:text="textView2"/>

            </LinearLayout>

        </RelativeLayout>


        <android.support.v7.widget.Toolbar
            android:id="@+id/mToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"/>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="textView3"/>

        <TextView
            android:layout_marginLeft="140dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="textView4"/>

    </LinearLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        android:background="?attr/colorPrimary"
        app:tabMode="scrollable"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/tab_viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

【讨论】:

  • 谢谢,但是我需要图像视图在其高度上有一个 match_parent,这样它将成为 CollapsingToolbarLayout 的背景。本质上,我需要 imageview 有一个触摸事件,它就像一个 collapingtoolbarlayout 的底部(负责缩小上部 barlayout)
  • 但是对于 CollapsingToolbarLayout 你需要设置一个最大高度。如果需要,您可以在 imageview 上设置 match_parent,但是您必须为 imageView 的包含布局设置高度。对于触摸事件,只需将 clickListener 添加到您的图像并以编程方式折叠工具栏,就像 appBarLayout.setExpanded(true/false)
  • 问题是我想得到类似于这里的东西 - saulmm.github.io/mastering-coordinator 只有背景图像视图是全屏的,并且仍然让它可以滑动。放置一个 onclicklistener 并不能解决我的问题,因为它不能回答我如何实现这一点的问题......
猜你喜欢
  • 2016-04-30
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 2015-05-29
相关资源
最近更新 更多