下面列出了怎么用android.support.v7.widget.OrientationHelper的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void initView() {
initData();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
FrameLayout headView = (FrameLayout) LayoutInflater.from(getContext()).inflate(R.layout.banner_home, null);
homeBanner = headView.findViewById(R.id.home_banner);
headView.removeView(homeBanner);
homeBanner.setImageLoader(new HomeBannerLoader());
homeBanner.setImages(bannerList);
homeBanner.start();
commonArticleAdapter = new CommonArticleAdapter(R.layout.item_article, articleList);
commonArticleAdapter.addHeaderView(homeBanner);
commonArticleAdapter.setEnableLoadMore(true);
recyclerView.setAdapter(commonArticleAdapter);
refreshAndloadMore();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RecyclerView recyclerView = new RecyclerView(this);
setContentView(recyclerView);
DisplayMetrics dm = getResources().getDisplayMetrics();
size = dm.widthPixels >> 1;
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
layoutManager.setOrientation(OrientationHelper.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
RecyclerAdapter adapter = new RecyclerAdapter(initData());
recyclerView.setAdapter(adapter);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (type == TYPE_GRID_LAYOUT) {
gridLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(gridLayoutManager);//这里用线性宫格显示 类似于grid view
} else if (type == TYPE_STAGGERED_GRID_LAYOUT) {
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
} else {
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//这里用线性显示 类似于list view
}
mAdapter = new HeaderBottomItemAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
if (gridLayoutManager != null) {//设置头部及底部View占据整行空间
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (mAdapter.isHeaderView(position) || mAdapter.isBottomView(position)) ? gridLayoutManager.getSpanCount() : 1;
}
});
}
}
private int getDistanceToEnd(View targetView, @NonNull OrientationHelper helper) {
int distance;
if (!snapToPadding) {
int childEnd = helper.getDecoratedEnd(targetView);
if (childEnd >= helper.getEnd() - (helper.getEnd() - helper.getEndAfterPadding()) / 2) {
distance = helper.getDecoratedEnd(targetView) - helper.getEnd();
} else {
distance = childEnd - helper.getEndAfterPadding();
}
} else {
distance = helper.getDecoratedEnd(targetView) - helper.getEndAfterPadding();
}
return distance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
// recyclerview.setLayoutManager(new GridLayoutManager(this, 3));
// recyclerview.setLayoutManager(new LinearLayoutManager(this,
// LinearLayoutManager.VERTICAL, false));
recyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
Divider divider = new Divider(new ColorDrawable(0xffff0000), OrientationHelper.VERTICAL);
//单位:px
divider.setMargin(50, 50, 50, 50);
divider.setHeight(20);
recyclerview.addItemDecoration(divider);
recyclerview.setAdapter(new Demo3Adapter(this));
}
public PatchedRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.PatchedRecyclerView);
boolean nestedScrollingEnabled = ta
.getBoolean(R.styleable.PatchedRecyclerView_nestedScrollingEnabled, true);
boolean hasFixedSize = ta.getBoolean(R.styleable.PatchedRecyclerView_hasFixedSize, false);
int orientation =
ta.getInt(R.styleable.PatchedRecyclerView_orientation, OrientationHelper.VERTICAL);
ta.recycle();
LayoutManager layoutManager = getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
int currOrientation = linearLayoutManager.getOrientation();
if (currOrientation != orientation) {
linearLayoutManager.setOrientation(orientation);
}
}
setHasFixedSize(hasFixedSize);
setNestedScrollingEnabled(nestedScrollingEnabled);
setFocusable(nestedScrollingEnabled);
}
/**
* 当item位置变换,滚动recycler到正确的位置
* TODO: 2017/2/21 0021 整理更优雅的写法 还有scrollToPosition(0)是否必要?
*/
private void scrollToRightPositionWhenItemChanged(RecyclerView recyclerView, View itemView, int itemPos) {
final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof ItemTouchHelper.ViewDropHandler) {
OrientationHelper helper = OrientationHelper.createVerticalHelper(layoutManager);
int start = helper.getDecoratedStart(itemView);
int end = helper.getDecoratedEnd(itemView);
((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(
itemPos, lastItemPos > itemPos ? start : end - itemViewHeight);
// System.out.println(lastItemPos + "-" + childPos + "OrientationHelperOrientationHelper:"
// + height + "==" + itemViewHeight + "=||=" + start + "===" + end + "||||||" + myStart + "===" + itemTargetView.getHeight() );
}
if (lastItemPos == 0 || itemPos == 0) {
recyclerView.scrollToPosition(0);
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (type == TYPE_GRID_LAYOUT) {
gridLayoutManager = new GridLayoutManager(getActivity(), 2);
mAttackView.setLayoutManager(gridLayoutManager);//这里用线性宫格显示 类似于grid view
} else if (type == TYPE_STAGGERED_GRID_LAYOUT) {
mAttackView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
} else {
mAttackView.setLayoutManager(new LinearLayoutManager(getActivity()));//这里用线性显示 类似于list view
}
mAdapter = new HeaderBottomItemAdapter(getActivity());
mAdapter.setHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.item_image, mAttackView, false));
mAdapter.setFooterView(LayoutInflater.from(getActivity()).inflate(R.layout.item_image, mAttackView, false));
mAttackView.setAdapter(mAdapter);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (type == TYPE_GRID_LAYOUT) {
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));//这里用线性宫格显示 类似于grid view
} else if (type == TYPE_STAGGERED_GRID_LAYOUT) {
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
} else {
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//这里用线性显示 类似于list view V23.2.0 支持自动计算高度大小,也就是可以嵌套了
}
mAdapter = new AnimAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
}
@NonNull
private OrientationHelper getHorizontalHelper(
@NonNull RecyclerView.LayoutManager layoutManager) {
if (mHorizontalHelper == null) {
mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
}
return mHorizontalHelper;
}
private int distanceToStart(View targetView, @NonNull OrientationHelper helper, boolean fromEnd) {
if (isRtlHorizontal && !fromEnd) {
return distanceToEnd(targetView, helper, true);
}
return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}
private int distanceToEnd(View targetView, @NonNull OrientationHelper helper, boolean fromStart) {
if (isRtlHorizontal && !fromStart) {
return distanceToStart(targetView, helper, true);
}
return helper.getDecoratedEnd(targetView) - helper.getEndAfterPadding();
}
private void initData() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(OrientationHelper.VERTICAL);
// 设置布局管理器
mContentRV.setLayoutManager(layoutManager);
ArrayList dataList = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
dataList.add("DIY-ITEM:" + i);
}
RecyclerAdapter adapter = new RecyclerAdapter(dataList);
mContentRV.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_av_settings_preview);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new AVSettingsPreviewAdapater(avConfigInfoList);
layoutManager.setOrientation(OrientationHelper.VERTICAL);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addItemDecoration(new ItemDecoration(this, 1));
loadChangedSettings();
}
private View getStartView(RecyclerView.LayoutManager layoutManager,
OrientationHelper helper) {
if (layoutManager instanceof LinearLayoutManager) {
int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
boolean isLastItem = ((LinearLayoutManager) layoutManager)
.findLastCompletelyVisibleItemPosition()
== layoutManager.getItemCount() - 1;
if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
return null;
}
View child = layoutManager.findViewByPosition(firstChild);
if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
&& helper.getDecoratedEnd(child) > 0) {
return child;
} else {
if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
== layoutManager.getItemCount() - 1) {
return null;
} else {
return layoutManager.findViewByPosition(firstChild + 1);
}
}
}
return super.findSnapView(layoutManager);
}
View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
boolean acceptPartiallyVisible) {
OrientationHelper helper;
if (layoutManager.canScrollVertically()) {
helper = OrientationHelper.createVerticalHelper(layoutManager);
} else {
helper = OrientationHelper.createHorizontalHelper(layoutManager);
}
final int start = helper.getStartAfterPadding();
final int end = helper.getEndAfterPadding();
final int next = toIndex > fromIndex ? 1 : -1;
View partiallyVisible = null;
for (int i = fromIndex; i != toIndex; i += next) {
final View child = layoutManager.getChildAt(i);
final int childStart = helper.getDecoratedStart(child);
final int childEnd = helper.getDecoratedEnd(child);
if (childStart < end && childEnd > start) {
if (completelyVisible) {
if (childStart >= start && childEnd <= end) {
return child;
} else if (acceptPartiallyVisible && partiallyVisible == null) {
partiallyVisible = child;
}
} else {
return child;
}
}
}
return partiallyVisible;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
SimpleAdapter adapter = new SimpleAdapter(this);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(OrientationHelper.VERTICAL);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recyclerview);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (type == TYPE_GRID_LAYOUT) {
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));//这里用线性宫格显示 类似于grid view
} else if (type == TYPE_STAGGERED_GRID_LAYOUT) {
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
} else {
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));//这里用线性显示 类似于list view
}
mAdapter = new AnimAdapter(getActivity());
mRecyclerView.setAdapter(mAdapter);
// mRecyclerView.setItemAnimator(new DefaultItemAnimator());
}
public void initView(View view) {
recyclerView = (RecyclerView) view.findViewById(R.id.mAuthorization);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new AuthorizationAdapter(getContext(), lists);
recyclerView.setAdapter(adapter);
adapter.setAuthorizationOnItemClickListener(this);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (type == TYPE_GRID_LAYOUT) {
mAttackview.setLayoutManager(new GridLayoutManager(getActivity(), 2));//这里用线性宫格显示 类似于grid view
} else if (type == TYPE_STAGGERED_GRID_LAYOUT) {
mAttackview.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
} else {
mAttackview.setLayoutManager(new LinearLayoutManager(getActivity()));//这里用线性显示 类似于list view
}
mAttackview.setAdapter(mTitanAdapter = new NormalRecyclerViewAdapter(getActivity()));
if (TYPE_LINEAR_LOAD_MORE_LAYOUT == type) {
mAttackview.setOnLoadMoreListener(new TitanRecyclerView.OnLoadMoreListener() {
@Override
public void onLoadMore() {
loadItem();
}
});
}
mAttackview.setItemAnimator(new DefaultItemAnimator());
mTitanAdapter.setFooterView(LayoutInflater.from(getActivity()).inflate(R.layout.footer_view, null));
int index = mTitanAdapter.getData().size();
int end = index + 20;
List<String> datas = new ArrayList<String>();
for (int i = index; i < end; i++) {
datas.add("item" + i);
}
mTitanAdapter.addDataEnd(datas);
}
/**
* 返回条目之间的间隔,例如我们想仿照ListView一样添加分割线,那么就需要设置outRect的下边距。
* @param outRect
* @param view
* @param parent
* @param state
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view);
int orientation = 0;
int headerCount = 0,footerCount = 0;
if (parent.getAdapter() instanceof XRecyclerViewAdapter){
headerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
footerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
}
RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
if (layoutManager instanceof StaggeredGridLayoutManager){
orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
}else if (layoutManager instanceof GridLayoutManager){
orientation = ((GridLayoutManager) layoutManager).getOrientation();
}else if (layoutManager instanceof LinearLayoutManager){
orientation = ((LinearLayoutManager) layoutManager).getOrientation();
}
if (position>=headerCount&&position<parent.getAdapter().getItemCount()-footerCount||mDrawHeaderFooter){
if (orientation == OrientationHelper.VERTICAL){
outRect.bottom = mHeight;
}else {
outRect.right = mHeight;
}
}
}
private int getDistanceToStart(View targetView, @NonNull OrientationHelper helper) {
int distance;
// If we don't care about padding, just snap to the start of the view
if (!snapToPadding) {
int childStart = helper.getDecoratedStart(targetView);
if (childStart >= helper.getStartAfterPadding() / 2) {
distance = childStart - helper.getStartAfterPadding();
} else {
distance = childStart;
}
} else {
distance = helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
}
return distance;
}
private OrientationHelper getOrientationHelper() {
if (mOrientation == HORIZONTAL) {
if (mHorizontalHelper == null) {
mHorizontalHelper = OrientationHelper.createHorizontalHelper(this);
}
return mHorizontalHelper;
} else {
if (mVerticalHelper == null) {
mVerticalHelper = OrientationHelper.createVerticalHelper(this);
}
return mVerticalHelper;
}
}
private int estimateNextPositionDiffForFling(RecyclerView.LayoutManager layoutManager,
OrientationHelper helper, int velocityX, int velocityY) {
int[] distances = calculateScrollDistance(velocityX, velocityY);
float distancePerChild = computeDistancePerChild(layoutManager, helper);
if (distancePerChild <= 0) {
return 0;
}
int distance = distances[0];
if (distance > 0) {
return (int) Math.floor(distance / distancePerChild);
} else {
return (int) Math.ceil(distance / distancePerChild);
}
}
private View findEndView(RecyclerView.LayoutManager layoutManager,
OrientationHelper helper) {
if (layoutManager instanceof LinearLayoutManager) {
int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
if (lastChild == RecyclerView.NO_POSITION) {
return null;
}
View child = layoutManager.findViewByPosition(lastChild);
float visibleWidth;
if (isRtlHorizontal) {
visibleWidth = (float) helper.getDecoratedEnd(child)
/ helper.getDecoratedMeasurement(child);
} else {
visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
/ helper.getDecoratedMeasurement(child);
}
// If we're at the start of the list, we shouldn't snap
// to avoid having the first item not completely visible.
boolean startOfList = ((LinearLayoutManager) layoutManager)
.findFirstCompletelyVisibleItemPosition() == 0;
if (visibleWidth > 0.5f && !startOfList) {
return child;
} else if (startOfList) {
return null;
} else {
// If the child wasn't returned, we need to return the previous view
return layoutManager.findViewByPosition(lastChild - 1);
}
}
return null;
}
private void initLayoutManagerType() {
this.mLayoutManagerType = mRecyclerViewListener.getCurLayoutManagerType();
RecyclerView.LayoutManager layoutManager = mRecyclerViewListener.getCurLayoutManager();
switch (mLayoutManagerType) {
case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR:
LinearLayoutManager curLinearLayoutManager = (LinearLayoutManager)layoutManager;
if (curLinearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
mOrientation = OrientationHelper.HORIZONTAL;
} else {
mOrientation = OrientationHelper.VERTICAL;
}
break;
case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_GRID:
GridLayoutManager curGridLayoutManager = (GridLayoutManager)layoutManager;
mGridSpanCount = curGridLayoutManager.getSpanCount();
if (curGridLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
mOrientation = OrientationHelper.HORIZONTAL;
} else {
mOrientation = OrientationHelper.VERTICAL;
}
break;
case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_STAGGERED_GRID:
StaggeredGridLayoutManager curStaggeredGridLayoutManager = (StaggeredGridLayoutManager)layoutManager;
mGridSpanCount = curStaggeredGridLayoutManager.getSpanCount();
if (curStaggeredGridLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
mOrientation = OrientationHelper.HORIZONTAL;
} else {
mOrientation = OrientationHelper.VERTICAL;
}
break;
default:
this.mLayoutManagerType = FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR;
}
initDivisible();
}
@NonNull
private OrientationHelper getHorizontalHelper(
@NonNull RecyclerView.LayoutManager layoutManager) {
if (mHorizontalHelper == null) {
mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
}
return mHorizontalHelper;
}
private int distanceToEnd(View targetView, @NonNull OrientationHelper helper, boolean fromStart) {
if (isRtlHorizontal && !fromStart) {
return distanceToStart(targetView, helper, true);
}
return helper.getDecoratedEnd(targetView) - helper.getEndAfterPadding();
}
public OrientationHelper getOrientationHelper() {
if (mOrientation == HORIZONTAL) {
if (mHorizontalHelper == null) {
mHorizontalHelper = OrientationHelper.createHorizontalHelper(this);
}
return mHorizontalHelper;
} else {
if (mVerticalHelper == null) {
mVerticalHelper = OrientationHelper.createVerticalHelper(this);
}
return mVerticalHelper;
}
}
private int estimateNextPositionDiffForFling(RecyclerView.LayoutManager layoutManager, OrientationHelper helper, int velocityX, int velocityY) {
int[] distances = calculateScrollDistance(velocityX, velocityY);
float distancePerChild = computeDistancePerChild(layoutManager, helper);
if (distancePerChild <= 0) {
return 0;
}
int distance = distances[0];
if (distance > 0) {
return (int) Math.floor(distance / distancePerChild);
} else {
return (int) Math.ceil(distance / distancePerChild);
}
}