PullZoomView是个不错的伸缩效果头部的控件,可以使用ListView、ScrollView和RecyclerView做出头部伸缩效果,但是不支持RecyclerView瀑布流布局,自己改轮子兼容.
控件的使用很简单,两个自定义属性,headerView为头部view,zoomView为后面那种有缩放的图片
<com.ecloud.pulltozoomview.demo.PullToZoomRecyclerViewEx
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:headerView="@layout/profile_head_view"
custom:zoomView="@layout/list_head_zoom_view" />
这是demo中使用GridLayoutManager配置的RecyclerView,效果如图
final GridLayoutManager manager = new GridLayoutManager(this, 2);
manager.setOrientation(GridLayoutManager.VERTICAL);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return mAdapter.getItemViewType(position) == RecyclerViewHeaderAdapter.INT_TYPE_HEADER ? 2 : 1;
}
});
listView.setAdapterAndLayoutManager(mAdapter, manager);
当你想换成StaggeredGridLayoutManager做瀑布流的时候,就留坑了,因为压根不支持,PullToZoomRecyclerViewEx的setAdapterAndLayoutManager参数只支持GridLayoutManager
public void setAdapterAndLayoutManager(RecyclerView.Adapter adapter, GridLayoutManager mLayoutManager) {
mRootView.setLayoutManager(mLayoutManager);
mRootView.setAdapter(adapter);
updateHeaderView();
}
好吧,既然写不出轮子就改造轮子吧,添加一个支持StaggeredGridLayoutManager方法看看咋样?
// PullToZoomRecyclerViewEx添加方法
public void setAdapterAndLayoutManager(RecyclerView.Adapter adapter, StaggeredGridLayoutManager mLayoutManager) {
mRootView.setLayoutManager(mLayoutManager);
mRootView.setAdapter(adapter);
updateHeaderView();
}
// PullToZoomRecyclerActivity改StaggeredGridLayoutManager
manager = new StaggeredGridLayoutManager(2,
StaggeredGridLayoutManager.VERTICAL);
listView.setAdapterAndLayoutManager(mAdapter, manager);
然后到作者封装的RecyclerViewHeaderAdapter的onBindViewHolder处理头部占据两格的操作,然后看到作者try catch住了设置瀑布流头部的代码,这里已经知道作者尝试过适配瀑布流然后没适配成,try catch用来调试的,既然这样还得看看效果是咋样的?再来改进
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (position >= headers.size() && (position - headers.size()) < getCount()) {
//noinspection unchecked
onBindView((V) holder, position - headers.size());
} else {
try {
final StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
lp.setFullSpan(true);
holder.itemView.setLayoutParams(lp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
还需要设置头部可见适配StaggeredGridLayoutManag