根据后台数据,动态添加Recycleview+headview
2020-06-02 本文已影响0人
丿灬序曲
外部布局LinearLayout videoreLayout
titles, Recycleview headview 标题名称
videoreLayout.removeAllViews();
for(int i =0 ;i<titles.size();i++){
RecyclerView recyclerView = new RecyclerView(getContext());
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = DensityUtil.dip2px(getContext(), 15);
layoutParams.rightMargin = DensityUtil.dip2px(getContext(), 15);
recyclerView.setLayoutParams(layoutParams);
List<XxxBean> typeList = new ArrayList<>();
JSONArray jsonArray = jinshanyunData.getJSONArray(titles.get(i)/*根据标题拿当前recycleview数据源,根据后台返回的json结构自由修改*/);
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject jsonObject1 = (JSONObject) jsonArray.get(j);
String name = jsonObject1 .getString("name");
...//后续自己根据数据结构添加
typeList.add(new XxxBean(name,...))
}
XXXAdapter xxxAdapter= new XXXAdapter (getActivity(), typeList);
xxxAdapter.setOnItemClickListener(new XXXAdapter .OnItemClickListener() {
@Override
public void onItemClick(int position) {
Log.e("info", "点击第" + position + "个");
}
});
recyclerView.setAdapter(xxxAdapter);
recyclerView.addItemDecoration(twodecoration/*分割距离**/);
recyclerView.setLayoutManager(/*recycleview 布局manager
上面自己new就行**/);
View view = View.inflate(getContext(), R.layout.head_item, null);
ImageView xxxtagImageView = view.findViewById(R.id.videorecommend_newtag);//recycleview标题布局的图案
xxxtagImageView .setTag(i);
xxxtagImageView .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i1 = (int) v.getTag();
//标题的点击事件
}
});
videoreLayout.addView(view);
videoreLayout.addView(recyclerView);
}
只是一次动态添加recycleview+headview,然后添加到布局,如果有性能问题或者有其他优化方式,感谢提醒