/**
* ght (c) 2007-2017 xxx Inc. All rights reserved.
*
* @author lzz
* Created 2017/5/4 10:07
* @license http://www.xxx.net
* @link http://www.xxx.net
* @description 推荐商品的ViewPager适配器;带有水平分页功能的RecyclerView
*
* **********************
* ## ## ## *
* *
* ## ## ## *
* **********************
*/
public class GoodsPageAdapter extends PagerAdapter {
private List<View> indicators =
new ArrayList<>();
private ArrayList<GoodsVo> datas =
new ArrayList<>();
private Context context;
private LinearLayout llIndicators;
public void setDatas(ArrayList<GoodsVo> datas) {
this.datas = datas;
}
public List<View>
getIndicators() {
return indicators;
}
public GoodsPageAdapter(Context context, LinearLayout llIndicators) {
this.context = context;
this.llIndicators = llIndicators;
}
/**
* 计算水平分页的数量
*
* @return
*/
@Override
public int getCount() {
int count = datas.size() %
6;
int divide = datas.size() /
6;
return count ==
0 ? divide : divide +
1;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public RecyclerView
instantiateItem(ViewGroup container,
int position) {
RecyclerView recyclerView =
new RecyclerView(context);
GridLayoutManager gridLayoutManager =
new GridLayoutManager(context,
2, LinearLayoutManager.HORIZONTAL,
false);
recyclerView.setLayoutManager(gridLayoutManager);
GoodsDetailsHotGridAdapter hotGridAdapter =
new GoodsDetailsHotGridAdapter(context);
recyclerView.setAdapter(hotGridAdapter);
List<GoodsVo> list =
new ArrayList<GoodsVo>();
for (
int i = position *
6; i < (position +
1) *
6 && i < datas.size(); i++) {
list.add(datas.get(i));
}
for (
int i =
0; position ==
0 && getCount() !=
1 && i < getCount(); i++) {
View indicator =
new View(context);
Drawable drawable = context.getResources().getDrawable(R.drawable.indicator_selector);
indicator.setBackground(drawable);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
24,
24);
params.setMarginEnd(
10);
llIndicators.addView(indicator, params);
indicators.add(indicator);
}
hotGridAdapter.setDatas(list);
hotGridAdapter.notifyDataSetChanged();
container.addView(recyclerView);
return recyclerView;
}
@Override
public void destroyItem(ViewGroup container,
int position, Object object) {
container.removeViewAt(position);
}
}