RecyclerView实现多布局+head轮播图

撰写于 2018-09-28 修改于 2018-09-28 分类 Android控件 标签 RecyclerView / 多布局 / head轮播图 阅读数量:

本文看点:

  • RecyclerView 实现多布局
  • RecyclerView head轮播图
  • RecyclerView 自定义item点击事件接口
  • RecyclerView 实现上拉下拉刷新数据

Activity代码

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Created by Dell on 2017/5/10.
* @author 小红妹
* 科技模块类
*/
public class ScienceTechnology extends BaseFragment implements NewsJsonUtils.CallBackListener, SwipeRefreshLayout.OnRefreshListener{

private View view;
private RecyclerView mRecyclerView;
private ScienceTechnologyAdapter adapterB;
//下拉刷新控件
private SwipeRefreshLayout sWipe;
//记录最后一个下标
private int mFootItem;
//或取数据的工具类
private NewsJsonUtils newsUtils;
//保存了所有数据集合数组
private List<NewsBeanData>[] msgSt;
//从msgSt中获取科技信息
private List<NewsBeanData> mData;
//保存了所有图片播放数据集合
private List<String> imgItem;
//保存了所有标题数据集合
private List<String> titleItem;
//记录当前真正要显示item数据
private List<NewsBeanData> nowItem;
//记录当前存放的数据条数
private int nowNum = 9;
//记录每次刷新的数
private final int NUM = 5;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

view = inflater.inflate(R.layout.hp_fm_news_fragment_b, container, false);
initView();//调用初始化方法
loadData();//调用加载数据方法
drawRecyclerView();//调用RecyclerView点击事件方法
initFootListener();//调用上拉加载事件监听方法
return view;
}

/**
* 初始化方法
*/
private void initView() {
imgItem = new ArrayList<>();
titleItem = new ArrayList<>();
nowItem = new ArrayList<>();
//下拉刷新
sWipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe_layout);
sWipe.setColorSchemeResources(android.R.color.holo_blue_bright);
sWipe.setOnRefreshListener(this);

mRecyclerView = (RecyclerView) view.findViewById(R.id.one_b_recycler_view);
adapterB = new ScienceTechnologyAdapter(getActivity(), imgItem, titleItem, nowItem);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(adapterB);
}

/**
* RecyclerView点击事件方法
*/
private void drawRecyclerView() {
//RecyclerView点击事件
adapterB.setOnItemClickListener(new ScienceTechnologyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
String url = mData.get(position).getUrl();
Intent intent = new Intent();
intent.setClass(getActivity(), NewsDetailActivity.class);
intent.putExtra("url",url);
Log.e("URL", "onItemClick: "+ url);
startActivity(intent);
}
});
}

/**
* 上拉加载事件监听方法
*/
private void initFootListener() {
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//状态发生时触发
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE && mFootItem + 1 == adapterB.getItemCount()) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
nowNum += NUM;
initData();
sWipe.setRefreshing(false);
}
}, 1500);
}
}

//滚动时监听
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
mFootItem = lm.findLastVisibleItemPosition();
}
});
}

/**
* 加载数据方法(获取网络数据)
*/
@Override
public void loadData() {
// super.loadData();
//注册本类去监听数据加载状态
newsUtils = new NewsJsonUtils(this);
//加载数据
newsUtils.getUtils();
}

/**
* 回调更新界面的方法
* @param msgSt
*/
@Override
public void upData(List<OneBeanData> msgList, List<NewsBeanData>[] msgSt) {
this.msgSt = msgSt;
initData();//调用分配数据,填充布局的方法
}

//分配数据,填充布局的方法
public void initData(){
if (msgSt != null){
imgItem.clear();//清空缓存
titleItem.clear();
nowItem.clear();
mData = msgSt[4];
for (int i = 0; i < 3; i++) {
imgItem.add(mData.get(i).getThumbnail());
titleItem.add(mData.get(i).getTitle());
}
for (int i = 3; i < nowNum; i++) {
nowItem.add(mData.get(i));
}
}
adapterB.notifyDataSetChanged();
}

/**
* 下拉刷新方法
*/
@Override
public void onRefresh() {
nowNum += NUM;//刷新操作执行后,显示增加的内容
initData();//调用分配数据,填充布局的方法
sWipe.setRefreshing(false);
}

public Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
}

主要看RecyclerView适配器

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
143
144
145
146
/**
* Created by Dell on 2017/5/14.
* @author 小红妹
*/
public class ScienceTechnologyAdapter extends RecyclerView.Adapter implements View.OnClickListener{

private final int TYPE_HEAD = 0;//表示首个位置,直接显示图片播放
private final int TYPE_NORMAL = 1;//表示正常的item布局
private final int TYPE_FOOT = 2;//表示刷新布局
private Context mContext;
//保存了所有图片播放数据集合
private List<String> imgItem;
private List<String> titleItem;
//记录当前真正要显示item数据
private List<NewsBeanData> nowItem;

//新建一个私有变量用于保存用户设置的监听器
private OnItemClickListener mOnItemClickListener = null;

//set方法:
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}

@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
//注意这里使用getTag方法获取position
mOnItemClickListener.onItemClick(v,(int)v.getTag());
}
}

//define interface 自定义一个接口
public static interface OnItemClickListener {
void onItemClick(View view, int position);
}

/**
* 构造方法
* @param mContext
* @param imgItem
* @param titleItem
* @param nowItem
*/
public ScienceTechnologyAdapter(Context mContext, List<String> imgItem, List<String> titleItem, List<NewsBeanData> nowItem){
this.mContext=mContext;
this.imgItem = imgItem;
this.titleItem = titleItem;
this.nowItem = nowItem;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder=null;
if (viewType==TYPE_HEAD){
//此处创建顶部banner的ViewHolder
holder = new BannerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.news_banner_imager_b, parent, false));
}else if (viewType==TYPE_NORMAL){
View view = LayoutInflater.from(mContext).inflate(R.layout.news_banner_top, parent, false);
holder = new ItemViewHolder(view);
//将创建的View注册点击事件
view.setOnClickListener(this);
}else if (viewType==TYPE_FOOT){
holder = new FootViewHolder(LayoutInflater.from(mContext).inflate(R.layout.news_item_footer, parent, false));
}

return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

if (holder instanceof BannerViewHolder){
BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
bannerViewHolder.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
bannerViewHolder.banner.setBannerTitles(titleItem);
bannerViewHolder.banner.setImages(imgItem);
bannerViewHolder.banner.setImageLoader(new GlideImageLoader());
bannerViewHolder.banner.start();
} else if (holder instanceof ItemViewHolder) {
//处理每个item里的布局
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.simpleDraweeView.setImageURI(nowItem.get(position-1).getThumbnail());
itemViewHolder.textView.setText(nowItem.get(position-1).getTitle());//从零开始
//item的高度
ViewGroup.LayoutParams layoutParams = itemViewHolder.itemView.getLayoutParams();
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
}
//将position保存在itemView的Tag中,以便点击时进行获取
holder.itemView.setTag(position);
}

@Override
public int getItemCount() {
return nowItem.size()+1+1;
}

/**
* 告诉创建什么类型的ViewHolder
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
if (position==0){
return TYPE_HEAD;
}else if (position+1==getItemCount()){
return TYPE_FOOT;
}else {
return TYPE_NORMAL;
}
}

/**
* 正常的ViewHolder
*/
class ItemViewHolder extends RecyclerView.ViewHolder{
SimpleDraweeView simpleDraweeView;
TextView textView;

public ItemViewHolder(View itemView) {
super(itemView);
simpleDraweeView = (SimpleDraweeView) itemView.findViewById(R.id.simple_view);
textView=(TextView) itemView.findViewById(R.id.item_banner);
}
}
/**
* 首位的ViewHolder
*/
class BannerViewHolder extends RecyclerView.ViewHolder {
Banner banner;
public BannerViewHolder(View itemView) {
super(itemView);
banner = (Banner) itemView.findViewById(R.id.banner);
}
}
/**
* 底部的ViewHolder(上拉加载)
*/
class FootViewHolder extends RecyclerView.ViewHolder{

public FootViewHolder(View itemView) {
super(itemView);
}
}
}
全站博客共21.6k字(●'◡'●)ノ♥
Site by SmallRedSister using Hexo & Random
© 2018-2024 | 载入天数...载入时分秒... 总访问量 | 访客数人次

小红妹个人技术博客网站

Hide