flexbox-Layout—热门界面+NestedScrollView

123.png

依赖

查看gethub

布局—设置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());
        }
    }
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,042评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,532评论 4 61
  • 在这个浮躁的、看不懂、摸不清的社会,越来越多的人处于迷茫不知所向期。 最近几天就有五六个朋友接连诉说“现在的工作不...
    小小白日梦阅读 3,508评论 0 0
  • 愁,愁,愁上眉梢头。 月隐叇云后――羞。 羞,事事落人后。
    随化阅读 1,535评论 0 0
  • 没有人会待在原地等你,我们都要继续
    懂你dongni阅读 1,328评论 0 0

友情链接更多精彩内容