效果图



说明
这是在我之前写的项目https://github.com/Peakmain/gankzhihu基础上进行修改
比较
没有使用封装前的代码


封装后的代码

之前的知乎页面,我们需要判断是否是0的位置然后创建新的viewholder

现在只需要添加一下的代码即可
mRecyclerView.addHeaderView(mTopBannerView);
自己封装的banner的使用方式
private void addTopView() {
mTopBannerView = LayoutInflater.from(getContext())
.inflate(R.layout.item_zhihu_top_stories, mRecyclerView, false);
List<TopStories> topList = mNewsTimeLine.getTop_stories();
mBannerImage = new ArrayList<>();
mBannerTitle = new ArrayList<>();
for (TopStories topStories : topList) {
mBannerImage.add(topStories.getImage());
mBannerTitle.add(topStories.getTitle());
}
BannerView mBanner = mTopBannerView.findViewById(R.id.vp_top_stories);
mBanner.setAdapter(new BannerAdapter() {
@Override
public View getView(int position, View convertView) {
if (convertView == null) {
convertView = new ImageView(getContext());
}
((ImageView) convertView).setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(getContext()).load(topList.get(position).getImage()).into((ImageView) convertView);
return convertView;
}
@Override
public int getCount() {
return topList.size();
}
@Override
public String getBannerDesc(int position) {
return topList.get(position).getTitle();
}
});
//BannerUtils.initBanner(mBanner, mBannerImage, mBannerTitle);
//下标从0开始
mBanner.setOnBannerItemClickListener(new BannerViewPager.BannerItemClickListener() {
@Override
public void click(int position) {
ARouter.getInstance().build("/activity/ZhihuWebActivity")
.withString(ZhihuWebActivity.ID, topList.get(position).getId())
.navigation();
}
});
mRecyclerView.addHeaderView(mTopBannerView);
}
TODO
下拉刷新的时候在新闻页面中刷新的时候,有时候view会在下拉刷新完成的时候不消失,尚未想到解决办法,若有大神知道,还请大神指导,感激不尽。
之所以没有用框架,是想了解和熟悉下recylerview
网友评论