我有FAQ作为列表视图,它将包含一个问题,一个答案和一个图像视图。我将在下面的屏幕快照中附加屏幕快照。单击第一个项目时,它将使第一行的答案可见(最初可见性消失了。问题在于它也会使第十项可见。直到第四项都出现相同的情况
@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;
}
答案 0 :(得分:1)
使用
answers.getVisibility()==View.VISIBLE
代替
answers.isShown()
尝试一下
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answers.getVisibility()==View.VISIBLE) {
answers.setVisibility(View.GONE);
arrow.setRotation(0);
} else {
answers.setVisibility(View.VISIBLE);
arrow.setRotation(180);
}
//mAdapter.notifyDataSetChanged();
}
});
如果您想在recyclerview或listview中进行维护,请在这里https://stackoverflow.com/a/51744548/7666442
答案 1 :(得分:1)
这就是您需要做的。
创建全局变量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();
}
});
答案 2 :(得分:0)
if (arrow.getRotation()==0) {
answers.setVisibility(View.GONE);
} else {
answers.setVisibility(View.VISIBLE);
}
如果未添加,则在适配器的onClickListener之外添加此条件。