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

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

本帖最后由 nickDeLove 于 2014-10-13 20:34: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 

end = (i 

//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 indices = new ArrayList();

while (pos != -1) {

indices.add(pos);

pos = s.indexOf(c, pos + 1);

}

return (Integer[]) indices.toArray(new Integer[0]);

}

}

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

------解决思路----------------------

getEachWord为每一个单词加Link,性能比较差,你可以考虑为TextView加一个onTouch事件,根据点击的x,y坐标计算被点击的单词,然后处理。

或者像异步加载图片一样,异步为textview加span

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值