gpt4 book ai didi

Android Listview onclick 随机选择

转载 作者:行者123 更新时间:2023-11-29 18:45:31 25 4
gpt4 key购买 nike

我有 FAQ 作为 ListView ,它将包含一个问题、一个答案和一个 ImageView 。我附上下面的屏幕截图。当单击第一个项目时,第一行的答案将可见(最初visibility gone) 。问题是它也会使第十项可见。同样的情况发生到第四项

       @Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.faq_listitem, parent,false);

holder.mQuestionnoTextView= convertView.findViewById(R.id.question_no);
holder.mQuestionTextView = convertView.findViewById(R.id.question);
holder.mAnswerTextView = convertView.findViewById(R.id.answer);
holder.viewMore=convertView.findViewById(R.id.show_more) ;

convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();

int number=position+1;

holder.mQuestionnoTextView.setText(MessageFormat.format("{0})", number));
holder.mQuestionTextView.setText(MessageFormat.format("{0}", getItem(position).getQuestion()));
holder.mAnswerTextView.setText(getItem(position).getAnswer());
final TextView answers=holder.mAnswerTextView;
final ImageView arrow=holder.viewMore;

convertView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

if (answers.isShown()) {
answers.setVisibility(View.GONE);
arrow.setRotation(0);
} else {
answers.setVisibility(View.VISIBLE);
arrow.setRotation(180);
}
//mAdapter.notifyDataSetChanged();

}

});

return convertView;
}

最佳答案

所以这就是您需要做的。

创建一个全局变量List<Integer> savedPositions = new ArrayList<>();

if(savedPositions.contains(position)){
answers.setVisibility(View.VISIBLE);
arrow.setRotation(180);
}else{
answers.setVisibility(View.GONE);
arrow.setRotation(0);
}

convertView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

if (savedPositions.contains(position)) {
savedPositions.remove((Integer) position);
answers.setVisibility(View.GONE);
arrow.setRotation(0);

} else {
savedPositions.add(position);
answers.setVisibility(View.VISIBLE);
arrow.setRotation(180);
}
notifyDataSetChanged();

}

});

关于Android Listview onclick 随机选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52114593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com