textview对单词设置点击事件,内容过多时卡顿

nickDeLove 2014-10-13 08:32:32
加精
textview对单词设置点击事件 内容过多时滑动,滑动停止卡顿,有人遇到过吗,如何解决卡顿的问题,或者有其他更好的方案吗





public class MainActivity extends Activity implements
SwipeRefreshLayout.OnRefreshListener {

TextView tv, tv2;
SwipeRefreshLayout swipeLayout;
ScrollView scllScrollView;
String content = "My " +
" father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment.My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment."
+" father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment.My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment."
+" father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment.My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment."
+" father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play a instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there wang the day of his audition and he did not want to be around that type of environment."
+ "Occasionally, Dad would get out his mandolin and play for the family. We three children: Trisha, Monte and I, George Jr. few times. I loved to sing, but I never learned how to play the mandolin. This is something I regret to this day.";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scllScrollView = (ScrollView) findViewById(R.id.scll);
tv = (TextView) this.findViewById(R.id.tv);

SpannableStringBuilder ssb = new SpannableStringBuilder(content);
tv.setText(ssb, BufferType.SPANNABLE);
getEachWord(tv);
tv.setMovementMethod(LinkMovementMethod.getInstance());

}

public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}

PopupWindow popupWindow;

@SuppressWarnings("deprecation")
private void showPopUp(View v, int x, int y, String str) {
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.GRAY);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setText("I'm a pop --" + str);
tv.setTextColor(Color.WHITE);
layout.addView(tv);

popupWindow = new PopupWindow(layout, 120, 120);

popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());

int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, x, y);
}

/**
* 点击响应方法
*
* @param textView
*/
public void getEachWord(TextView textView) {
Log.i("main", "getEachWord .....");
Spannable spans = (Spannable) textView.getText();
Integer[] indices = getIndices(textView.getText().toString().trim()
+ " ", ' ');

// // 单词开始点
int start = 0;
// 单词结束点
int end = 0;
for (int i = 0; i < indices.length; i++) {
end = (i < indices.length ? indices[i] : spans.length());
// Log.i(tag, "start:" + start + " end:" + end);
ClickableSpan clickSpan = getClickableSpan(spans, start, end,
textView.getText().toString().length());
// to cater last/only word
spans.setSpan(clickSpan, start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = end + 1;
}
// 改变选中文本的高亮颜
textView.setHighlightColor(Color.TRANSPARENT);
}

String tag = "wydemo";

private ClickableSpan getClickableSpan(final Spannable spans,
final int start, final int end, final int total) {
return new ClickableSpan() {
@Override
public void onClick(View widget) {

// TextView tv = (TextView) widget;
// String s = tv
// .getText()
// .subSequence(tv.getSelectionStart(),
// tv.getSelectionEnd()).toString();
//
// spans.setSpan(new ForegroundColorSpan(Color.BLACK), 0, total,
// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// tv.setTextColor(getResources().getColor(android.R.color.black));
// spans.setSpan(new ForegroundColorSpan(Color.RED), start, end,
// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Layout layout = tv.getLayout();
// int linenumber = layout.getLineForOffset(start);
// Rect rect = new Rect();
// layout.getLineBounds(linenumber, rect);
// float x =
// layout.getPrimaryHorizontal(tv.getSelectionStart());
// Log.i(tag,
// "ClickableSpan linenumber:"+linenumber+" start:"+start+" end:"+end+" s");
// showPopUp(tv, (int) x,
// rect.bottom - scllScrollView.getScrollY(), s);
}

@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.BLACK);
ds.setUnderlineText(false);
}
};
}

class CustomizedClickable extends ClickableSpan {

/*
* (non-Javadoc)
*
* @see android.text.style.ClickableSpan#onClick(android.view.View)
*/
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "come on success",
Toast.LENGTH_SHORT).show();
}

@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.GREEN);
ds.setAlpha(50);
Log.i(tag, "updateDrawState");
}

}

public static Integer[] getIndices(String s, char c) {
int pos = s.indexOf(c, 0);
List<Integer> indices = new ArrayList<Integer>();
while (pos != -1) {
indices.add(pos);
pos = s.indexOf(c, pos + 1);
}
return (Integer[]) indices.toArray(new Integer[0]);
}

}


textview对单词设置点击事件 内容过多时滑动,滑动停止卡顿,有人遇到过吗,如何解决卡顿的问题,或者有其他更好的方案吗
...全文
1338 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
xrc906522694 2014-11-14
  • 打赏
  • 举报
回复
小时候老师教算数,要么是数棒棒,要么是用算盘,后来有个有钱的孩子买了台计算机,能算加减乘除,我感觉好高端,后来长大做了收银员,感觉计算机就是手速要快,没啥难的。。
wangdan092469 2014-11-04
  • 打赏
  • 举报
回复
学习了,谢谢
_周星星 2014-11-03
  • 打赏
  • 举报
回复
能否内容用html 换成webview显示了
qq_20733459 2014-10-30
  • 打赏
  • 举报
回复
学习收藏了,谢谢分享
u010009545 2014-10-28
  • 打赏
  • 举报
回复
bianholmes 2014-10-27
  • 打赏
  • 举报
回复
写的不错,赞一个~
zhoupeng61766300 2014-10-27
  • 打赏
  • 举报
回复
很正确,YES
q3695361789 2014-10-27
  • 打赏
  • 举报
回复
进来学习一下。
yd709299705 2014-10-27
  • 打赏
  • 举报
回复
hugh_z 2014-10-27
  • 打赏
  • 举报
回复
leanring
  • 打赏
  • 举报
回复
SuperCoderJz 2014-10-27
  • 打赏
  • 举报
回复
进来看看大神们的代码
lincematejava 2014-10-27
  • 打赏
  • 举报
回复
好厉害的代码阿
long096364 2014-10-27
  • 打赏
  • 举报
回复
学习中
lxccam 2014-10-26
  • 打赏
  • 举报
回复
刚接触 学习下
hugh_z 2014-10-26
  • 打赏
  • 举报
回复
learning
风过竹静 2014-10-26
  • 打赏
  • 举报
回复
虽然没看懂,进来学习下
WCTNOC 2014-10-26
  • 打赏
  • 举报
回复
好帖啊.........
PLL_VCO 2014-10-25
  • 打赏
  • 举报
回复
哦,好厉害
GW786228836 2014-10-25
  • 打赏
  • 举报
回复
加载更多回复(11)

80,354

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧