近日使用慕课网APP的时候,发现一个页面效果感觉很有意思(正常情况下页面可以滑动,但是当页面中播放视频的时候,只有
下面可以滑动),所以仿照这做了一个,效果图如下:
主要是通过改变AppBarLayout的Flag来实现的,可以参考:https://www.jianshu.com/p/7caa5f4f49bd
本Demo上传至:http://download.csdn.net/download/eueheuen/10212787
需要加入Design包:
implementation 'com.android.support:design:26.1.0'
主要代码:
具体代码实现xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="@color/colorMain"
android:layout_width="match_parent"
android:layout_height="30dp" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="250dp">
<RelativeLayout
android:id="@+id/rl_show"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这里可以设置你自己的布局" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="4dp"
app:tabSelectedTextColor="#000"
app:tabTextColor="#fff" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
MainActivity:
public class MainActivity extends BaseActivity implements View.OnClickListener {
private RelativeLayout rlShow;
private AppBarLayout.LayoutParams mParams;
private TabLayout mTableLayout;
private ViewPager mViewPager;
private ArrayList<Fragment> data;
@Override
public int setLayout() {
return R.layout.activity_main;
}
@Override
public void initView() {
data = new ArrayList<>();
rlShow = (RelativeLayout) findViewById(R.id.rl_show);
mTableLayout = findViewById(R.id.tabs);
mViewPager = findViewById(R.id.viewpager);
rlShow.setOnClickListener(this);
data.add(new FragmentOne());
data.add(new FragmentTwo());
data.add(new FragmentThree());
}
@Override
public void initData() {
//初次设置的滑动方式(Flag)
mParams = (AppBarLayout.LayoutParams) rlShow.getLayoutParams();
mParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
| AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
MyFragmentAdapter myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), data);
mViewPager.setAdapter(myFragmentAdapter);
mTableLayout.setupWithViewPager(mViewPager);
}
@Override
public void onClick(View v) {
//点击图片后改变Flag
Toast.makeText(this,"锁定",Toast.LENGTH_SHORT).show();
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) rlShow.getLayoutParams();
params.setScrollFlags(0);
rlShow.setLayoutParams(params);
}
}