1. RecyclerView下StaggeredGridLayoutManager基本使用
recyclerView的简单使用在此不多口舌,先初始化StaggeredGridLayoutManager。
@Override
public void InitViewAndData() {
mRecyclerView = f(R.id.rv_attention);
mRefreshLayout = f(R.id.vhrf_attention);
mRecyclerView.setNestedScrollingEnabled(false);
layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE); //防止瀑布流图片闪烁
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
layoutManager.invalidateSpanAssignments();//防止瀑布流图片闪烁
}
});
}
2. 自定义ViewHolder
recyclerView封装了ViewHolder的回收复用机制,只需要按标准流程实现头部广告位的ViewHolder以及分栏Header条等item的ViewHolder。
public class LiveNoticeVH extends RecyclerView.ViewHolder {
private static final String TAG = "LiveNoticeVH";
private RecyclerView rv_live_notice;
private LiveNoticeAdapter mAdapter;
private TextView tv_header;
private FrameLayout fl_empty_view;
private LinearLayout ll_content_view;
public LiveNoticeVH(View itemView) {
super(itemView);
rv_live_notice = ViewUtils.f(itemView, R.id.rv_live_notice);
tv_header = ViewUtils.f(itemView, R.id.tv_header);
fl_empty_view = ViewUtils.f(itemView, R.id.fl_empty_view);
ll_content_view = ViewUtils.f(itemView, R.id.ll_content_view);
tv_header.setText("关注的直播");
// rv_live_notice.setLayoutManager(new GridLayoutManager(itemView.getContext(),2));
rv_live_notice.setLayoutManager(new LinearLayoutManager(itemView.getContext(), LinearLayoutManager.HORIZONTAL, false));
// rv_live_notice.setNestedScrollingEnabled(false);
mAdapter = new LiveNoticeAdapter(rv_live_notice);
rv_live_notice.setAdapter(mAdapter);
mAdapter.setOnRVItemClickListener(new BGAOnRVItemClickListener() {
@Override
public void onRVItemClick(ViewGroup parent, View itemView, int position) {
try {
SubscriptionInfo item = mAdapter.getItem(position);
if (ICoreManagerBase.getCore(ILivePluginCore.class) != null) {
ICoreManagerBase.getCore(ILivePluginCore.class).getTemplateSelectorInstance().universalToChannel(itemView.getContext(), item.sid, item.ssid, item.liveId, "HomeLiveNoticePush", item.templateId, null);
}
}catch (Exception e){
MLog.info(TAG,"ex="+e);
}
}
});
}
public void setData(List<SubscriptionInfo> attentionInfoList) {
MLog.info(TAG, "[initData] debug data=" + attentionInfoList);
// MLog.error("LiveNoticeVH","setData size="+attentionInfoList.size());
if (attentionInfoList == null) {
fl_empty_view.setVisibility(View.VISIBLE);
ll_content_view.setVisibility(View.GONE);
} else {
if (attentionInfoList.size() > 0) {
fl_empty_view.setVisibility(View.GONE);
ll_content_view.setVisibility(View.VISIBLE);
// mAdapter.notifyDataSetChanged();
} else {
fl_empty_view.setVisibility(View.VISIBLE);
ll_content_view.setVisibility(View.GONE);
}
}
mAdapter.setData(attentionInfoList);
mAdapter.notifyDataSetChanged();
}
}
3. 构造支持多种item类型的Adapter
从上面的配置我们清楚StaggeredGridLayoutManager显示2列数据,但是Banner位和header条是占据整行的,因此在onCreateViewHolder()里要通过setFullSpan()方式实现单条item占满整行。
public class ShenquAttentionRecyclerAdapter extends RecyclerView.Adapter {
public static final int VIEW_TYPE_LIVE_NOTICE = R.layout.vh_live_notice;
public static final int VIEW_TYPE_ATTENTION_VIDEO = R.layout.vh_attention_video;
public static final int VIEW_TYPE_BASE_HEADER = R.layout.vh_base_header;
public static final int VIEW_TYPE_EMPTY_ATTENTION_VIDEO=R.layout.vh_empty_attention_video;
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof LiveNoticeVH) {
LiveNoticeVH liveNoticeVH = (LiveNoticeVH) holder;
liveNoticeVH.setData(liveNoticeData);
} else if (holder instanceof AttentionVideoVH && attentionVideoList.size() > 0) {
MLog.debug(TAG, "position=" + position);
AttentionVideoVH attentionVideoVH = (AttentionVideoVH) holder;
attentionVideoVH.initData(attentionVideoList.get(position - count));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mClickListener != null) {
mClickListener.onItemClick(position - count, v, holder);
}
}
});
}else if(holder instanceof EmptyAttentionVideoVH ){
View itemView = holder.itemView;
RecyclerView.LayoutParams param= (RecyclerView.LayoutParams) itemView.getLayoutParams();
if (isHideAttentionView){
param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
param.width = LinearLayout.LayoutParams.MATCH_PARENT;
itemView.setVisibility(View.VISIBLE);
}else{
itemView.setVisibility(View.GONE);
param.height = 0;
param.width = 0;
}
itemView.setLayoutParams(param);
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext()); //Inflater为全局单例模式
View itemView = inflater.inflate(viewType, parent, false);
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams();
layoutParams.setFullSpan(true);//将 StaggeredGrid的某个item直接占满宽
if (viewType == VIEW_TYPE_LIVE_NOTICE) { //直播预览
LiveNoticeVH liveNoticeVH = new LiveNoticeVH(itemView);
return liveNoticeVH;
} else if (viewType == VIEW_TYPE_BASE_HEADER) { //标题头
return new BaseHeaderVH(itemView);
} else if (viewType == VIEW_TYPE_ATTENTION_VIDEO) {
AttentionVideoVH attentionVideoVH = new AttentionVideoVH(itemView);
layoutParams.setFullSpan(false);
return attentionVideoVH;
} else return new EmptyAttentionVideoVH(itemView);
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return VIEW_TYPE_LIVE_NOTICE;
} else if (position == 1) {
return VIEW_TYPE_BASE_HEADER;
}else if(position==2){
return VIEW_TYPE_EMPTY_ATTENTION_VIDEO ;
}
else {
return VIEW_TYPE_ATTENTION_VIDEO;
}
}
}