依赖
布局—设置FlexboxLayout属性
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.flexbox.FlexboxLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/flexboxLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="15dp"
android:paddingRight="15dp"
app:flexWrap="wrap"
app:justifyContent="space_around"></com.google.android.flexbox.FlexboxLayout>
</android.support.v4.widget.NestedScrollView>
for (int i = 0; i < list.size(); i++) {
final TextView textView = new TextView(getContext());
textView.setText(list.get(i));
textView.setTextSize(16);
textView.setTextColor(Color.WHITE);
textView.setPadding(dp12, dp9, dp12, dp9);
Drawable pressed = DrawableUtil.generateDrawable(dp20);
Drawable normal = DrawableUtil.generateDrawable(dp20);
textView.setBackgroundDrawable(DrawableUtil.generateSelector(pressed, normal));
//设置margin
FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(FlexboxLayout.LayoutParams.WRAP_CONTENT
, FlexboxLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = dp15;
params.topMargin = dp15;
textView.setLayoutParams(params);
flexboxLayout.addView(textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtil.showToast(getContext(), textView.getText().toString());
}
});
}
NestedScrollView
//NestedScrollView和ScrollView相比,是能够兼容协调布局的ScrollView,是design包里面的类
NestedScrollView scrollView = new NestedScrollView(getContext());
flowLayout = new FlowLayout(getContext());
int padding = getResources().getDimensionPixelSize(R.dimen.dp15);
//设置padding值
flowLayout.setPadding(padding, padding, padding, padding);
//将recycleview添加到scrollview中
scrollView.addView(flowLayout);
跟着动画滚动
//监听动画值的变化
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
//设置给控件的高度
ViewGroup.LayoutParams params = tvDes.getLayoutParams();
params.height = animatedValue;
tvDes.setLayoutParams(params);
//如果当前是展开动画,则需要让ScrollView进行滚动
//参1--x方向滚动 参2y方向滚动--getMaxScrollAmount()最大滚动距离
if (isOpen) {
scrollView.scrollTo(0, scrollView.getMaxScrollAmount());
}
}
});