下面列出了android.widget.LinearLayout#HORIZONTAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initRecyclerView() {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final RecycleViewItemLine line = new RecycleViewItemLine(this, LinearLayout.HORIZONTAL,
SizeUtils.dp2px(1), Color.parseColor("#f5f5f7"));
recyclerView.addItemDecoration(line);
adapter = new LocalOfficeAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
SwipeRefreshLayout swipeToRefresh = recyclerView.getSwipeToRefresh();
if (swipeToRefresh.isRefreshing()) {
recyclerView.setRefreshing(false);
}else {
initData();
}
}
});
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float progress = ((endProgress - startProgress) * interpolatedTime) + startProgress;
Log.i(TAG, "applyTransformation: interpolatedTime =" + interpolatedTime);
if (mOrientation == LinearLayout.HORIZONTAL) {
scrollBy((int) ((MAX_WIDTH - getScrollX()) * progress), 0);
} else {
float v = (MAX_WIDTH - getScrollY()) * progress;
Log.i(TAG, "applyTransformation: getScrollY() =" + getScrollY() + " progress = " + progress + " v =" + v);
scrollBy(0, (int) v);
}
if (progress >= 1) {
isRunAnim = false;
}
}
/**
* Set the orientation of this layout if it has changed,
* and center the elements based on the new orientation.
*/
private void checkOrientation(int orientation)
{
final boolean isHorizontal = LinearLayout.HORIZONTAL == getOrientation();
final boolean isPortrait = Configuration.ORIENTATION_PORTRAIT == orientation;
if (isPortrait && !isHorizontal)
{
// Portrait orientation is out of sync, setting to horizontal
// and reversing children
fixGravityAndPadding(LinearLayout.HORIZONTAL);
setOrientation(LinearLayout.HORIZONTAL);
reverseChildren();
requestLayout();
} else if (!isPortrait && isHorizontal)
{
// Landscape orientation is out of sync, setting to vertical
// and reversing children
fixGravityAndPadding(LinearLayout.VERTICAL);
setOrientation(LinearLayout.VERTICAL);
reverseChildren();
requestLayout();
}
}
public void updateBackground() {
mDrawableMap = new HashMap<>();
int count = super.getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
updateBackground(child);
// If this is the last view, don't set LayoutParams
if (i == count - 1) break;
LayoutParams initParams = (LayoutParams) child.getLayoutParams();
LayoutParams params = new LayoutParams(initParams.width, initParams.height, initParams.weight);
// Check orientation for proper margins
if (getOrientation() == LinearLayout.HORIZONTAL) {
params.setMargins(0, 0, -mMarginDp, 0);
} else {
params.setMargins(0, 0, 0, -mMarginDp);
}
child.setLayoutParams(params);
}
}
private void fill(RecyclerView.Recycler recycler, int scrollOffset) {
if (getItemCount() == 0) {
return;
}
if (mOrientation == LinearLayout.HORIZONTAL) {
fillWithHorizontal(recycler, scrollOffset);
} else {
fillWithVertical(recycler, scrollOffset);
}
if (mPageTransformer != null) {
View child;
for (int i = 0; i < getChildCount(); i++) {
child = getChildAt(i);
if (child == null) {
continue;
}
mPageTransformer.transformPage(child, calculateOffsetPercentToCenter(child, scrollOffset), mOrientation);
}
}
}
@Override
public void setOrientation(int orientation) {
if (LinearLayout.HORIZONTAL == orientation) {
throw new IllegalArgumentException("ExpandableTextView only supports Vertical Orientation.");
}
super.setOrientation(orientation);
}
@Override
public boolean canSwipeOut(View view, int position, int dx, int dy, int velocityX, int velocityY) {
int width = view.getWidth();
int height = view.getHeight();
boolean isOverBounds = mOrientation == LinearLayout.HORIZONTAL ? Math.abs(dx) >= width / 2 : Math.abs(dy) >= height / 2;
return isOverBounds || exceed(velocityX, velocityY);
}
private int getMinDistance() {
if (mOrientation == LinearLayout.HORIZONTAL) {
return getWidth() - getPaddingLeft() - getPaddingRight();
} else {
return getHeight() - getPaddingTop() - getPaddingBottom();
}
}
private void initYCRefreshView() {
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
final RecycleViewItemLine line = new RecycleViewItemLine(this, LinearLayout.HORIZONTAL,
SizeUtils.dp2px(1), Color.parseColor("#f5f5f7"));
recyclerView.addItemDecoration(line);
adapter = new DetailVideoAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setRefreshing(false);
recyclerView.scrollTo(0,0);
recyclerView.scrollBy(0,0);
addHeader();
}
@Override
public void setOrientation(int orientation) {
if (LinearLayout.HORIZONTAL == orientation) {
throw new IllegalArgumentException("ExpandableTextView only supports Vertical Orientation.");
}
super.setOrientation(orientation);
}
@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
// 如果在自定义ViewGroup之上还有父View交给我来处理
getParent().requestDisallowInterceptTouchEvent(true);
int orientation = getOrientation();
if (orientation == LinearLayout.HORIZONTAL) {
handleHorizontal(target, dx, consumed);
} else {
handleVertical(target, dy, consumed);
}
}
@Override
public void setOrientation(int orientation) {
if (LinearLayout.HORIZONTAL == orientation) {
throw new IllegalArgumentException("ExpandableTextView only supports Vertical Orientation.");
}
super.setOrientation(orientation);
}
private OrientationHelper getOrientationHelper() {
if (mOrientation == LinearLayout.HORIZONTAL) {
if (mHorizontalHelper == null) {
mHorizontalHelper = OrientationHelper.createHorizontalHelper(this);
}
return mHorizontalHelper;
} else {
if (mVerticalHelper == null) {
mVerticalHelper = OrientationHelper.createVerticalHelper(this);
}
return mVerticalHelper;
}
}
private LinearLayout.LayoutParams obtainLayoutParams() {
final int size = mDotSize;
final int margin = mDotMargin;
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(size, size);
if (getOrientation() == LinearLayout.HORIZONTAL) {
lp.gravity = Gravity.CENTER_VERTICAL;
lp.leftMargin = margin;
lp.rightMargin = margin;
} else {
lp.gravity = Gravity.CENTER_HORIZONTAL;
lp.topMargin = margin;
lp.bottomMargin = margin;
}
return lp;
}
/**
* @param child 计算的view
* @param offset view的滑动偏移量
* @return 返回view距离中心轴的距离
*/
private int calculateDistanceToCenter(View child, float offset) {
final OrientationHelper orientationHelper = getOrientationHelper();
final int centerToStart = (orientationHelper.getEndAfterPadding() - orientationHelper.getStartAfterPadding()) / 2 + orientationHelper.getStartAfterPadding();
if (mOrientation == LinearLayout.HORIZONTAL) {
return (int) (child.getWidth() / 2 - offset + child.getLeft() - centerToStart);
} else {
return (int) (child.getHeight() / 2 - offset + child.getTop() - centerToStart);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
int size = orientation == LinearLayout.HORIZONTAL ? width : height;
setVisibility(expansion == 0 && size == 0 ? GONE : VISIBLE);
int expansionDelta = size - Math.round(size * expansion);
if (parallax > 0) {
float parallaxDelta = expansionDelta * parallax;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (orientation == HORIZONTAL) {
int direction = -1;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
direction = 1;
}
child.setTranslationX(direction * parallaxDelta);
} else {
child.setTranslationY(-parallaxDelta);
}
}
}
if (orientation == HORIZONTAL) {
setMeasuredDimension(width - expansionDelta, height);
} else {
setMeasuredDimension(width, height - expansionDelta);
}
}
private void initView() {
top = findViewById(R.id.top);
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
final RecycleViewItemLine line = new RecycleViewItemLine(this, LinearLayout.HORIZONTAL,
(int)AppUtils.convertDpToPixel(1,this),
this.getResources().getColor(R.color.color_666666));
recyclerView.addItemDecoration(line);
recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter<PersonData>(this) {
@Override
public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
return new PersonViewHolder(parent);
}
});
adapter.setMore(R.layout.view_more2, new OnMoreListener() {
@Override
public void onMoreShow() {
//不做处理
}
@Override
public void onMoreClick() {
//点击触发加载下一页数据
handler.postDelayed(new Runnable() {
@Override
public void run() {
//刷新
if (!hasNetWork) {
adapter.pauseMore();
return;
}
adapter.addAll(DataProvider.getPersonList(10));
page++;
}
}, 200);
}
});
adapter.setNoMore(R.layout.view_nomore, new OnNoMoreListener() {
@Override
public void onNoMoreShow() {
adapter.pauseMore();
}
@Override
public void onNoMoreClick() {
Log.e("逗比","没有更多数据了");
}
});
adapter.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(int position) {
adapter.remove(position);
return true;
}
});
adapter.setError(R.layout.view_error, new OnErrorListener() {
@Override
public void onErrorShow() {
adapter.resumeMore();
}
@Override
public void onErrorClick() {
adapter.resumeMore();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_refresh_view);
recyclerView = findViewById(R.id.recyclerView);
adapter = new PersonAdapter(this);
recyclerView.setAdapter(adapter);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
DividerViewItemLine itemDecoration = new DividerViewItemLine(
this.getResources().getColor(R.color.color_f9f9f9),
LibUtils.dip2px(this,0.5f),
LibUtils.dip2px(this,72),0);
itemDecoration.setDrawLastItem(true);
itemDecoration.setDrawHeaderFooter(true);
recyclerView.addItemDecoration(itemDecoration);
final RecycleViewItemLine line = new RecycleViewItemLine(this,
LinearLayout.HORIZONTAL, 1, getResources().getColor(R.color.colorAccent));
recyclerView.addItemDecoration(line);
recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
adapter.clear();
adapter.addAll(DataProvider.getPersonList(0));
}
},1500);
}
});
//recyclerView.setRefreshing(true);
recyclerView.setRefreshingColorResources(R.color.colorAccent);
initHeader();
adapter.addAll(DataProvider.getPersonList(0));
adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
@Override
public void onItemChildClick(View view, int position) {
switch (view.getId()){
case R.id.iv_news_image:
Toast.makeText(HeaderFooterActivity.this,
"点击图片了",Toast.LENGTH_SHORT).show();
break;
case R.id.tv_title:
Toast.makeText(HeaderFooterActivity.this,
"点击标题",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
}
@Override
public boolean canScrollHorizontally() {
return mOrientation == LinearLayout.HORIZONTAL;
}
private void initRecyclerView() {
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
final RecycleViewItemLine line = new RecycleViewItemLine(this, LinearLayout.HORIZONTAL,
(int)AppUtils.convertDpToPixel(1,this),
this.getResources().getColor(R.color.color_f9f9f9));
recyclerView.addItemDecoration(line);
adapter = new PersonAdapter(this);
recyclerView.setAdapter(adapter);
adapter.setMore(R.layout.view_more, new OnLoadMoreListener() {
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
//刷新
if (!hasNetWork) {
adapter.pauseMore();
return;
}
adapter.addAll(DataProvider.getPersonList(page));
page++;
}
}, 2000);
}
});
adapter.setNoMore(R.layout.view_nomore, new OnNoMoreListener() {
@Override
public void onNoMoreShow() {
adapter.pauseMore();
}
@Override
public void onNoMoreClick() {
}
});
adapter.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(int position) {
adapter.remove(position);
return true;
}
});
adapter.setError(R.layout.view_error, new OnErrorListener() {
@Override
public void onErrorShow() {
adapter.resumeMore();
}
@Override
public void onErrorClick() {
adapter.resumeMore();
}
});
}