使用Scrollview和LinearLayout动态添加布局

j025xjs 7年前
   <ul>     <li> <p>使用Scrollview 和LinearLayout动态添加布局</p> </li>     <li> <p>焦点位置不变,列表实现滚动</p> </li>    </ul>    <p><strong>1. 放置ScrollView的布局文件,LinearLayout里的 paddingBottom 和 paddingTop 是为了在显示的列表的顶部和底部留下空白,可根据需要调整其布局;</strong></p>    <pre>  <code class="language-java"><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>    <p><strong>2. Java代码,动态生成ScrollView中的布局,这个添加里一个linearLayout,里面包含一个ImageView和一个TextView,设置它们的属性,最后处理LinearLayout的点击事件;</strong></p>    <pre>  <code class="language-java">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      }</code></pre>    <h2><strong>附:</strong></h2>    <p><strong>1. 静态常量值</strong></p>    <pre>  <code class="language-java">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"};</code></pre>    <p>2. ScreenUtil屏幕大小及单位计算的工具类,见 ScreenUtil 屏幕大小及单位计算的工具类</p>    <p>3. channelPos是本人项目中使用的变量,想要选定第几条就赋相应的值;</p>    <p>4. R.drawable.bg_list_selected 是一张Item被选定时的背景图片,替换为所需资源即可;</p>    <h2><strong>注意:</strong></h2>    <p>在第一次进入时,直接使用smoothScrollTo( ); 无法直接滚到所期望的位置,使用如下代码即可解决。</p>    <pre>  <code class="language-java">svChannel.post(new Runnable() {                  @Override                  public void run() {                      svChannel.smoothScrollTo(0, lastSelectIndex * ScreenUtil.dp2px(context, 200));                  }              });</code></pre>    <p> </p>    <p>来自:http://www.jianshu.com/p/fe8a0b083297</p>    <p> </p>