先上效果图
源码已上传到码云上https://git.oschina.net/osczaizai/AndroidDemo
前面学习了coordinatorLayout,
连接:http://catbro.cn/c/Android开发之CoordinatorLayout使用详解一.php
http://catbro.cn/c/Android开发之CoordinatorLayout使用详解二.php
现在我们来个小demo 其实代码都很简单,只要你看了前面的两个使用介绍,基本久OK了
1、主布局文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<?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代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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();
}
}
}
|