Android:在单击可单击跨度时使 ListView 行中的特定小部件可见

标签 android listview

这里我在 ListView 的一行中有几个小部件。

我需要获得小部件的正确位置并管理它们在适配器内的可见性。目前,它使 ListViewlast item 可见。

我在这里也使用了 getTag()setTag() 但没有成功。

@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 并在那里设置一个标志,位置为

//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 行中的特定小部件可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38872198/

相关文章:

java - 底部导航 View : Glitch that overwrites other layout

listview - 具有水平项目流的 Windows 8 ListView

Android:在ListView的onItemClickListener()中获取TextView

java - 安卓+Appium : Scroll inside ListView to specific position and click on it

java - 单击按钮时显示 ListView 项目

listview - 滚动 ListView 直到 xamarin.forms 中的最终项目时应用程序崩溃

java - AndroidStudio- fatal error : IndexOutOfBoundsException: Invalid index 0, 大小为 0

java - 如何在 postexecute 方法中引用 JSON onResponse?

android - Build OS X需要太多时间

java - 计算最近截面的角度