/** * 设置新闻详情 * * @param index */ public void ShowNewsDetail(NewsDetailModel model) { this.model = model; if (model.isFavorate) { favoritBtn.setText("取消收藏"); } else { favoritBtn.setText("收藏"); } scrollView.scrollTo(0, 0); scrollView.smoothScrollTo(0, 0); LogInfo.LogOut("OnlineNewsAdapter", "ShowNews-->pos:" + pos); final NewsDetailModel news = model; mTitle.setText(news.title); mTimeProvenance.setText( (TextUtils.isEmpty(news.cFrom) ? "" : " 来源: " + news.cFrom) + "\t" + news.cTime); LogInfo.LogOut("字数:" + (news.content == null ? 0 : news.content.length())); mDetails.setText(""); postDelayed( new Runnable() { @Override public void run() { setChangeLine(news.content); } }, 50); if (TextUtils.isEmpty(news.imgUrl)) { mNewsImage.setVisibility(GONE); } else { mNewsImage.setVisibility(VISIBLE); ImageEngine.setImageBitmap(news.imgUrl, mNewsImage, R.drawable.ic_launcher, -1); } }
private void setChangeLine(String str) { TextPaint paint = mDetails.getPaint(); StringBuffer tmpbuffer = new StringBuffer(); StringBuffer buffer = new StringBuffer(); int detailwidth = mDetails.getWidth(); LogInfo.LogOut("mDetails.getWidth()=" + mDetails.getWidth()); if (str == null) { return; } char[] chars = str.toCharArray(); for (int i = 0; i < chars.length; i++) { tmpbuffer.append(chars[i]); if (chars[i] == '\n') { buffer.append(tmpbuffer.toString()); tmpbuffer = new StringBuffer(); } else if (paint.measureText(tmpbuffer.toString()) > detailwidth) { int tmp = i + 1; tmpbuffer.deleteCharAt(tmpbuffer.length() - 1); i--; while (tmp < chars.length) { if (chars[tmp] == ' ') { tmp++; } else if (chars[tmp] == '\n') { i = tmp; break; } else { break; } } buffer.append(tmpbuffer.toString() + "\n"); tmpbuffer = new StringBuffer(); } else if (i == chars.length - 1) { buffer.append(tmpbuffer.toString()); } } mDetails.setText(buffer.toString()); }