效果图:
PagerSlidingTabStrip, 这里根据最新的项目需求实现根据listview的上下滑动,收起和显示出标题指示器
布局:
1. <?xml version="1.0" encoding="utf-8"?>
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. "http://schemas.android.com/apk/res-auto"
4. "match_parent"
5. "match_parent">
6.
7. <RelativeLayout
8. "match_parent"
9. "match_parent"
10. "vertical">
11.
12. <android.support.v4.view.ViewPager
13. "@+id/fragment_recommend_list_viewpager"
14. "match_parent"
15. "match_parent" />
16.
17. <com.astuetz.PagerSlidingTabStrip
18. "@+id/fragment_recommend_list_tabs"
19. "match_parent"
20. "40dp"
21. "#FFFFFF"
22. "never"
23. "@color/gray_979799_80"
24. "16dp"
25. "@android:color/transparent"
26. "@color/red2"
27. "0dp"
28. "@color/red2"
29. "@android:color/transparent"
30. "16dp"
31. "@android:color/transparent"
32. "0.5dp" />
33. </RelativeLayout>
34. </RelativeLayout>
代码:
1. package com.miu.apps.miss.fragment;
2.
3. import android.animation.ObjectAnimator;
4. import android.content.Context;
5. import android.os.Bundle;
6. import android.support.annotation.Nullable;
7. import android.support.v4.app.Fragment;
8. import android.support.v4.view.PagerAdapter;
9. import android.support.v4.view.ViewPager;
10. import android.util.Log;
11. import android.view.LayoutInflater;
12. import android.view.MotionEvent;
13. import android.view.View;
14. import android.view.ViewGroup;
15. import android.widget.ScrollView;
16.
17. import com.astuetz.PagerSlidingTabStrip;
18. import com.miu.apps.miss.R;
19. import com.miu.apps.miss.views.RecommendView;
20. import com.yt.utils.DensityUtil;
21.
22. /**
23. * yt make
24. * Created by Administrator on 2016/12/03.
25. * 图文推荐
26. */
27. public class FragmentRecommend extends Fragment {
28.
29. private Context mContext;
30. private ViewPager mViewPager;
31. public PagerSlidingTabStrip tabs;
32.
33.
34. @Nullable
35. @Override
36. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
37.
38. null);
39.
40. mContext = getContext();
41.
42. initData();
43.
44. initView(view);
45.
46. return view;
47.
48. }
49.
50.
51. private void initView(View view) {
52. mViewPager = (ViewPager) view.findViewById(R.id.fragment_recommend_list_viewpager);
53. 3);//设置缓存view 的个数(实际有3个,缓存2个+正在显示的1个)
54. new RecommendLListViewPagerAdapter();
55. mViewPager.setAdapter(adapter);
56.
57. // Bind the tabs to the ViewPager
58. tabs = (PagerSlidingTabStrip) view.findViewById(R.id.fragment_recommend_list_tabs);
59. tabs.setViewPager(mViewPager);
60.
61. }
62.
63. //精选,时尚,美妆,生活,剧情,音乐,创意
64.
65. private String[] CLASSIFY = new String[]{CLASSIFY_CHOICENESS, CLASSIFY_FASHION, CLASSIFY_BEAUTY, CLASSIFY_LIVE, CLASSIFY_MUSIC, CLASSIFY_AUTHENTICATE, CLASSIFY_UNIQUE};
66. private static String CLASSIFY_CHOICENESS = " 每日精选 ";
67. private static String CLASSIFY_FASHION = " 风向 ";
68. private static String CLASSIFY_BEAUTY = " 美妆 ";
69. private static String CLASSIFY_LIVE = " 态度 ";
70. private static String CLASSIFY_MUSIC = " 榜样 ";
71. private static String CLASSIFY_AUTHENTICATE = " 鉴定 ";
72. private static String CLASSIFY_UNIQUE = " 别致 ";
73.
74. private void initData() {
75.
76. }
77.
78. /**
79. * Viewpager adapter
80. */
81. class RecommendLListViewPagerAdapter extends PagerAdapter {
82.
83. @Override
84. public CharSequence getPageTitle(int position) {
85. return CLASSIFY[position];
86. }
87.
88. @Override
89. public int getCount() {
90. return CLASSIFY.length;
91. }
92.
93. @Override
94. public boolean isViewFromObject(View view, Object object) {
95. return view == object;
96. }
97.
98. @Override
99. public void destroyItem(ViewGroup container, int position, Object object) {
100. container.removeView((View) object);
101. }
102.
103. @Override
104. public Object instantiateItem(ViewGroup container, int position) {
105. final RecommendView recommendView = new RecommendView(mContext, CLASSIFY[position]);
106.
107. new View.OnTouchListener() {
108. @Override
109. public boolean onTouch(View v, MotionEvent event) {
110.
111. switch (event.getAction()) {
112. case MotionEvent.ACTION_DOWN:// 按下
113. // 获取按下的时候的XY轴的位置
114. mDownX = event.getX();
115. mDownY = event.getY();
116. break;
117. case MotionEvent.ACTION_MOVE:// 移动
118. float moveY = event.getY();
119. float diffY = mDownY - moveY;// 计算Y轴滚动偏移量
120.
121. float moveX = event.getX();
122. float diffX = mDownX - moveX;// 计算X轴滚动偏移量
123.
124. if (diffX > diffY) {
125. if (isYetGliet) {
126. true);
127. }
128. else if (diffY > diffX) {
129. if (diffY > 2) {
130. if (isYetUpgliet) {
131. false);
132. }
133. else if (diffY < -2) {
134. if (isYetGliet) {
135. true);
136. }
137. }
138. }
139. // 更新DownXY的位置:
140. mDownY = moveY;
141. mDownX = moveX;
142. break;
143. case MotionEvent.ACTION_UP:// 抬起
144. break;
145. }
146. return false;
147. }
148. });
149. container.addView(recommendView);
150. return recommendView;
151. }
152. }
153.
154. /**
155. * viewpager TAB 是否下滑还是上滑
156. *
157. * @param isGlide true 下滑 flase 上滑
158. */
159. private void tabGlide(boolean isGlide) {
160. if (isGlide) {
161. "translationY", -DensityUtil.dip2px(mContext, 40), 0);
162. 300);
163. animator.start();
164. false;
165. true;
166. else {
167. "translationY", 0, -DensityUtil.dip2px(mContext, 40));
168. 300);
169. animator.start();
170. false;
171. true;
172. }
173. }
174. private boolean isYetUpgliet = true;
175. private boolean isYetGliet = true;
176. private float mDownX;
177. private float mDownY;
178. }