在按钮点击监听器的卡片视图内逐字突出显示字符串文本

时间:2017-01-02 10:11:48

标签: android android-cardview

当我点击CardView中的按钮时,我必须突出显示特定的单词。我需要突出显示单词的文本也是CardView的一部分。 问题是,当我点击按钮时,其他CardViews中的文字也会突出显示。

导致此问题的原因以及如何解决此问题?

这是我的代码:

 @Override
public void onBindViewHolder(final DuaViewHolder duaViewHolder,final  int i) {

    ci = allDuasLists.get(i);
    duaViewHolder.arebicText.setText(ci.getDuas());
    duaViewHolder.arebicEuivalantText.setText(ci.getArebicText());
    duaViewHolder.nativeText.setText(ci.getNativeText());
    mp = new MediaPlayer();
    duaViewHolder.playAudio.setBackgroundResource(R.drawable.play_48);

      class MyTimer extends CountDownTimer {
          public MyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            WordtoSpan = new SpannableString(CurrentDua);
        }
        @Override
        public void onFinish() {
            k=0;
            this.cancel();
            Toast.makeText(context, "Timer finished", Toast.LENGTH_LONG).show();
        }
        @Override
        public void onTick(long millisUntilFinished) {
                WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), arrString[k], arrString[k + 1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                k++;
                duaViewHolder.arebicText.setText(WordtoSpan);
        }
    }

    duaViewHolder.playAudio.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ci = allDuasLists.get(i);
            CurrentDua = ci.getDuas();
            Log.e("Current Dua", CurrentDua);
            arrString = new int[CurrentDua.length() + 1];
            MakeSubString();
            duaViewHolder.playAudio.setBackgroundResource(R.drawable.stop_48);
            MyTimer timer = new MyTimer(mp.getDuration() * 1000, 1300);
            timer.start();
            k = 0;
        }

    });
}

以下是它的样子:

Current look

1 个答案:

答案 0 :(得分:1)

尝试这种方式,而不是从列表中获取onclicklistener中的ci值

ci = allDuasLists.get(i);

将ci设置为duaViewHolder.playAudio标签,如

duaViewHolder.playAudio.setTag(ci);

并在onClickListener内部得到它

ci = /*(Cast to your ci class)*/ v.getTag();