可展开的TextView

 
 
public class ExpandableTextView extends LinearLayout implements OnClickListener {

	private static final int DEFAULT_MAX_LINE_COUNT = 3;
	private QTextView mDescriptionTitleView;
	private QTextView mDescriptionContentView;
	private QTextView mMoreInfoView;
	private static final String TAG = "ExpandableTextView";
	private InnerRunnable mRunnable;
	private boolean mFlag;
	private PluginResUtil mRes;


    private static final int COLLAPSIBLE_STATE_NONE = 0;
    
    /**
     * 收起状态
     */
    private static final int COLLAPSIBLE_STATE_SHRINKUP = 1;
    
    /**
     * 展开状态
     */
    private static final int COLLAPSIBLE_STATE_EXPAND = 2;
    private int mState;

	public ExpandableTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mRes= PluginResUtil.getInstance();
		View view=(LinearLayout) mRes.inflate(context, R.layout.layout_expandable_textview, this);
		view.setPadding(0, -1, 0, 0);
		mDescriptionTitleView = (QTextView) mRes.findView(view, R.id.description_title);
		mDescriptionContentView = (QTextView) mRes.findView(view, R.id.description_content);
		//mComplainView = (QTextView) mRes.findView(view, R.id.user_complain_report);
		mMoreInfoView = (QTextView) mRes.findView(view, R.id.more_info);
		mMoreInfoView.getPaint().setAntiAlias(true);
		mMoreInfoView.setOnClickListener(this);
		mRunnable = new InnerRunnable();

	}

	public ExpandableTextView(Context context) {
		this(context, null);
	}
	
	public void setTitle(CharSequence charSequence) {
		if (TextUtils.isEmpty(charSequence)) {
			mDescriptionTitleView.setVisibility(View.GONE);
			return ;
		}
		mDescriptionTitleView.setText(charSequence);
	}

	public void setContent(CharSequence charSequence) {
		if (TextUtils.isEmpty(charSequence)) {
			mDescriptionContentView.setVisibility(View.GONE);
			return ;
		}
		mDescriptionContentView.setText(charSequence, BufferType.NORMAL);
		mState = COLLAPSIBLE_STATE_EXPAND; 
		requestLayout();
	}
	
	

	public void setTextColor(int color) {
		mDescriptionContentView.setTextColor(color);
	}

	@Override
	public void onClick(View v) {
		mFlag = false;
		mDescriptionContentView.setEllipsize(null);
		requestLayout();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (!mFlag) {
			mFlag = true;
			int lineCount = mDescriptionContentView.getLineCount();
			if (lineCount <= DEFAULT_MAX_LINE_COUNT) {
				mState = COLLAPSIBLE_STATE_NONE;
				mDescriptionContentView.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1);
				// setMovementMethod会使TextView滚动,移动到展开后去设置。
				mDescriptionContentView.setMovementMethod(LinkMovementMethod.getInstance());
				mDescriptionContentView.setVisibility(View.VISIBLE);
				mMoreInfoView.setVisibility(View.GONE);	
			} else {
				post(mRunnable);
			}
		}
	}

	class InnerRunnable implements Runnable {
		@Override
		public void run() {
	            if (mState == COLLAPSIBLE_STATE_EXPAND) {
	            	mDescriptionContentView.setVisibility(View.VISIBLE);
	            	mDescriptionContentView.setMaxLines(DEFAULT_MAX_LINE_COUNT);
	            	mDescriptionContentView.setEllipsize(null);
	            	mMoreInfoView.setVisibility(View.VISIBLE);
	                mState = COLLAPSIBLE_STATE_SHRINKUP;
	            } else if (mState == COLLAPSIBLE_STATE_SHRINKUP) {
	            	mDescriptionContentView.setVisibility(View.VISIBLE);
	            	// setMovementMethod会使TextView滚动,移动到展开后去设置。
	            	mDescriptionContentView.setMovementMethod(LinkMovementMethod.getInstance());
	            	mDescriptionContentView.setMaxLines(Integer.MAX_VALUE);
	            	mDescriptionContentView.setEllipsize(null);
	            	mMoreInfoView.setVisibility(View.GONE);
	                mState = COLLAPSIBLE_STATE_EXPAND;
	            }
		}
	}
}



你可能感兴趣的:(可展开的TextView)