使用Scrollview和LinearLayout动态添加布局

作者: 李世德Str | 来源:发表于2016-11-02 13:21 被阅读0次
    • 使用Scrollview 和LinearLayout动态添加布局
    • 焦点位置不变,列表实现滚动

    1. 放置ScrollView的布局文件,LinearLayout里的paddingBottompaddingTop 是为了在显示的列表的顶部和底部留下空白,可根据需要调整其布局;

        <ScrollView
            android:id="@+id/sv_channel"
            android:layout_width="200dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:fillViewport="false"
            android:scrollbars="none">
        
            <LinearLayout
                android:id="@+id/ll_channel_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:orientation="vertical"
                android:paddingBottom="210dp"
                android:paddingTop="200dp">
        
            </LinearLayout>
        </ScrollView>
    

    2. Java代码,动态生成ScrollView中的布局,这个添加里一个linearLayout,里面包含一个ImageView和一个TextView,设置它们的属性,最后处理LinearLayout的点击事件;

        private LinearLayout llChannelList;
        private ScrollView svChannel;
        private int lastSelectIndex;
        
        /**
         * 动态生成的布局
         */
        llChannelList = (LinearLayout) this.findViewById(R.id.ll_channel_list);
        svChannel = (ScrollView) this.findViewById(R.id.sv_channel);
        
        lastSelectIndex = channelPos;
        
        for (int i = 0; i < ConstUtils.channelNames.length; i++) {
            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setPadding(10, 10, 10, 10);
            linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
            if (i == lastSelectIndex) {
                linearLayout.setBackgroundResource(R.drawable.bg_list_selected);
                svChannel.post(new Runnable() {
                    @Override
                    public void run() {
                        svChannel.smoothScrollTo(0, lastSelectIndex * ScreenUtil.dp2px(context, 200));
                    }
                });
            }
    
            ImageView imageView = new ImageView(this);
            int imageWidth = ScreenUtil.dp2px(context, 140);
            int imageHeight = ScreenUtil.dp2px(context, 140);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageHeight));
            imageView.setPadding(10, 10, 10, 10);
            imageView.setImageResource(ConstUtils.channelImgWhite[i]);
        
            TextView textView = new TextView(this);
            textView.setTextSize(30);
            textView.setTextColor(Color.WHITE);
            textView.setGravity(Gravity.CENTER_HORIZONTAL);
            textView.setText(ConstUtils.channelNames[i]);
        
            linearLayout.addView(imageView);
            linearLayout.addView(textView);
            llChannelList.addView(linearLayout);
        
            final int pos = i;
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClickAction(pos);
                }
            });
        }
        
        /**
         * 处理动态布局每一条linearLayout的点击事件
         *
         * @param pos 点击位置
         */
        private void itemClickAction(int pos) {
            svChannel.smoothScrollTo(0, pos * ScreenUtil.dp2px(context, 200));
            ((LinearLayout) llChannelList.getChildAt(lastSelectIndex)).setBackgroundColor(Color.TRANSPARENT);
            ((LinearLayout) llChannelList.getChildAt(pos)).setBackgroundResource(R.drawable.bg_list_selected);
            lastSelectIndex = pos;
        
            channelPos = pos;
        
        // do something
        }
    

    附:

    1. 静态常量值

    public static final int[] channelImgWhite = {R.drawable.ic_white_movies, R.drawable.ic_white_funny,
            R.drawable.ic_white_random, R.drawable.ic_white_foods,
            R.drawable.ic_white_cartoon, R.drawable.ic_white_lady, R.drawable.ic_white_car};
    public static final String[] channelNames = {"频道名称1", "频道名称2", "频道名称3", "频道名称4",
            "频道名称5", "频道名称6", "频道名称7"};
    

    2. ScreenUtil屏幕大小及单位计算的工具类,见 ScreenUtil 屏幕大小及单位计算的工具类

    3. channelPos是本人项目中使用的变量,想要选定第几条就赋相应的值;

    4. R.drawable.bg_list_selected 是一张Item被选定时的背景图片,替换为所需资源即可;


    注意:

    在第一次进入时,直接使用smoothScrollTo( ); 无法直接滚到所期望的位置,使用如下代码即可解决。

        svChannel.post(new Runnable() {
                    @Override
                    public void run() {
                        svChannel.smoothScrollTo(0, lastSelectIndex * ScreenUtil.dp2px(context, 200));
                    }
                });
    

    相关文章

      网友评论

        本文标题:使用Scrollview和LinearLayout动态添加布局

        本文链接:https://www.haomeiwen.com/subject/yzevuttx.html