使用Scrollview和LinearLayout動態添加布局

j025xjs 8年前發布 | 30K 次閱讀 Android開發 移動開發 ScrollView

  • 使用Scrollview 和LinearLayout動態添加布局

  • 焦點位置不變,列表實現滾動

1. 放置ScrollView的布局文件,LinearLayout里的 paddingBottom 和 paddingTop 是為了在顯示的列表的頂部和底部留下空白,可根據需要調整其布局;

<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></code></pre> 

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));
                }
            });

 

來自:http://www.jianshu.com/p/fe8a0b083297

 

 本文由用戶 j025xjs 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!