flexbox-Layout—熱門界面+NestedScrollView

123.png

依賴

查看gethub

布局—設(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());
        }
    }
});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,251評論 4 61
  • 在這個(gè)浮躁的、看不懂、摸不清的社會(huì),越來越多的人處于迷茫不知所向期。 最近幾天就有五六個(gè)朋友接連訴說“現(xiàn)在的工作不...
    小小白日夢閱讀 550評論 0 0
  • 愁,愁,愁上眉梢頭。 月隱叇云后――羞。 羞,事事落人后。
    隨化閱讀 214評論 0 0
  • 沒有人會(huì)待在原地等你,我們都要繼續(xù)
    懂你dongni閱讀 153評論 0 0