网友通过本文主要向大家介绍了热门投资理财,我想要理财投资公司,我想投资理财,我要投资理财平台,我想要理财投资信息等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
我的投资3--热门理财,投资3--热门理财
”热门理财”的流式布局的实现
1.自定义流式布局
public class FlowLayout extends ViewGroup { public FlowLayout(Context context) { super(context); } public FlowLayout(Context context, AttributeSet attrs) { super(context, attrs); } public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); //记录当前布局的宽高 int width = 0; int height = 0; //记录一行视图的宽度、高度 int lineWidth = 0; int lineHeight = 0; //获取在at_most模式下的布局的宽度、高度 int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); measureChild(childView, widthMeasureSpec, heightMeasureSpec); int childWidth = childView.getMeasuredWidth(); int childHeight = childView.getMeasuredHeight(); MarginLayoutParams mp = ((MarginLayoutParams) childView.getLayoutParams()); //表示当前的子视图还可以放在一行上 if (lineWidth + childWidth + mp.leftMargin + mp.rightMargin < widthSize) { lineWidth += childWidth + mp.leftMargin + mp.rightMargin; lineHeight = Math.max(lineHeight, childHeight + mp.topMargin + mp.bottomMargin); } else {//换行 width = Math.max(width, lineWidth); height += lineHeight; //重置 lineWidth = childWidth + mp.leftMargin + mp.rightMargin; lineHeight = childHeight + mp.topMargin + mp.bottomMargin; } if (i == childCount - 1) { width = Math.max(width, lineWidth); height += lineHeight; } } Log.e("TAG", "width = " + width + ",height = " + height); setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width, heightMode == MeasureSpec.EXACTLY ? heightSize : height); } private List<List<View>> allViews = new ArrayList<List<View>>(); private List<Integer> allHeights = new ArrayList<>(); @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int width = getWidth(); int childCount = getChildCount(); int lineWidth = 0; int lineHeight = 0; List<View> lineViews = new ArrayList<>(); for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); int childWidth = childView.getMeasuredWidth(); int childHeight = childView.getMeasuredHeight(); MarginLayoutParams mp = (MarginLayoutParams) childView.getLayoutParams(); //不换行 if (lineWidth + childWidth + mp.leftMargin + mp.rightMargin < width) { lineWidth += childWidth + mp.leftMargin + mp.rightMargin; lineViews.add(childView); lineHeight = Math.max(lineHeight, childHeight + mp.topMargin + mp.bottomMargin); } else {//换行 allViews.add(lineViews); allHeights.add(lineHeight); lineViews = new ArrayList<>(); lineViews.add(childView); lineWidth = childWidth + mp.leftMargin + mp.rightMargin; lineHeight = childHeight + mp.topMargin + mp.bottomMargin; } if (i == childCount - 1) { allViews.add(lineViews); allHeights.add(lineHeight); } } Log.e("TAG", "allViews.size = " + allViews.size() + ",allHeights.size = " + allHeights.size()); //通过计算每一行的每一个子view的left,top,right,bottom,摆放每一行的每一个子view的位置 int x = 0; int y = 0; for (int i = 0; i < allViews.size(); i++) { int curLineHeight = allHeights.get(i); //当前行的所有子view List<View> views = allViews.get(i); for (View view : views) { int viewWidth = view.getMeasuredWidth(); int viewHeight = view.getMeasuredHeight(); MarginLayoutParams mp = (MarginLayoutParams) view.getLayoutParams(); int lc = x + mp.leftMargin; int tc = y + mp.topMargin; int rc = lc + viewWidth; int bc = tc + viewHeight; view.layout(lc, tc, rc, bc); x += viewWidth + mp.rightMargin + mp.leftMargin; } x = 0; y += curLineHeight; } } //有了如下的方法,在上面通过child就可以getLayoutParams() @Override public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { MarginLayoutParams mp = new MarginLayoutParams(getContext(), attrs); return mp; } }
2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.atguigu.p2p.ui.FlowLayout android:id="@+id/flow_hot" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_green_light"> </com.atguigu.p2p.ui.FlowLayout> </LinearLayout>
3.ProductHotFragment
public class ProductHotFragment extends BaseFragment { @Bind(R.id.flow_layout) FlowLayout flowLayout; //提供页面要显示的数据 private String[] datas = new String[]{"新手计划", "乐享活系列90天计划", "钱包", "30天理财计划(加息2%)", "林业局投资商业经营与大捞一笔", "中学老师购买车辆", "屌丝下海经商计划", "新西游影视拍", "Java培训老师自己周转", "HelloWorld", "C++-C-ObjectC-java", "Android vs ios", "算法与数据结构", "JNI与NDK", "team working"}; private Random random; @Override protected RequestParams getParams() { return null; } @Override protected String getUrl() { return null; } @Override protected void initData(String content) { random = new Random(); //1.动态的创建TextView for(int i = 0; i < datas.length; i++) { final TextView tv = new TextView(getActivity()); //设置TextView的属性 tv.setText(datas[i]); tv.setTextSize(UIUtils.dp2Px(10)); ViewGroup.MarginLayoutParams mp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); mp.leftMargin = UIUtils.dp2Px(8); mp.rightMargin = UIUtils.dp2Px(8); mp.topMargin = UIUtils.dp2Px(8); mp.bottomMargin = UIUtils.dp2Px(8); tv.setLayoutParams(mp); //设置textView的背景 int red = random.nextInt(211); int green = random.nextInt(211); int blue = random.nextInt(211); //测试一: // tv.setBackground(DrawUtils.getDrawable(Color.rgb(red,green,blue),UIUtils.dp2Px(5))); //测试二: tv.setBackground(DrawUtils.getSelector(DrawUtils.getDrawable(Color.rgb(re