recyclerView 实现复杂的item布局(如淘宝、京东、商城类首页) (2)

ViewGroup.LayoutParams bannerParams = mBanner.getLayoutParams();
int resourceId = getContext().getResources().getIdentifier("status_bar_height",
"dimen","android");
int statusBarHeight = getContext().getResources().getDimensionPixelSize(resourceId);
height = bannerParams.height - statusBarHeight - 104;
return convertView;
}

//模拟后台请求数据 存放在AssertsFile文件夹里
public static byte[] getAssertsFile(Context context, String fileName) {
InputStream inputStream = null;
//读取资源
AssetManager assetManager = context.getAssets();
try {
inputStream = assetManager.open(fileName);
if (inputStream == null) {
return null;
}

BufferedInputStream bis = null;
int length;
try {
bis = new BufferedInputStream(inputStream);
length = bis.available();
byte[] data = new byte[length];
bis.read(data);

return data;
} catch (IOException e) {

} finally {
if (bis != null) {
try {
bis.close();
} catch (Exception e) {

}
}
}

return null;
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

}
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
基类Fragment

public abstract class BaseFragment extends Fragment {

View mView;
boolean isLoad = false;
boolean isInit = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mView == null)
mView = inflater.inflate(getLayoutId(), container, false);
isInit = true;
initView(mView);
isCanLoadData();
return mView;
}

public abstract void initView(View mView);

/**
* 视图是否已经对用户可见,系统的方法
*/
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isCanLoadData();
}

/**
* 懒加载
*/
private void isCanLoadData() {
if (!isInit) {
return;
}
if (getUserVisibleHint() && !isLoad) {
lazyLoad();
isLoad = true;
} else {
if (isLoad) {
stopLoad();
}
}
}

@Override
public void onDestroyView() {
super.onDestroyView();
isInit = false;
}


/**
* 第一次加载的处理 此处可以留给加载网络去处理
*/
public void lazyLoad() {

}

/**
* 页面停止加载的处理
*/
public void stopLoad() {

}

public abstract int getLayoutId();

}
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
核心adapter

public class TestHomeAdapter extends MultipleItemRvAdapter<TestBean, BaseViewHolder> {
//通栏商品大图片
public static final int TYPE_IMG = 100;
//通栏图文
public static final int TYPE_TEXT_AND_IMG = 200;
//通栏文本
public static final int TYPE_TEXT = 300;
//三列
public static final int TYPE_THREE_COLUMN = 400;
//通栏水平拖动
public static final int TYPE_HORIZONTAL_SCROLL = 500;
//通栏窄图片
public static final int TYPE_NARROW_IMG = 600;
//两列
public static final int TYPE_TWO_COLUMN = 700;
public TestHomeAdapter() {
super(null);
finishInitialize();
}

@Override
protected int getViewType(TestBean testBean) {
int type = testBean.getType();
if (type == 1) {
return TYPE_IMG;
} else if (type == 2) {
return TYPE_TEXT_AND_IMG;
} else if (type == 3) {
return TYPE_TEXT;
} else if (type == 4) {
return TYPE_THREE_COLUMN;
} else if (type == 5) {
return TYPE_HORIZONTAL_SCROLL;
} else if (type == 6) {
return TYPE_NARROW_IMG;
} else if (type == 7) {
return TYPE_TWO_COLUMN;
}
return 0;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwsgyg.html