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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

您可以使用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选项卡)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值