使用Scrollview和LinearLayout动态添加布局

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

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,250评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,822评论 4 61
  • 黑夜的静谧开启了无声的喧嚣 思维像乘着风的风筝争相涌入无边黑暗的天际 将我不断的抽离 抽离 无边的黑暗 无边的霓...
    空云儿阅读 2,688评论 0 1
  • 悠悠我心,情已至今。 亢亢浩涯,谁为我念? 沉沉如梦,梦中经过。 似似重来,回者写梦
    花念凡阅读 1,139评论 0 0

友情链接更多精彩内容