标题可以用 代替android,android – 使用coordinatorlayout折叠全屏图像视图,并将其替换为带有标题的viewpager...

本文介绍了如何使用自定义的ViewBehavior ViewBehavior.java来处理在AppBarLayout滚动时隐藏的字符串。通过设置layout_behavior,展示了如何在Android布局中应用此行为,并详细解释了关键方法如layoutDependsOn和onDependentViewChanged的用法。同时,提到了所需的依赖和在Activity中配置的相关组件。

您可以使用layout_behavior来处理要在滚动时消失的字符串.使用CoordinatorLayout.Behavior自定义您的视图行为

ViewBehavior.java

public class ViewBehavior extends CoordinatorLayout.Behavior {

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中

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: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: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">

android:id="@+id/image"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

app:layout_collapseMode="parallax" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fillViewport="true"

app:layout_behavior="@string/appbar_scrolling_view_behavior">

android:id="@+id/llViewPager"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/tabs"

android:layout_width="match_parent"

android:layout_height="48dp"

android:textColor="@color/red"

app:pstsShouldExpand="true"

app:pstsTextAllCaps="true" />

android:id="@+id/viewpager"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@android:color/white" />

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="com.stacktest.ViewBehavior">

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" />

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" />

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" />

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" />

最后,使用布局并在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选项卡)

<think> 我们正在讨论如何确定ViewPager2使用appbar_scrolling_view_behavior时所需的CoordinatorLayout版本(1.2.0+) 具体步骤包括: 1. 检查项目中的build.gradle文件 2. 查看依赖项中CoordinatorLayout的版本 3. 确认是否满足最低版本要求(1.2.0) 4. 如果不满足,如何升级 同时,我们需要在最后提出3-5个相关问题,以“§§相关问题§§”开头,列出问题。 </think> 要确定项目中是否使用了 `androidx.coordinatorlayout:coordinatorlayout:1.2.0+` 版本,可通过以下方法验证: ### 版本确认方法 1. **检查 build.gradle 文件** 在模块的 `build.gradle` 中查看依赖声明: ```gradle dependencies { // 确认 coordinatorlayout 版本号 implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0' // 或更高版本如 1.3.0 } ``` 2. **使用 Gradle 命令** 在终端执行: ```bash ./gradlew app:dependencies --configuration releaseRuntimeClasspath ``` 在输出中搜索: ```plaintext \--- androidx.coordinatorlayout:coordinatorlayout -> 1.2.0 ``` 3. **Android Studio 可视化检查** - 打开 `Project Structure` (File > Project Structure) - 选择 `Dependencies` 选项卡 - 搜索 `coordinatorlayout`,查看版本号 4. **代码中动态检测** ```java // 在Activity中检测运行时版本 Log.d("VersionCheck", "CoordinatorLayout版本: " + CoordinatorLayout.class.getPackage().getImplementationVersion()); ``` ### 版本要求说明 | 版本范围 | 兼容性状况 | 必须升级的场景 | |--------------|-------------------------------|------------------------------| | ≤1.1.0 | **不兼容** ViewPager2 联动 | 使用 ViewPager2 + Behavior 时 | | 1.2.0-1.2.1 | 基础兼容 | 需要补丁修复嵌套滚动问题 | | ≥1.3.0 | 完美支持所有功能 | 推荐新项目使用 | ### 升级操作指南 1. 修改 `build.gradle`: ```gradle dependencies { // 升级到最新稳定版(截至2024年1月最新为1.3.0) implementation 'androidx.coordinatorlayout:coordinatorlayout:1.3.0' } ``` 2. 执行同步: - 点击 Android Studio 的 `Sync Project with Gradle Files` 按钮 3. 冲突解决: - 若存在版本冲突,可添加强制约束: ```gradle configurations.all { resolutionStrategy.force 'androidx.coordinatorlayout:coordinatorlayout:1.3.0' } ``` ### 验证升级效果 ```xml <!-- 测试布局 --> <androidx.coordinatorlayout.widget.CoordinatorLayout> <com.google.android.material.appbar.AppBarLayout.../> <androidx.viewpager2.widget.ViewPager2 app:layout_behavior="@string/appbar_scrolling_view_behavior"/> </CoordinatorLayout> ``` 检查以下联动效果: 1. 上滑 ViewPager2 → AppBar 应平滑折叠 2. 下滑 ViewPager2 → AppBar 应自动展开 3. 快速滑动 → 无卡顿或位置错位
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值