下面列出了android.support.v7.widget.StaggeredGridLayoutManager#HORIZONTAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 判断是否是最后一列
* 为什么要判断StaggeredGridLayoutManager?
* 这里需要注意的是StaggeredGridLayoutManager构造的第二个参数传一个orientation,
* 如果传入的是StaggeredGridLayoutManager.VERTICAL那么前面那个参数就代表有多少列;
* 如果传是StaggeredGridLayoutManager.HORIZONTAL那么前面那个参数就代表有多少行
*
* @param spanCount
* @param position
* @param childCount
* @param recyclerView
* @return
*/
private boolean isLastColumn(int spanCount, int position, int childCount, RecyclerView recyclerView) {
LayoutManager layoutManager = recyclerView.getLayoutManager();
//如果有headerView那就不一样咯
if (position >= mWrapAdapter.getHeaderCount()) {
if (layoutManager instanceof GridLayoutManager) {
if ((position - mWrapAdapter.getHeaderCount() + 1) % spanCount == 0) {
return true;
}
}
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
if (orientation == StaggeredGridLayoutManager.HORIZONTAL) {
//判断是否是最后一列
childCount = childCount - childCount % spanCount;
if (position >= childCount) {
return true;
}
} else {
if ((position + 1) % spanCount == 0) {
return true;
}
}
}
return false;
}
/**
* 处理水平loadmoreview
*/
private void setupLoadMore() {
boolean isHorizonal = false;
if (getLayoutManager() instanceof LinearLayoutManager) {
isHorizonal = ((LinearLayoutManager) getLayoutManager()).getOrientation() == LinearLayoutManager.HORIZONTAL;
} else if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
isHorizonal = ((StaggeredGridLayoutManager) getLayoutManager()).getOrientation() == StaggeredGridLayoutManager.HORIZONTAL;
}
mLoadMoreResourceId = R.layout.layout_default_more_view == mLoadMoreResourceId && isHorizonal ? R.layout.layout_default_horizontal_more_view : R.layout.layout_default_more_view;
}
@Override
protected RecyclerView.LayoutManager getLayoutManager() {
if ("0".equals(mIsVertical)) {
return new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
} else {
ViewGroup.LayoutParams layoutParams =
mTvRecyclerView.getLayoutParams();
layoutParams.width = ViewUtils.dpToPx(this, 886f);
mTvRecyclerView.setLayoutParams(layoutParams);
return new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
}
}
public int calcMoveNum(int moveDirection) {
RecyclerView.LayoutManager layoutManager =
mRecyclerView.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if (((GridLayoutManager) layoutManager).getOrientation() == GridLayoutManager.HORIZONTAL) {
if (moveDirection == KeyEvent.KEYCODE_DPAD_LEFT || moveDirection == KeyEvent.KEYCODE_DPAD_RIGHT) {
return ((GridLayoutManager) layoutManager).getSpanCount();
} else {
return 1;
}
} else {
if (moveDirection == KeyEvent.KEYCODE_DPAD_DOWN || moveDirection == KeyEvent.KEYCODE_DPAD_UP) {
return ((GridLayoutManager) layoutManager).getSpanCount();
} else {
return 1;
}
}
} else if (layoutManager instanceof LinearLayoutManager) {
return 1;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
if (((StaggeredGridLayoutManager) layoutManager).getOrientation() == StaggeredGridLayoutManager.HORIZONTAL) {
if (moveDirection == KeyEvent.KEYCODE_DPAD_LEFT || moveDirection == KeyEvent.KEYCODE_DPAD_DOWN) {
return ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
} else {
return 1;
}
} else {
if (moveDirection == KeyEvent.KEYCODE_DPAD_DOWN || moveDirection == KeyEvent.KEYCODE_DPAD_UP) {
return ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
} else {
return 1;
}
}
}
return 1;
}
private boolean isHorizontal( RecyclerView parent){
final LayoutManager manager = parent.getLayoutManager();
if(manager instanceof GridLayoutManager){
return ((GridLayoutManager) manager).getOrientation() == GridLayoutManager.HORIZONTAL;
}else if(manager instanceof StaggeredGridLayoutManager){
return ((StaggeredGridLayoutManager) manager).getOrientation()
== StaggeredGridLayoutManager.HORIZONTAL;
}else{
return false;
}
}
@SuppressWarnings( "UnusedParameters" )
private void initInstance( View rootView ){
int orientation = getResources().getConfiguration().orientation;
int orientationLinear;
int orientationGrid;
int orientationStaggeredGrid;
if( orientation == Configuration.ORIENTATION_PORTRAIT ){
orientationLinear = LinearLayoutManager.VERTICAL;
orientationGrid = GridLayoutManager.VERTICAL;
orientationStaggeredGrid = StaggeredGridLayoutManager.VERTICAL;
}else{
orientationLinear = LinearLayoutManager.HORIZONTAL;
orientationGrid = GridLayoutManager.HORIZONTAL;
orientationStaggeredGrid = StaggeredGridLayoutManager.HORIZONTAL;
}
rvMargin = (RecyclerView) rootView.findViewById( R.id.rv_margin );
int itemSpace = getSpace();
int layout = getArguments().getInt( KEY_LAYOUT );
if( layout == LINEAR ){
rvMargin.removeItemDecoration( linearMargin );
LinearLayoutManager layout1 = new LinearLayoutManager( getContext(), orientationLinear, false );
rvMargin.setLayoutManager( layout1 );
linearMargin = new LayoutMarginDecoration( itemSpace );
linearMargin.setPadding( rvMargin, getMarginTop(), getMarginBottom(), getMarginLeft(), getMarginRight() );
linearMargin.setOnClickLayoutMarginItemListener( onClickItem() );
rvMargin.addItemDecoration( linearMargin );
}else if( layout == GRID ){
int gridSpan = 3;
rvMargin.removeItemDecoration( gridMargin );
rvMargin.setLayoutManager( new GridLayoutManager( getContext(), gridSpan, orientationGrid, false ) );
gridMargin = new LayoutMarginDecoration( gridSpan, itemSpace );
gridMargin.setPadding( rvMargin, getMarginTop(), getMarginBottom(), getMarginLeft(), getMarginRight() );
gridMargin.setOnClickLayoutMarginItemListener( onClickItem() );
rvMargin.addItemDecoration( gridMargin );
}else if( layout == STAGGERED_GRID ){
int stagSpan = 3;
rvMargin.removeItemDecoration( stagMargin );
rvMargin.setLayoutManager( new StaggeredGridLayoutManager( stagSpan, orientationStaggeredGrid ) );
stagMargin = new LayoutMarginDecoration( stagSpan, itemSpace );
stagMargin.setPadding( rvMargin, getMarginTop(), getMarginBottom(), getMarginLeft(), getMarginRight() );
stagMargin.setOnClickLayoutMarginItemListener( onClickItem() );
rvMargin.addItemDecoration( stagMargin );
}
rvMargin.setAdapter( new MarginAdapter( getContext() ) );
}
public void init() {
View header = LayoutInflater.from(this).inflate(R.layout.layout_horitonal_header, null);
normalAdapterManager = new BindSuperAdapterManager();
normalAdapterManager
.bind(BindImageModel.class, R.layout.horizontal_image_item, BindImageHolder.class)
.bind(BindTextModel.class, R.layout.horizontal_text_item, BindTextHolder.class)
.bind(BindClickModel.class, R.layout.horizontal_click_item, BindClickHolder.class)
.bindEmpty(BindNoDataHolder.NoDataModel.class, BindNoDataHolder.ID, BindNoDataHolder.class)
.setNeedAnimation(true)
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(Context context, int position) {
//需要减去你的header和刷新的view的数量
Toast.makeText(context, "点击了!! " + position, Toast.LENGTH_SHORT).show();
}
})
.addHeaderView(header)
.setPullRefreshEnabled(true)
.setLoadingMoreEnabled(true)
.setFootView(new BindHorizontalCustomLoadMoreFooter(this))
.setRefreshHeader(new BindHorizontalCustomRefreshHeader(this))
.setLoadingListener(new OnLoadingListener() {
@Override
public void onRefresh() {
recycler.postDelayed(new Runnable() {
@Override
public void run() {
refresh();
}
}, 3000);
}
@Override
public void onLoadMore() {
recycler.postDelayed(new Runnable() {
@Override
public void run() {
loadMore();
}
}, 2000);
}
});
;
adapter = new BindSuperAdapter(this, normalAdapterManager, datas);
//瀑布流管理器
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
//staggeredGridLayoutManager.setReverseLayout(true);
recycler.setLayoutManager(staggeredGridLayoutManager);
//间隔线
recycler.addItemDecoration(new BindDecorationBuilder(adapter)
.setColor(getResources()
.getColor(R.color.material_deep_teal_500))
.setSpace(dip2px(this, 5))
.setNeedGridRightLeftEdge(true)
.setNeedFirstTopEdge(true)
.builder());
recycler.setAdapter(adapter);
}