自定义view 加载更多

来源:互联网 发布:mac版flash cs5 编辑:程序博客网 时间:2024/05/16 12:11

在查阅其他博主的博文中,发现了一个比较不错的文本伸展的效果,在此借鉴学习。可以先看看到底是什么样的效果


看起来很眼熟吧,很多应用中都有这样的使用场景,其实就是控制textview的maxlines属性,来做的。在这里就简单的说下定义的过程

1.stretchy_text_layout.xml --这是创建一个布局,用来装裱以上展示的控件

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/content_textview"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginTop="4.0dip"  
  12.         android:ellipsize="end"  
  13.         android:gravity="center_vertical"  
  14.         android:textColor="#ff000000"  
  15.         android:textSize="14.0dip" />  
  16.   
  17.     <LinearLayout  
  18.         android:id="@+id/bottom_text_layout"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:gravity="right"  
  22.         android:orientation="horizontal" >  
  23.   
  24.         <TextView  
  25.             android:id="@+id/bottom_textview"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_marginRight="2dp"  
  29.             android:layout_marginTop="2dp"  
  30.             android:gravity="center"  
  31.             android:padding="2dp"  
  32.             android:singleLine="true"  
  33.             android:textColor="#ff576b95"  
  34.             android:textSize="15.0dip"  
  35.             android:visibility="visible" />  
  36.     </LinearLayout>  
  37. </LinearLayout>  
2.在string.xml中定义两个后面需要显示的内容

[html] view plain copy
  1. <string name="retract">收起</string>  
  2.     <string name="spread">展开</string>  
3.主要的代码StretchyTextView.java --继承线性布局linearlayout,对其中的控件做状态的预判,并更改content TextView的最大显示行数。做到我们想要的效果。


[java] view plain copy
  1. /** 
  2.  * 可伸展的文本显示布局 
  3.  * @author jan 
  4.  */  
  5. public class StretchyTextView extends LinearLayout implements OnClickListener {  
  6.     //默认显示的最大行数  
  7.     private static final int DEFAULT_MAX_LINE_COUNT = 2;  
  8.     //当前展开标志显示的状态  
  9.     private static final int SPREADTEXT_STATE_NONE = 0;  
  10.     private static final int SPREADTEXT_STATE_RETRACT = 1;  
  11.     private static final int SPREADTEXT_STATE_SPREAD = 2;  
  12.       
  13.     private TextView contentText;  
  14.     private TextView operateText;  
  15.     private LinearLayout bottomTextLayout;  
  16.   
  17.     private String shrinkup;  
  18.     private String spread;  
  19.     private int mState;  
  20.     private boolean flag = false;  
  21.     private int maxLineCount = DEFAULT_MAX_LINE_COUNT;  
  22.     private InnerRunnable runable;  
  23.   
  24.     public StretchyTextView(Context context) {  
  25.         this(context, null);  
  26.     }  
  27.   
  28.     public StretchyTextView(Context context, AttributeSet attrs) {  
  29.         super(context, attrs);  
  30.         shrinkup = context.getString(R.string.retract);  
  31.         spread = context.getString(R.string.spread);  
  32.         View view = inflate(context, R.layout.stretchy_text_layout, this);  
  33.         view.setPadding(0, -100);  
  34.         contentText = (TextView) view.findViewById(R.id.content_textview);  
  35.         operateText = (TextView) view.findViewById(R.id.bottom_textview);  
  36.         bottomTextLayout = (LinearLayout) view.findViewById(R.id.bottom_text_layout);  
  37.         setBottomTextGravity(Gravity.LEFT);  
  38.         operateText.setOnClickListener(this);  
  39.         runable = new InnerRunnable();  
  40.     }  
  41.   
  42.     @Override  
  43.     public void onClick(View v) {  
  44.         flag = false;  
  45.         requestLayout();  
  46.     }  
  47.   
  48.     public final void setContent(CharSequence charSequence) {  
  49.         contentText.setText(charSequence, BufferType.NORMAL);  
  50.         mState = SPREADTEXT_STATE_SPREAD;  
  51.         Log.d("setContent""count lines="+contentText.getLineCount()+",flag="+flag);  
  52.         flag = false;  
  53.         requestLayout();  
  54.     }  
  55.   
  56.     @SuppressLint("DrawAllocation")  
  57.     @Override  
  58.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  59.         super.onLayout(changed, l, t, r, b);  
  60.         if (!flag) {  
  61.             flag = !flag;  
  62.             if (contentText.getLineCount() <= DEFAULT_MAX_LINE_COUNT) {  
  63.                 mState = SPREADTEXT_STATE_NONE;  
  64.                 operateText.setVisibility(View.GONE);  
  65.                 contentText.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1);  
  66.             }else {  
  67.                 post(runable);  
  68.             }  
  69.         }  
  70.     }  
  71.   
  72.     class InnerRunnable implements Runnable {  
  73.         @Override  
  74.         public void run() {  
  75.             if (mState == SPREADTEXT_STATE_SPREAD) {  
  76.                 contentText.setMaxLines(maxLineCount);  
  77.                 operateText.setVisibility(View.VISIBLE);  
  78.                 operateText.setText(spread);  
  79.                 mState = SPREADTEXT_STATE_RETRACT;  
  80.             } else if (mState == SPREADTEXT_STATE_RETRACT) {  
  81.                 contentText.setMaxLines(Integer.MAX_VALUE);  
  82.                 operateText.setVisibility(View.VISIBLE);  
  83.                 operateText.setText(shrinkup);  
  84.                 mState = SPREADTEXT_STATE_SPREAD;  
  85.             }  
  86.         }  
  87.     }  
  88.   
  89.     public void setMaxLineCount(int maxLineCount) {  
  90.         this.maxLineCount = maxLineCount;  
  91.     }  
  92.       
  93.     public void setContentTextColor(int color){  
  94.         this.contentText.setTextColor(color);  
  95.     }  
  96.       
  97.     public void setContentTextSize(float size){  
  98.         this.contentText.setTextSize(size);  
  99.     }  
  100.     /** 
  101.      * 内容字体加粗 
  102.      */  
  103.     public void setContentTextBold(){  
  104.         TextPaint textPaint = contentText.getPaint();  
  105.         textPaint.setFakeBoldText(true);  
  106.     }  
  107.     /** 
  108.      * 设置展开标识的显示位置 
  109.      * @param gravity 
  110.      */  
  111.     public void setBottomTextGravity(int gravity){  
  112.         bottomTextLayout.setGravity(gravity);  
  113.     }  
  114. }  

4.使用

在其它需要的布局中引入 自定义的layout

[html] view plain copy
  1. <org.jan.okhttp.demo.StretchyTextView  
  2.             android:id="@+id/spread_textview"  
  3.             android:layout_width="match_parent"  
  4.             android:layout_height="wrap_content" />  

然后在代码中find出来,设置内容。

[java] view plain copy
  1. spreadTextView = (StretchyTextView) findViewById(R.id.spread_textview);  
  2.         spreadTextView.setMaxLineCount(3);  
  3.         spreadTextView.setContent("近些年来,越来越多的行业开始和互联网结合,诞生了越来越多的互联网创业公司。互联网创业公司需要面对许多的不确定因素。如果你和你的小伙伴们够幸运,你们的公司可能会在几个星期之内让用户数、商品数、订单量增长几十倍上百倍。一次促销可能会带来平时几十倍的访问流量,一次秒杀活动可能会吸引平时数百倍的访问用户。这对公司自然是极大的好事,说明产品得到认可,公司未来前景美妙。");