123.png
依賴
布局—設(shè)置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));
//設(shè)置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相比,是能夠兼容協(xié)調(diào)布局的ScrollView,是design包里面的類
NestedScrollView scrollView = new NestedScrollView(getContext());
flowLayout = new FlowLayout(getContext());
int padding = getResources().getDimensionPixelSize(R.dimen.dp15);
//設(shè)置padding值
flowLayout.setPadding(padding, padding, padding, padding);
//將recycleview添加到scrollview中
scrollView.addView(flowLayout);
跟著動(dòng)畫滾動(dòng)
//監(jiān)聽動(dòng)畫值的變化
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
//設(shè)置給控件的高度
ViewGroup.LayoutParams params = tvDes.getLayoutParams();
params.height = animatedValue;
tvDes.setLayoutParams(params);
//如果當(dāng)前是展開動(dòng)畫,則需要讓ScrollView進(jìn)行滾動(dòng)
//參1--x方向滾動(dòng) 參2y方向滾動(dòng)--getMaxScrollAmount()最大滾動(dòng)距離
if (isOpen) {
scrollView.scrollTo(0, scrollView.getMaxScrollAmount());
}
}
});