先上效果图
源码已上传到码云上 https://git.oschina.net/osczaizai/AndroidDemo
前面学习了 coordinatorLayout,
连接:http://cherylgood.cn/c/Android 开发之 CoordinatorLayout 使用详解一.php
http://cherylgood.cn/c/Android 开发之 CoordinatorLayout 使用详解二.php
现在我们来个小 demo 其实代码都很简单,只要你看了前面的两个使用介绍,基本久 OK 了
1、主布局文件
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="guanaj.com.coordinatorlayoutdemo.recyclerdemo.RecyclerViewActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<LinearLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#cccccc"
app:layout_scrollFlags="scroll|enterAlways">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="top content"
android:textColor="#000" />
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#000000" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
重点看这两句:
app:layout_scrollFlags="scroll|enterAlways"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
其实
layout_behavior 只要设置在有子 view 是可以滚动的就可以了,
主 activity 代码:
package guanaj.com.coordinatorlayoutdemo.recyclerdemo;
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import guanaj.com.coordinatorlayoutdemo.R;
public class RecyclerViewActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private List<Fragment> fragmentList = new ArrayList<>();
private MyFragmentAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
initViews();
initEvent();
initData();
}
private void initViews() {
tabLayout = (TabLayout) findViewById(R.id.tablayout);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout.setupWithViewPager(viewPager);
adapter = new MyFragmentAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
private void initEvent() {
}
private void initData() {
for (int i = 0;i<5;i++){
Fragment fragment = new ContentFragment();
fragmentList.add(fragment);
}
adapter.notifyDataSetChanged();
}
class MyFragmentAdapter extends FragmentPagerAdapter {
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return "标题:"+position;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList == null ? 0 : fragmentList.size();
}
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于