先看一下效果吧
大家如果喜欢就一起来实现一下吧,顺便点一下关注呦,万一以后用的上呢!
那么,我们就开始吧。
首先当然还是依赖了
Vlayout依赖
implementation 'com.alibaba.android:vlayout:1.0.3'
//marqueeview 跑马灯依赖
implementation 'com.sunfusheng:marqueeview:1.3.1'
布局
也就一个Recycleryout布局
那么,开始上主菜了
private void initView(View inflate) {
mRecy = (RecyclerView) inflate.findViewById (R.id.recy);
//创建Vlayout布局管理器
VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager (getActivity ());
//设置Vlayout布局管理器
mRecy.setLayoutManager (virtualLayoutManager);
final RecyclerView.RecycledViewPool pool = new RecyclerView.RecycledViewPool ();
mRecy.setRecycledViewPool (pool);
pool.setMaxRecycledViews (0, 10);
//添加banner头布局
BaseDelegeteAdapter adapter = new BaseDelegeteAdapter (getActivity (), new LinearLayoutHelper (),
R.layout.vlayout_banner, 1) {
@Override
public void onBindViewHolder(BaseViewHolder holder, int i) {
ArrayList bannerList = new ArrayList<> ();
bannerList.add (Uri.parse ("https://gw.alicdn.com/imgextra/i1/7/O1CN01nfNUEq1BvFJ6YLtPD_!!7-0" +
"-lubanu.jpg"));
bannerList.add (Uri.parse ("https://gw.alicdn.com/imgextra/i3/20/O1CN01UTTfyr1C1CSm4hzOv_!!20" +
"-0" +
"-lubanu.jpg"));
bannerList.add (Uri.parse ("https://gw.alicdn.com/imgextra/i3/106/O1CN01etvHvW1CeaWUFfTu9_" +
"!!106-0" +
"-lubanu.jpg"));
bannerList.add (Uri.parse ("https://gw.alicdn.com/imgextra/i3/20/O1CN01UTTfyr1C1CSm4hzOv_!!20" +
"-0" +
"-lubanu.jpg"));
bannerList.add (Uri.parse ("https://gw.alicdn.com/imgextra/i2/14/O1CN019L7VgZ1ByS5PJJpnO_!!14" +
"-0" +
"-lubanu.jpg"));
bannerList.add (Uri.parse ("https://img.alicdn.com/tps/i4/TB1YMbpeoKF3KVjSZFESutExFXa.jpg"));
//绑定数据
Banner mBanner = holder.getView (R.id.banner);
//设置banner样式
mBanner.setBannerStyle (BannerConfig.CIRCLE_INDICATOR);
//设置图片加载器
mBanner.setImageLoader (new ImageLoader ());
//设置图片集合
mBanner.setImages (bannerList);
//设置banner动画效果
mBanner.setBannerAnimation (Transformer.DepthPage);
//设置自动轮播
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 (getActivity (), "banner被点击了" + position, Toast.LENGTH_SHORT).show ();
}
});
super.onBindViewHolder (holder, i);
}
};
//添加Grid的图标
GridLayoutHelper gridLayoutHelper = new GridLayoutHelper (5);
gridLayoutHelper.setPadding (0, 16, 0, 0);
gridLayoutHelper.setVGap (10);
gridLayoutHelper.setHGap (0);//控制子元素之间的水平间距
BaseDelegeteAdapter menuAdapter = new BaseDelegeteAdapter (getActivity (), gridLayoutHelper, R.layout.vlayout_menu, 10) {
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, final int i) {
holder.setText (R.id.tv_menu_title, ITEM_NAMES[i] + " ");
holder.setImageResource (R.id.iv_menu, IMG_URLS[i]);
holder.getView (R.id.ll_menu_home).setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
Toast.makeText (getActivity ().getApplicationContext (), ITEM_NAMES[i], Toast.LENGTH_SHORT).show ();
}
});
super.onBindViewHolder (holder, i);
}
};
//vlayout_menu布局
//第三个页面(跑马灯)
BaseDelegeteAdapter newsAdapter = new BaseDelegeteAdapter (getActivity (), new LinearLayoutHelper (),
R.layout.vlayout_news, 1) {
@Override
public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
MarqueeView marqueeView1 = baseViewHolder.getView (R.id.marqueeview1);
MarqueeView marqueeView2 = baseViewHolder.getView (R.id.marqueeview2);
ArrayList info1 = new ArrayList<> ();
info1.add ("天猫超市最近发大活动啦,快来抢");
info1.add ("没有最便宜,只有更便宜");
ArrayList info2 = new ArrayList<> ();
info2.add ("降价大酬宾");
info2.add ("拉拉阿拉啦啦,吐血大甩卖了!");
super.onBindViewHolder (baseViewHolder, i);
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 (getActivity (), textView.getText ().toString (), Toast.LENGTH_SHORT).show ();
}
});
marqueeView2.setOnItemClickListener (new MarqueeView.OnItemClickListener () {
@Override
public void onItemClick(int position, TextView textView) {
Toast.makeText (getActivity (), textView.getText ().toString (), Toast.LENGTH_SHORT).show ();
}
});
}
};
//添加到DelegateAdapter的集合中
DelegateAdapter delegateAdapter = new DelegateAdapter (virtualLayoutManager, true);
delegateAdapter.addAdapter (adapter);
delegateAdapter.addAdapter (menuAdapter);
delegateAdapter.addAdapter (newsAdapter);
//vlayout_news
//第四个页面(高颜值好货)
for (int i = 0; i < ITEM_URL.length; i++) {
final int finalI = i;
BaseDelegeteAdapter titleAdapter = new BaseDelegeteAdapter (getActivity (), new LinearLayoutHelper (), R.layout.vlayout_title, 1) {
@Override
public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
baseViewHolder.setImageResource (R.id.iv, ITEM_URL[finalI]);
}
};
//第五个页面(下方图片)
GridLayoutHelper gridHelper = new GridLayoutHelper (2);
BaseDelegeteAdapter gridAdapter = new BaseDelegeteAdapter (getActivity (), 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 (getActivity ()).load (item).into (iv);
iv.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
Toast.makeText (getActivity (), "item:" + position, Toast.LENGTH_SHORT).show ();
}
});
}
};
//添加至delegateAdapter集合
delegateAdapter.addAdapter (titleAdapter);
delegateAdapter.addAdapter (gridAdapter);
}
//设置适配器显示
mRecy.setAdapter (delegateAdapter);
文章很短,路还漫长,大家好,我是玖玖君,一个帅气与才华并存的男人,我们下期再见