use of com.alibaba.android.vlayout.layout.ColumnLayoutHelper in project vlayout by alibaba.
the class VLayoutActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mFirstText = (TextView) findViewById(R.id.first);
mLastText = (TextView) findViewById(R.id.last);
mCountText = (TextView) findViewById(R.id.count);
mTotalOffsetText = (TextView) findViewById(R.id.total_offset);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_view);
findViewById(R.id.jump).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText position = (EditText) findViewById(R.id.position);
if (!TextUtils.isEmpty(position.getText())) {
try {
int pos = Integer.parseInt(position.getText().toString());
recyclerView.scrollToPosition(pos);
} catch (Exception e) {
Log.e("VlayoutActivity", e.getMessage(), e);
}
} else {
recyclerView.requestLayout();
}
}
});
final VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
}
@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
mFirstText.setText("First: " + layoutManager.findFirstVisibleItemPosition());
mLastText.setText("Existing: " + MainViewHolder.existing + " Created: " + MainViewHolder.createdTimes);
mCountText.setText("Count: " + recyclerView.getChildCount());
mTotalOffsetText.setText("Total Offset: " + layoutManager.getOffsetToStart());
}
});
recyclerView.setLayoutManager(layoutManager);
// layoutManager.setReverseLayout(true);
RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = ((LayoutParams) view.getLayoutParams()).getViewPosition();
outRect.set(4, 4, 4, 4);
}
};
final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
recyclerView.setRecycledViewPool(viewPool);
// recyclerView.addItemDecoration(itemDecoration);
viewPool.setMaxRecycledViews(0, 20);
final DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, true);
recyclerView.setAdapter(delegateAdapter);
List<DelegateAdapter.Adapter> adapters = new LinkedList<>();
if (BANNER_LAYOUT) {
adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 1) {
@Override
public void onViewRecycled(MainViewHolder holder) {
if (holder.itemView instanceof ViewPager) {
((ViewPager) holder.itemView).setAdapter(null);
}
}
@Override
public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 1)
return new MainViewHolder(LayoutInflater.from(VLayoutActivity.this).inflate(R.layout.view_pager, parent, false));
return super.onCreateViewHolder(parent, viewType);
}
@Override
public int getItemViewType(int position) {
return 1;
}
@Override
protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {
}
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
if (holder.itemView instanceof ViewPager) {
ViewPager viewPager = (ViewPager) holder.itemView;
viewPager.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));
// from position to get adapter
viewPager.setAdapter(new PagerAdapter(this, viewPool));
}
}
});
}
if (FLOAT_LAYOUT) {
FloatLayoutHelper layoutHelper = new FloatLayoutHelper();
layoutHelper.setAlignType(FixLayoutHelper.BOTTOM_RIGHT);
layoutHelper.setDefaultLocation(100, 400);
LayoutParams layoutParams = new LayoutParams(150, 150);
adapters.add(new SubAdapter(this, layoutHelper, 1, layoutParams));
}
if (LINEAR_LAYOUT) {
LinearLayoutHelper layoutHelper1 = new LinearLayoutHelper();
layoutHelper1.setAspectRatio(2.0f);
LinearLayoutHelper layoutHelper2 = new LinearLayoutHelper();
layoutHelper2.setAspectRatio(4.0f);
layoutHelper2.setDividerHeight(10);
layoutHelper2.setMargin(10, 30, 10, 10);
layoutHelper2.setPadding(10, 30, 10, 10);
layoutHelper2.setBgColor(0xFFF5A623);
adapters.add(new SubAdapter(this, layoutHelper1, 1));
adapters.add(new SubAdapter(this, layoutHelper2, 6) {
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
if (position % 2 == 0) {
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
layoutParams.mAspectRatio = 5;
holder.itemView.setLayoutParams(layoutParams);
}
}
});
}
if (STICKY_LAYOUT) {
StickyLayoutHelper layoutHelper = new StickyLayoutHelper();
layoutHelper.setOffset(100);
layoutHelper.setAspectRatio(4);
adapters.add(new SubAdapter(this, layoutHelper, 1, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
}
if (SINGLE_LAYOUT) {
SingleLayoutHelper layoutHelper = new SingleLayoutHelper();
layoutHelper.setBgColor(Color.rgb(135, 225, 90));
layoutHelper.setAspectRatio(4);
layoutHelper.setMargin(10, 20, 10, 20);
layoutHelper.setPadding(10, 10, 10, 10);
adapters.add(new SubAdapter(this, layoutHelper, 1, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
}
if (COLUMN_LAYOUT) {
ColumnLayoutHelper layoutHelper = new ColumnLayoutHelper();
layoutHelper.setBgColor(0xff00f0f0);
layoutHelper.setWeights(new float[] { 40.0f, Float.NaN, 40 });
adapters.add(new SubAdapter(this, layoutHelper, 5) {
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
if (position == 0) {
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
layoutParams.mAspectRatio = 4;
holder.itemView.setLayoutParams(layoutParams);
}
}
});
}
if (ONEN_LAYOUT) {
OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
helper.setBgColor(0xff876384);
helper.setAspectRatio(4.0f);
helper.setColWeights(new float[] { 40f, 45f });
helper.setMargin(10, 20, 10, 20);
helper.setPadding(10, 10, 10, 10);
adapters.add(new SubAdapter(this, helper, 2));
}
if (ONEN_LAYOUT) {
OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
helper.setBgColor(0xffef8ba3);
helper.setAspectRatio(2.0f);
helper.setColWeights(new float[] { 40f });
helper.setRowWeight(30f);
helper.setMargin(10, 20, 10, 20);
helper.setPadding(10, 10, 10, 10);
adapters.add(new SubAdapter(this, helper, 4) {
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
LayoutParams lp = (LayoutParams) holder.itemView.getLayoutParams();
if (position == 0) {
lp.rightMargin = 1;
} else if (position == 1) {
} else if (position == 2) {
lp.topMargin = 1;
lp.rightMargin = 1;
}
}
});
}
if (ONEN_LAYOUT) {
adapters.add(new SubAdapter(this, new OnePlusNLayoutHelper(), 0));
OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
helper.setBgColor(0xff87e543);
helper.setAspectRatio(1.8f);
helper.setColWeights(new float[] { 33.33f, 50f, 40f });
helper.setMargin(10, 20, 10, 20);
helper.setPadding(10, 10, 10, 10);
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
adapters.add(new SubAdapter(this, helper, 3, lp) {
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
LayoutParams lp = (LayoutParams) holder.itemView.getLayoutParams();
if (position == 0) {
lp.rightMargin = 1;
}
}
});
}
if (COLUMN_LAYOUT) {
adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 0));
adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 4));
}
if (FIX_LAYOUT) {
FixLayoutHelper layoutHelper = new FixLayoutHelper(10, 10);
adapters.add(new SubAdapter(this, layoutHelper, 0));
layoutHelper = new FixLayoutHelper(FixLayoutHelper.TOP_RIGHT, 20, 20);
adapters.add(new SubAdapter(this, layoutHelper, 1) {
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
LayoutParams layoutParams = new LayoutParams(200, 200);
holder.itemView.setLayoutParams(layoutParams);
}
});
}
if (STICKY_LAYOUT) {
StickyLayoutHelper layoutHelper = new StickyLayoutHelper(false);
adapters.add(new SubAdapter(this, layoutHelper, 0));
layoutHelper = new StickyLayoutHelper(false);
layoutHelper.setOffset(100);
adapters.add(new SubAdapter(this, layoutHelper, 1, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
}
if (GRID_LAYOUT) {
GridLayoutHelper layoutHelper = new GridLayoutHelper(2);
layoutHelper.setMargin(7, 0, 7, 0);
layoutHelper.setWeights(new float[] { 46.665f });
layoutHelper.setHGap(3);
adapters.add(new SubAdapter(this, layoutHelper, 2));
layoutHelper = new GridLayoutHelper(4);
layoutHelper.setWeights(new float[] { 20f, 26.665f });
layoutHelper.setMargin(7, 0, 7, 0);
layoutHelper.setHGap(3);
adapters.add(new SubAdapter(this, layoutHelper, 8));
}
if (GRID_LAYOUT) {
adapters.add(new SubAdapter(this, new GridLayoutHelper(4), 0));
GridLayoutHelper helper = new GridLayoutHelper(4);
helper.setAspectRatio(4f);
//helper.setColWeights(new float[]{40, 20, 30, 30});
// helper.setMargin(0, 10, 0, 10);
helper.setGap(10);
adapters.add(new SubAdapter(this, helper, 80) {
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
LayoutParams lp = (LayoutParams) holder.itemView.getLayoutParams();
// lp.bottomMargin = 1;
// lp.rightMargin = 1;
}
});
}
if (FIX_LAYOUT) {
adapters.add(new SubAdapter(this, new ScrollFixLayoutHelper(20, 20), 1) {
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
LayoutParams layoutParams = new LayoutParams(200, 200);
holder.itemView.setLayoutParams(layoutParams);
}
});
}
if (LINEAR_LAYOUT)
adapters.add(new SubAdapter(this, new LinearLayoutHelper(), 10));
if (GRID_LAYOUT) {
GridLayoutHelper helper = new GridLayoutHelper(3);
helper.setMargin(0, 10, 0, 10);
adapters.add(new SubAdapter(this, helper, 3));
}
if (STAGGER_LAYOUT) {
// adapters.add(new SubAdapter(this, new StaggeredGridLayoutHelper(2, 0), 0));
final StaggeredGridLayoutHelper helper = new StaggeredGridLayoutHelper(2, 10);
helper.setMargin(20, 10, 10, 10);
helper.setPadding(10, 10, 20, 10);
helper.setBgColor(0xFF86345A);
adapters.add(new SubAdapter(this, helper, 27) {
@Override
public void onBindViewHolder(MainViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
if (position % 2 == 0) {
layoutParams.mAspectRatio = 1.0f;
} else {
layoutParams.height = 340 + position % 7 * 20;
}
holder.itemView.setLayoutParams(layoutParams);
}
});
}
if (COLUMN_LAYOUT) {
// adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 3));
}
if (GRID_LAYOUT) {
// adapters.add(new SubAdapter(this, new GridLayoutHelper(4), 24));
}
delegateAdapter.setAdapters(adapters);
final Handler mainHandler = new Handler(Looper.getMainLooper());
trigger = new Runnable() {
@Override
public void run() {
// recyclerView.scrollToPosition(22);
// recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.requestLayout();
// mainHandler.postDelayed(trigger, 1000);
}
};
mainHandler.postDelayed(trigger, 1000);
}
use of com.alibaba.android.vlayout.layout.ColumnLayoutHelper in project CustomViews by AndroidStudy233.
the class VLayoutActivity method initUI.
private void initUI() {
// 创建VirtualLayoutManager对象
VirtualLayoutManager manager = new VirtualLayoutManager(this);
recyclerView.setLayoutManager(manager);
// 设置组件复用回收池
RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
recyclerView.setRecycledViewPool(viewPool);
viewPool.setMaxRecycledViews(0, 10);
// 添加divider
RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = ((VirtualLayoutManager.LayoutParams) view.getLayoutParams()).getViewPosition();
outRect.set(1, 1, 1, 4);
}
};
recyclerView.addItemDecoration(itemDecoration);
// 设置适配器, 参数2 当true的时候,不论是不是属于同一个子adapter,相同类型的item都能复用。
// 表示它们共享一个类型。 当false的时候,不同子adapter之间的类型不共享
DelegateAdapter delegateAdapter = new DelegateAdapter(manager, true);
recyclerView.setAdapter(delegateAdapter);
// 下面再让delegateAdapter添加所有的adapter, 就可以实现不同的类型的条目了
final List<DelegateAdapter.Adapter> adapters = new LinkedList<>();
// 第一个0 使用LinearLayoutHelper实现ViewPager(img)
VLayout1thAdapter adapter1th = new VLayout1thAdapter(this, new LinearLayoutHelper(), 1);
adapters.add(adapter1th);
// 第二个1 浮动的一个正方形
FloatLayoutHelper layoutHelper2th = new FloatLayoutHelper();
layoutHelper2th.setAlignType(FixLayoutHelper.TOP_RIGHT);
// 设置默认位置
layoutHelper2th.setDefaultLocation(100, 400);
VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(200, 200);
SimpleVLayoutAdapter adapter2th = new SimpleVLayoutAdapter(this, layoutHelper2th, 1, layoutParams);
adapters.add(adapter2th);
// 第三个2 就是一个简单的灰色条目
LinearLayoutHelper layoutHelper3th = new LinearLayoutHelper();
// 不通过params设置高度, 而通过宽高比实现 宽:高=2
layoutHelper3th.setAspectRatio(2.0f);
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper3th, 1));
// 第四个3-8 6个条目, 位置偶数的高度大一点
LinearLayoutHelper layoutHelper4th = new LinearLayoutHelper();
layoutHelper4th.setAspectRatio(2.0f);
layoutHelper4th.setDividerHeight(10);
layoutHelper4th.setMargin(0, 30, 0, 10);
layoutHelper4th.setPadding(10, 30, 10, 10);
layoutHelper4th.setBgColor(0xFFA9FFF8);
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper4th, 6) {
@Override
public void onBindViewHolder(VLayoutViewHolder holder, int position) {
/*
这里主要提一个AspectRatio的优先级问题, LayoutHelper的AspectRatio是要低于
layoutParams的AspectRatio的, 因此上面设置的宽高比2就无效了
另外这里的position指的是SimpleVLayoutAdapter下的条目item, 是从0开始的
别和总共的Recycler弄混了
*/
VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(VirtualLayoutManager.LayoutParams.MATCH_PARENT, 300);
if (position % 2 == 0) {
layoutParams.mAspectRatio = 3;
} else {
layoutParams.mAspectRatio = 5;
}
holder.itemView.setLayoutParams(layoutParams);
}
});
// 第五个9 这个牛逼了, 就是头部粘性效果
StickyLayoutHelper layoutHelper5th = new StickyLayoutHelper();
// 距离头部多少高度粘性
layoutHelper5th.setOffset(1);
layoutHelper5th.setAspectRatio(4);
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper5th, 1));
// 第六个10-14 一行多个, column
ColumnLayoutHelper layoutHelper6th = new ColumnLayoutHelper();
layoutHelper6th.setBgColor(0xff00f0f0);
layoutHelper6th.setWeights(new float[] { 10, 10, 20, 20, 40 });
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper6th, 5) {
@Override
public void onBindViewHolder(VLayoutViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
/*
同样的这里的weight优先级要低于LayoutParams的AspectRatio
如果设置LayoutParams的高度, 然后设置LayoutParams的AspectRatio, 还是会根据高度重新计算宽度
*/
}
});
// 第七个15 底部粘性效果 构造传个false
StickyLayoutHelper layoutHelper7th = new StickyLayoutHelper(false);
layoutHelper7th.setOffset(100);
VirtualLayoutManager.LayoutParams layoutParams1 = new VirtualLayoutManager.LayoutParams(VirtualLayoutManager.LayoutParams.MATCH_PARENT, 200);
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper7th, 1, layoutParams1));
// 第八个固定布局16
FixLayoutHelper layoutHelper8th = new FixLayoutHelper(FixLayoutHelper.TOP_LEFT, 100, 100);
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper8th, 1) {
@Override
public void onBindViewHolder(VLayoutViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(200, 200);
holder.itemView.setLayoutParams(layoutParams);
}
});
// 第九个17-18 1拖N小试
OnePlusNLayoutHelper layoutHelper9th = new OnePlusNLayoutHelper();
layoutHelper9th.setBgColor(0xffE551DC);
layoutHelper9th.setAspectRatio(4.0f);
// 2列, 分别占20% 40%
layoutHelper9th.setColWeights(new float[] { 20, 40 });
layoutHelper9th.setMargin(20, 20, 20, 20);
// layoutHelper7th.setPadding(10, 10, 10, 10);
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper9th, 2));
// 第十个19-22 继续1拖N 这种布局数量只能有1-5 而且怎么排是里面写好的
OnePlusNLayoutHelper layoutHelper10th = new OnePlusNLayoutHelper();
layoutHelper10th.setBgColor(0xff635EF8);
// 宽高比2:1
layoutHelper10th.setAspectRatio(2.0f);
// 第一行占30%高度
layoutHelper10th.setRowWeight(30f);
// 第一列30%, 剩下的占满
layoutHelper10th.setColWeights(new float[] { 30f });
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper10th, 4) {
@Override
public void onBindViewHolder(VLayoutViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
if (position == 0) {
lp.rightMargin = 1;
} else if (position == 1) {
} else if (position == 2) {
lp.topMargin = 1;
lp.rightMargin = 1;
}
}
});
// 第十一个23-50 瀑布流2列, 条目间距10
final StaggeredGridLayoutHelper layoutHelper11th = new StaggeredGridLayoutHelper(2, 10);
layoutHelper11th.setBgColor(0xFF86345A);
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper11th, 28) {
@Override
public void onBindViewHolder(VLayoutViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
if (position % 2 == 0) {
// 偶数位置正方形
layoutParams.mAspectRatio = 1.0f;
} else {
// 奇数位置最低高度300
layoutParams.height = 300 + position % 7 * 30;
}
holder.itemView.setLayoutParams(layoutParams);
}
});
// 第十二个51-70 Grid布局, 2列
GridLayoutHelper layoutHelper12th = new GridLayoutHelper(2);
layoutHelper12th.setAspectRatio(2f);
layoutHelper12th.setMargin(0, 0, 0, 0);
// 第一列占50%
layoutHelper12th.setWeights(new float[] { 50 });
// setVGap竖直 setHGap水平间距
layoutHelper12th.setGap(5);
adapters.add(new SimpleVLayoutAdapter(this, layoutHelper12th, 20));
// 最后将所有的adapter设置给delegate
delegateAdapter.setAdapters(adapters);
}
Aggregations