-
Notifications
You must be signed in to change notification settings - Fork 20
/
HomeFragment.java
executable file
·170 lines (130 loc) · 4.8 KB
/
HomeFragment.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.daxueoo.shopnc.ui.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.daxueoo.shopnc.R;
import java.util.ArrayList;
import java.util.List;
/**
* 这是首页的Fragment,主要有滚动图片,ListView,Button等
*/
public class HomeFragment extends BaseFragment implements View.OnClickListener {
private String TAG = "HomeFragment";
// Header标题
private TextView tv_title;
private ViewPager viewPager;
private List<Fragment> mFragmentList = new ArrayList<Fragment>();
/**
* Fragment
*/
private HomeIndexFragment homeIndexFragment = new HomeIndexFragment();
private HomeTrendsFragment homeTrendsFragment = new HomeTrendsFragment();
/**
* ViewPager的当前选中页
*/
private int currentIndex;
private boolean isFirst = true;
private ImageView iv_center;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = View.inflate(activity, R.layout.fragment_home, null);
tv_title = (TextView) view.findViewById(R.id.titlebar_tv);
iv_center = (ImageView) view.findViewById(R.id.titlebar_iv_center);
viewPager = (ViewPager) view.findViewById(R.id.id_page_vp);
Log.e(TAG, "onCreateView");
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv_title.setVisibility(View.GONE);
iv_center.setImageResource(R.mipmap.top_home_02);
iv_center.setVisibility(View.VISIBLE);
init();
initImageView();
// if (isFirst) {
// init();
// isFirst = false;
// }
}
private void initImageView() {
iv_center.setOnClickListener(this);
}
private void init() {
Log.e(TAG, "init");
FragmentManager fm = getChildFragmentManager();
viewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
@Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public Fragment getItem(int pos) {
// TODO Auto-generated method stub
Fragment fragment;
switch (pos) {
case 0:
fragment = new HomeIndexFragment();
return fragment;
case 1:
fragment = new HomeTrendsFragment();
return fragment;
default:
fragment = new HomeIndexFragment();
return fragment;
}
}
});
viewPager.setCurrentItem(0);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
/**
* state滑动中的状态 有三种状态(0,1,2) 1:正在滑动 2:滑动完毕 0:什么都没做。
*/
@Override
public void onPageScrollStateChanged(int state) {
}
/**
* position :当前页面,及你点击滑动的页面 offset:当前页面偏移的百分比
* offsetPixels:当前页面偏移的像素位置
*/
@Override
public void onPageScrolled(int position, float offset, int offsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
iv_center.setImageResource(R.mipmap.top_home_02);
break;
case 1:
iv_center.setImageResource(R.mipmap.top_home_01);
break;
case 2:
break;
}
currentIndex = position;
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.titlebar_iv_center:
if (viewPager.getCurrentItem() == 0){
viewPager.setCurrentItem(1);
}else {
viewPager.setCurrentItem(0);
}
break;
}
}
}