android 组件更新内容之后的刷新小结

来源:互联网 发布:淘宝手表带价格及图片 编辑:程序博客网 时间:2024/06/07 22:09

在android开发过程中,当我们对一个TextView更新内容之后,并且会改变界面布局的高度的时候。比如:

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/author_lay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tab_item_bg"
        android:paddingBottom="5dip"
        >

        <TextView
            android:id="@+id/author_name_tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_marginTop="5dip"
            android:singleLine="true"
            android:textColor="@color/item_name"
            android:textSize="16dip" />

        <TextView
            android:id="@+id/author_des_tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/author_name_tv"
            android:layout_margin="5dip"
            android:textColor="@color/black"
            android:textSize="16dip" />
    </RelativeLayout>
    <ImageView
        android:id="@+id/author_line"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/line" />
    <ListView
        android:id="@+id/author_works_lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:cacheColorHint="@color/transparent"
        android:divider="@drawable/line"
        android:fadeScrollbars="true"
        android:fadingEdge="none"
        android:scrollbarThumbVertical="@drawable/scroll" />
</LinearLayout>

 

2. 关键代码

TextView authorDesTV;

authorDesTV = (TextView) findViewById(R.id.author_des_tv);

  authorDesTV.setText(getString(R.string.author_description, authorDes));

 //设置Tag,根据Tag的值来判断authorDesTV是否展开
  authorDesTV.setTag(false);
  authorDesTV.setMaxLines(4);

1)通过view的requestLayout()方法来刷新界面
  authorDesTV.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
          boolean isExpend = (Boolean) authorDesTV.getTag();
                 if (isExpend) {     
                       authorDesTV.setTag(false);
                       authorDesTV.setMaxLines(4); 
                       authorDesTV.requestLayout();
               } else {
                     authorDesTV.setTag(true);
                     authorDesTV.setMaxLines(20);
                     authorDesTV.setEllipsize(null);
             }    
         }
  });
 

2)通过view的invalidate()方法来刷新界面
  authorDesTV.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
          boolean isExpend = (Boolean) authorDesTV.getTag();
                 if (isExpend) {     
                       authorDesTV.setTag(false);
                       authorDesTV.setMaxLines(4); 
                       authorDesTV.invalidate();
              } else {
                     authorDesTV.setTag(true);
                     authorDesTV.setMaxLines(20);
                     authorDesTV.setEllipsize(null);
             }    
         }
  });

3)通过ListView的scrollBy()方法来刷新界面
  authorDesTV.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
          boolean isExpend = (Boolean) authorDesTV.getTag();
                 if (isExpend) {     
                       authorDesTV.setTag(false);
                       authorDesTV.setMaxLines(4); 
      
                       authorWorksLV.scrollBy(0, 1); 
               } else {
                     authorDesTV.setTag(true);
                     authorDesTV.setMaxLines(20);
                     authorDesTV.setEllipsize(null);
             }    
         }
  });

3.第一种方式是通过重新布局来请求界面刷新;

第二种方式是view的刷新方法;

第三种方式是间接通过重新布局的方式来请求界面刷新。

4.最后如果通过以上三种方式均不能够实现界面刷新,那就要考虑尝试以下方式:

1)对RelativeLayout添加一个android:clipChildren属性;

   <RelativeLayout
        android:id="@+id/author_lay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tab_item_bg_hover"
        android:paddingBottom="5dip"
        android:clipChildren="true"

      >

2)对listview添加一个背景android:background="@drawable/window_bg"。

 <ListView
        android:id="@+id/author_works_lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/window_bg"
        android:cacheColorHint="@color/transparent"
        android:divider="@drawable/line"
        android:fadeScrollbars="true"
        android:fadingEdge="none"
        android:scrollbarThumbVertical="@drawable/scroll" />

 

原创粉丝点击