Android:在“可点击范围"的“单击"上使ListView行中的特定小部件可见 [英] Android: Make visible to particular widgets in the row of ListView on Click of Clickable span

查看:85
本文介绍了Android:在“可点击范围"的“单击"上使ListView行中的特定小部件可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListView行中,我没有几个小部件.

Here I have few widgets in a row of ListView.

我需要找到小部件的正确位置并管理它们在适配器内的可见性.当前,它对ListViewlast item可见.

I need to get right position of the widgets and manage their visibilities inside adapter. Currently it makes visible to last item of the ListView.

我也在这里使用getTag()setTag(),但是没有成功.

I'm using getTag() and setTag() also here but its not getting success.

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.clist_item, null);
        holder = new ViewHolder();
holder.requestion_layout = (RelativeLayout) convertView.findViewById(R.id.requestion_layout);
holder.submitAnswerBtn = (RelativeLayout) convertView.findViewById(R.id.submit_answer_btn);
holder.questionTitle = (TextView) convertView.findViewById(R.id.question_title);
 //these are the widgets 
    holder.requestion_layout.setTag(position);
    holder.submitAnswerBtn.setTag(position);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


//trying to change visibility on click of Clickable Span 
 Spannable main_words = new SpannableString(items.get(position).getAnswer() + "");
        int color1 = ContextCompat.getColor(activity, R.color.light_black);
        main_words.setSpan(new ForegroundColorSpan(color1), 0, main_words.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        int color2 = ContextCompat.getColor(activity, R.color.tab_green);
        Spannable wordTwo = new SpannableString("Reply");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //Toast.makeText(DummyActivity.this, "Click", Toast.LENGTH_LONG).show();
           Integer position_clicked = (Integer) v.getTag();
           //Widgets are not getting their positions here
                holder.requestion_layout.setVisibility(View.VISIBLE);
                holder.submitAnswerBtn.setVisibility(View.VISIBLE);
            }
        };
        wordTwo.setSpan(clickableSpan, 0, wordTwo.toString().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        wordTwo.setSpan(new ForegroundColorSpan(color2), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        wordTwo.setSpan(new StyleSpan(Typeface.ITALIC), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        CharSequence concat = TextUtils.concat(main_words, " ", wordTwo);
        holder.answerValue.setText(concat);
        holder.answerValue.setMovementMethod(LinkMovementMethod.getInstance());
        holder.answerValue.setHighlightColor(Color.TRANSPARENT);
        holder.answerValue.setVisibility(View.VISIBLE);
        holder.answerTitle.setVisibility(View.VISIBLE);
return convertView;
 }

推荐答案

对于此问题,您必须使用arrayList并在该位置设置一个标记为

For this issue you must use an arrayList and set a flag there with positions as

//Define arraylist globally
 private List<Boolean> requestionVisibleState;

//set a default value in constructor of adapter
 this.requestionVisibleState = new ArrayList<>(Arrays.asList(new Boolean[getCount()]));
    Collections.fill(this.requestionVisibleState, false);

//In getView after completing if else statement
 if (requestionVisibleState.get(position)) {
        holder.submitAnswerBtn.setVisibility(View.VISIBLE);
        holder.requestion_layout.setVisibility(View.VISIBLE);
    }
//And in `onClick` of your ClicableSpan
                    if (requestionVisibleState.get(position)) {
                        requestionVisibleState.set(position, false);
                    } else {
                        requestionVisibleState.set(position, true);
                    }
                    notifyDataSetChanged();

我希望它对您有用.

这篇关于Android:在“可点击范围"的“单击"上使ListView行中的特定小部件可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆