1.实现的效果
- 1.初始化 adapter 创建layoutManager
mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(this);
mRecyclerView.setLayoutManager(virtualLayoutManager);
RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
mRecyclerView.setRecycledViewPool(viewPool);
viewPool.setMaxRecycledViews(0, 10);
- 创建轮播 适配器
BaseDelegeteAdapter bannerAdapter = new BaseDelegeteAdapter(this
, new LinearLayoutHelper(), R.layout.vlayout_banner, 1){
@Override
public void onBindViewHolder(BaseViewHolder holder, int i) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("http://dn.dengpaoedu.com/examples/glide/1.jpg");
arrayList.add("http://dn.dengpaoedu.com/examples/glide/2.jpg");
arrayList.add("http://dn.dengpaoedu.com/examples/glide/3.jpg");
arrayList.add("http://dn.dengpaoedu.com/examples/glide/4.jpg");
arrayList.add("http://dn.dengpaoedu.com/examples/glide/5.jpg");
arrayList.add("http://dn.dengpaoedu.com/examples/glide/6.jpg");
// 绑定数据
Banner mBanner = holder.getView(R.id.banner);
//设置banner样式
mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
//设置图片加载器
mBanner.setImageLoader(new GlideImageLoader());
//设置图片集合
mBanner.setImages(arrayList);
//设置banner动画效果
mBanner.setBannerAnimation(Transformer.DepthPage);
//设置标题集合(当banner样式有显示title时)
// mBanner.setBannerTitles(titles);
//设置自动轮播,默认为true
mBanner.isAutoPlay(true);
//设置轮播时间
mBanner.setDelayTime(3000);
//设置指示器位置(当banner模式中有指示器时)
mBanner.setIndicatorGravity(BannerConfig.CENTER);
//banner设置方法全部调用完毕时最后调用
mBanner.start();
mBanner.setOnBannerListener(new OnBannerListener() {
@Override
public void OnBannerClick(int position) {
Toast.makeText(getApplicationContext(), "banner点击了" + position, Toast.LENGTH_SHORT).show();
}
});
super.onBindViewHolder(holder, i);
}
};
- 3.创建 分类我的频道 自定义GridLayoutHelper
GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(5);
gridLayoutHelper.setPadding(0, 16, 0, 0);
gridLayoutHelper.setVGap(10);
gridLayoutHelper.setHGap(0);//// 控制子元素之间的水平间距
BaseDelegeteAdapter menuAdapter = new BaseDelegeteAdapter(this, gridLayoutHelper, R.layout.vlayout_menu, 10){
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, final int position) {
holder.setText(R.id.tv_menu_title_home, ITEM_NAMES[position] + "");
holder.setImageResource(R.id.iv_menu_home, IMG_URLS[position]);
holder.getView(R.id.ll_menu_home).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), ITEM_NAMES[position], Toast.LENGTH_SHORT).show();
}
});
}
};
- 创建横向跑马灯
BaseDelegeteAdapter newsAdapter = new BaseDelegeteAdapter(this, new LinearLayoutHelper(),
R.layout.vlayout_news, 1){
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int i) {
MarqueeView marqueeView1 = holder.getView(R.id.marqueeView1);
MarqueeView marqueeView2 = holder.getView(R.id.marqueeView2);
List<String> info1 = new ArrayList<>();
info1.add("天猫超市最近发大活动啦,快来抢");
info1.add("没有最便宜,只有更便宜!");
List<String> info2 = new ArrayList<>();
info2.add("这个是用来搞笑的,不要在意这写小细节!");
info2.add("啦啦啦啦,我就是来搞笑的!");
marqueeView1.startWithList(info1);
marqueeView2.startWithList(info2);
// 在代码里设置自己的动画
marqueeView1.startWithList(info1, R.anim.anim_bottom_in, R.anim.anim_top_out);
marqueeView2.startWithList(info2, R.anim.anim_bottom_in, R.anim.anim_top_out);
marqueeView1.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
@Override
public void onItemClick(int position, TextView textView) {
Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
marqueeView2.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
@Override
public void onItemClick(int position, TextView textView) {
Toast.makeText(getApplicationContext(), textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
};
- 分类列表:
for (int i = 0; i < ITEM_URL.length; i++) {
final int finalI = i;
BaseDelegeteAdapter titleAdapter = new BaseDelegeteAdapter(this, new LinearLayoutHelper(), R.layout.vlayout_title, 1) {
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
holder.setImageResource(R.id.iv, ITEM_URL[finalI]);
}
};
GridLayoutHelper gridHelper = new GridLayoutHelper(2);
BaseDelegeteAdapter gridAdapter = new BaseDelegeteAdapter(this, gridHelper, R.layout.vlayout_grid, 4){
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, final int position) {
int item = GRID_URL[position];
ImageView iv = holder.getView(R.id.iv);
Glide.with(getApplicationContext()).load(item).into(iv);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "item" + position, Toast.LENGTH_SHORT).show();
}
});
}
};
- 6.添加分类适配器:
delegateAdapter.addAdapter(bannerAdapter);
delegateAdapter.addAdapter(menuAdapter);
delegateAdapter.addAdapter(newsAdapter);
mRecyclerView.setAdapter(delegateAdapter);