下面列出了android.widget.LinearLayout#VERTICAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Initializes a clipping rect for the header based on the margins of the header and the padding of the
* recycler.
* FIXME: Currently right margin in VERTICAL orientation and bottom margin in HORIZONTAL
* orientation are clipped so they look accurate, but the headers are not being drawn at the
* correctly smaller width and height respectively.
*
* @param clipRect {@link Rect} for clipping a provided header to the padding of a recycler view
* @param recyclerView for which to provide a header
* @param header for clipping
*/
private void initClipRectForHeader(Rect clipRect, RecyclerView recyclerView, View header) {
mDimensionCalculator.initMargins(clipRect, header);
if (mOrientationProvider.getOrientation(recyclerView) == LinearLayout.VERTICAL) {
clipRect.set(
recyclerView.getPaddingLeft(),
recyclerView.getPaddingTop(),
recyclerView.getWidth() - recyclerView.getPaddingRight() - clipRect.right,
recyclerView.getHeight() - recyclerView.getPaddingBottom());
} else {
clipRect.set(
recyclerView.getPaddingLeft(),
recyclerView.getPaddingTop(),
recyclerView.getWidth() - recyclerView.getPaddingRight(),
recyclerView.getHeight() - recyclerView.getPaddingBottom() - clipRect.bottom);
}
}
/**
* Initializes a clipping rect for the header based on the margins of the header and the padding of the
* recycler.
* FIXME: Currently right margin in VERTICAL orientation and bottom margin in HORIZONTAL
* orientation are clipped so they look accurate, but the headers are not being drawn at the
* correctly smaller width and height respectively.
*
* @param clipRect {@link Rect} for clipping a provided header to the padding of a recycler view
* @param recyclerView for which to provide a header
* @param header for clipping
*/
private void initClipRectForHeader(@NonNull Rect clipRect, @NonNull RecyclerView recyclerView, @NonNull View header) {
mDimensionCalculator.initMargins(clipRect, header);
if (mOrientationProvider.getOrientation(recyclerView) == LinearLayout.VERTICAL) {
clipRect.set(
recyclerView.getPaddingLeft(),
recyclerView.getPaddingTop(),
recyclerView.getWidth() - recyclerView.getPaddingRight() - clipRect.right,
recyclerView.getHeight() - recyclerView.getPaddingBottom());
} else {
clipRect.set(
recyclerView.getPaddingLeft(),
recyclerView.getPaddingTop(),
recyclerView.getWidth() - recyclerView.getPaddingRight(),
recyclerView.getHeight() - recyclerView.getPaddingBottom() - clipRect.bottom);
}
}
public DialerCallBar(Context context, AttributeSet attrs, int style) {
super(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.dialpad_additional_buttons, this, true);
findViewById(R.id.dialVideoButton).setOnClickListener(this);
findViewById(R.id.dialButton).setOnClickListener(this);
findViewById(R.id.deleteButton).setOnClickListener(this);
findViewById(R.id.deleteButton).setOnLongClickListener(this);
if(getOrientation() == LinearLayout.VERTICAL) {
LayoutParams lp;
for(int i=0; i < getChildCount(); i++) {
lp = (LayoutParams) getChildAt(i).getLayoutParams();
int w = lp.width;
lp.width = lp.height;
lp.height = w;
lp.gravity = Gravity.CENTER_HORIZONTAL;
// Added for clarity but not necessary
getChildAt(i).setLayoutParams(lp);
}
}
}
/**
* Gets a clipping rect for the header based on the margins of the header and the padding of the
* recycler.
* FIXME: Currently right margin in VERTICAL orientation and bottom margin in HORIZONTAL
* orientation are clipped so they look accurate, but the headers are not being drawn at the
* correctly smaller width and height respectively.
*
* @param recyclerView for which to provide a header
* @param header for clipping
* @return a {@link Rect} for clipping a provided header to the padding of a recycler view
*/
private Rect getClipRectForHeader(RecyclerView recyclerView, View header) {
Rect headerMargins = mDimensionCalculator.getMargins(header);
if (mOrientationProvider.getOrientation(recyclerView) == LinearLayout.VERTICAL) {
return new Rect(
recyclerView.getPaddingLeft(),
recyclerView.getPaddingTop(),
recyclerView.getWidth() - recyclerView.getPaddingRight() - headerMargins.right,
recyclerView.getHeight() - recyclerView.getPaddingBottom());
} else {
return new Rect(
recyclerView.getPaddingLeft(),
recyclerView.getPaddingTop(),
recyclerView.getWidth() - recyclerView.getPaddingRight(),
recyclerView.getHeight() - recyclerView.getPaddingBottom() - headerMargins.bottom);
}
}
private void findTopView() {
if ((mOrientation == LinearLayout.HORIZONTAL && Math.abs(mDx) > mMinDistance) ||
(mOrientation == LinearLayout.VERTICAL && Math.abs(mDy) > mMinDistance)) {
if (mAnimStatus == ANIMATION_IN) {
if (mOnCardSwipeListener != null) {
mOnCardSwipeListener.onAnimInStop(getViewByPosition(mTopPosition), mTopPosition);
}
} else if (mAnimStatus == ANIMATION_OUT) {
if (mOnCardSwipeListener != null) {
mOnCardSwipeListener.onAnimOutStop(getViewByPosition(mTopPosition), mTopPosition, mAnimDirection);
}
}
mAnimStatus = NO_ANIMATION;
mDx = 0;
mDy = 0;
mTopPosition++;
mIsScrollEnabled = false;
mIsSwipe = false;
mCardSwipeController.setDegree(0);
}
}
/**
* Initializes a clipping rect for the header based on the margins of the header and the padding of the
* recycler.
* FIXME: Currently right margin in VERTICAL orientation and bottom margin in HORIZONTAL
* orientation are clipped so they look accurate, but the headers are not being drawn at the
* correctly smaller width and height respectively.
*
* @param clipRect {@link Rect} for clipping a provided header to the padding of a recycler view
* @param recyclerView for which to provide a header
* @param header for clipping
*/
private void initClipRectForHeader(Rect clipRect, RecyclerView recyclerView, View header) {
mDimensionCalculator.initMargins(clipRect, header);
if (mOrientationProvider.getOrientation(recyclerView) == LinearLayout.VERTICAL) {
clipRect.set(
recyclerView.getPaddingLeft(),
recyclerView.getPaddingTop(),
recyclerView.getWidth() - recyclerView.getPaddingRight() - clipRect.right,
recyclerView.getHeight() - recyclerView.getPaddingBottom());
} else {
clipRect.set(
recyclerView.getPaddingLeft(),
recyclerView.getPaddingTop(),
recyclerView.getWidth() - recyclerView.getPaddingRight(),
recyclerView.getHeight() - recyclerView.getPaddingBottom() - clipRect.bottom);
}
}
/**
* 添加单个指示器
* @param backgroundResId
* @param animator
*/
private void addIndicator(@DrawableRes int backgroundResId, Animator animator){
if (animator.isRunning()){
animator.end();
animator.cancel();
}
View indicator = new View(getContext());
indicator.setBackgroundResource(backgroundResId);
addView(indicator, mIndicatorWidth, mIndicatorHeight);
LayoutParams lp = (LayoutParams) indicator.getLayoutParams();
if (mOrientation == LinearLayout.HORIZONTAL){
lp.leftMargin = mIndicatorMargin;
lp.rightMargin = mIndicatorMargin;
}else if (mOrientation == LinearLayout.VERTICAL){
lp.topMargin = mIndicatorMargin;
lp.bottomMargin = mIndicatorMargin;
}
indicator.setLayoutParams(lp);
animator.setTarget(indicator);
animator.start();
}
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
if (mOrientation == LinearLayout.VERTICAL) {
return new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
} else {
return new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
if (mOrientation == LinearLayout.VERTICAL) {
return 0;
}
if (getChildCount() == 0 || dx == 0) {
return 0;
}
int offset = -dx;
final OrientationHelper orientationHelper = getOrientationHelper();
final int centerToStart = (orientationHelper.getEndAfterPadding() - orientationHelper.getStartAfterPadding()) / 2 + orientationHelper.getStartAfterPadding();
View child;
if (dx > 0) {
child = getChildAt(getChildCount() - 1);
if (child != null && getPosition(child) == getItemCount() - 1 && !isLooper) {
// 计算全部加载完后item的偏移量,右边会留出空隙
offset = -Math.max(0, Math.min(dx, (child.getRight() - child.getLeft()) / 2 + child.getLeft() - centerToStart));
}
} else {
child = getChildAt(0);
if (mFirstVisiblePosition == 0 && child != null && !isLooper) {
// 计算首次加载item的偏移量,左边会留出空隙
offset = -Math.min(0, Math.max(dx, ((child.getRight() - child.getLeft()) / 2 + child.getLeft()) - centerToStart));
}
}
// 记录偏移量
getState().scrollOffset = -offset;
fill(recycler, -offset);
offsetChildrenHorizontal(offset);
return -offset;
}
@Override
public void setOrientation(int orientation){
if(LinearLayout.VERTICAL == orientation){
throw new IllegalArgumentException("DragLinearLayout must be HORIZONTAL.");
}
super.setOrientation(orientation);
}
/**
* Determines if a view should have a sticky header.
* The view has a sticky header if:
* 1. It is the first element in the recycler view
* 2. It has a valid ID associated to its position
*
* @param itemView given by the RecyclerView
* @param orientation of the Recyclerview
* @param position of the list item in question
* @return True if the view should have a sticky header
*/
public boolean hasStickyHeader(@NonNull View itemView, int orientation, int position) {
int offset, margin;
mDimensionCalculator.initMargins(mTempRect1, itemView);
if (orientation == LinearLayout.VERTICAL) {
offset = itemView.getTop();
margin = mTempRect1.top;
} else {
offset = itemView.getLeft();
margin = mTempRect1.left;
}
return offset <= margin && mAdapter.getHeaderId(position) >= 0;
}
@Override
public void setOrientation(int orientation) {
if (LinearLayout.VERTICAL != orientation) {
throw new IllegalArgumentException(
"This class only supports VERTICAL orientation.");
}
// Only support vertical orientation
super.setOrientation(orientation);
}
/**
* Determines if a view should have a sticky header.
* The view has a sticky header if:
* 1. It is the first element in the recycler view
* 2. It has a valid ID associated to its position
*
* @param itemView given by the RecyclerView
* @param orientation of the Recyclerview
* @param position of the list item in question
* @return True if the view should have a sticky header
*/
public boolean hasStickyHeader(View itemView, int orientation, int position) {
int offset, margin;
mDimensionCalculator.initMargins(mTempRect1, itemView);
if (orientation == LinearLayout.VERTICAL) {
offset = itemView.getTop();
margin = mTempRect1.top;
} else {
offset = itemView.getLeft();
margin = mTempRect1.left;
}
return offset <= margin && mAdapter.getHeaderId(position) >= 0;
}
/**
* Determines if a view should have a sticky header.
* The view has a sticky header if:
* 1. It is the first element in the recycler view
* 2. It has a valid ID associated to its position
*
* @param itemView given by the RecyclerView
* @param orientation of the Recyclerview
* @param position of the list item in question
* @return True if the view should have a sticky header
*/
public boolean hasStickyHeader(View itemView, int orientation, int position) {
int offset, margin;
mDimensionCalculator.initMargins(mTempRect1, itemView);
if (orientation == LinearLayout.VERTICAL) {
offset = itemView.getTop();
margin = mTempRect1.top;
} else {
offset = itemView.getLeft();
margin = mTempRect1.left;
}
return offset <= margin && mAdapter.getHeaderId(position) >= 0;
}
public void setFooterView(View footerView, int orientation) {
if (null == footerView.getLayoutParams()) {
if (LinearLayout.VERTICAL == orientation) {
footerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
} else {
footerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
}
this.mFooterView = footerView;
this.mHasFooter = true;
}
/**
* Swap gravity:
* left for bottom
* right for top
* center horizontal for center vertical
* etc
* <p>
* also swap left|right padding for bottom|top
*/
private void fixGravityAndPadding(int direction)
{
for (int i = 0; i < getChildCount(); i++)
{
// gravity swap
View v = getChildAt(i);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
int gravity = layoutParams.gravity;
if (direction == LinearLayout.VERTICAL)
{
if ((gravity & Gravity.LEFT) != 0)
{ // if gravity left is set . . .
gravity &= ~Gravity.LEFT; // unset left
gravity |= Gravity.BOTTOM; // and set bottom
}
} else
{
if ((gravity & Gravity.BOTTOM) != 0)
{ // etc
gravity &= ~Gravity.BOTTOM;
gravity |= Gravity.LEFT;
}
}
if (direction == LinearLayout.VERTICAL)
{
if ((gravity & Gravity.RIGHT) != 0)
{
gravity &= ~Gravity.RIGHT;
gravity |= Gravity.TOP;
}
} else
{
if ((gravity & Gravity.TOP) != 0)
{
gravity &= ~Gravity.TOP;
gravity |= Gravity.RIGHT;
}
}
// don't mess with children that are centered in both directions
if ((gravity & Gravity.CENTER) != Gravity.CENTER)
{
if (direction == LinearLayout.VERTICAL)
{
if ((gravity & Gravity.CENTER_VERTICAL) != 0)
{
gravity &= ~Gravity.CENTER_VERTICAL;
gravity |= Gravity.CENTER_HORIZONTAL;
}
} else
{
if ((gravity & Gravity.CENTER_HORIZONTAL) != 0)
{
gravity &= ~Gravity.CENTER_HORIZONTAL;
gravity |= Gravity.CENTER_VERTICAL;
}
}
}
layoutParams.gravity = gravity;
// padding swap
int paddingLeft = v.getPaddingLeft();
int paddingTop = v.getPaddingTop();
int paddingRight = v.getPaddingRight();
int paddingBottom = v.getPaddingBottom();
v.setPadding(paddingBottom, paddingRight, paddingTop, paddingLeft);
}
}
@Override
public boolean canScrollVertically() {
return mOrientation == LinearLayout.VERTICAL;
}
private void setupConfiguration(int orientation, String type) {
if(orientation==LinearLayout.VERTICAL)
setLayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
else
setLayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,0);
fixOrientation(layout,orientation);
if(type.equals(TYPE_ARRAY)){
}else if(type.equals(TYPE_FLOAT)){
}else{
}
}
private boolean isVertical() {
return getOrientation() == LinearLayout.VERTICAL;
}
@OnClick(R.id.bbutton_group_orientation_change_btn) void onOrientationChangeExampleClicked() {
boolean isHorizontal = orientationChange.getOrientation() == LinearLayout.HORIZONTAL;
int newOrientation = isHorizontal ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL;
orientationChange.setOrientation(newOrientation);
}