如何在recyclerview中一次仅显示一个列表项?

时间:2018-09-18 09:37:28

标签: android android-layout onclicklistener visibility android-adapter

我正在使用<form autocomplete="off" class="detail-info-form" [formGroup]="formGroupObj" novalidate> <div class="row no-gutters group-outer-wrapper"> <div *ngFor="let group of row.groups; let outerIndex=index;" class="group-wrapper group-col group-col-{{group.length}}"> <div class="group-inner-wrapper"> <div class="group-name ml-2 px-2 d-inline-block font-weight-bold">{{group.key | humanize}}</div> <div class="mt-1" [formArrayName]="group.key"> <div *ngFor="let ctrl of formGroupObj.get(group.key)['controls']; let i = index" [formGroupName]="i"> <div *ngFor="let gfield of group.value ; let i=index;"> <div *ngIf="nonEditableColumns.indexOf(gfield.fieldName) === -1" class="form-group clearfix" [style.z-index]="(20 - i)"> <label for="{{gfield.fieldName}}" class="pr-1 mb-0 detail-row-label float-left text-right"> <span class="d-inline-block row-detail-value-middle row-detail-small-font" title="{{gfield.fieldName | humanize}}">{{gfield.fieldName | humanize}}</span> </label> <span class="d-inline-block float-left row-detail-value-middle row-detail-small-font">: </span> <div class="pl-1 row-detail-value-wrapper float-left"> <div class="row-detail-value"> <div *ngIf="currentEditableRowId !== row.id; else paramInput" class="row-detail-value"> <span class="row-detail-value-text row-detail-value-middle d-inline-block ellipsis" title="{{gfield.value}}">{{gfield.value}}</span> </div> <ng-template #paramInput> <div class="input-group"> <input type="text" class="form-control form-control-sm w-100 text-left" placeholder="{{gfield.fieldName | humanize}}" [formControlName]="gfield.fieldName" name="{{gfield.fieldName}}" /> </div> </ng-template> </div> </div> </div> </div> </div> </div> </div> </div> </div> </form> ,它有两个RecyclerView;一个问题,一个答案。以下为参考视图。默认情况下,我将答案TextView设置为用户不可见。现在,如果用户单击按钮,答案将可见。到目前为止,一切正常,现在我想要的是,如果用户对问题1的答案的公开答案是可见的,现在如果用户单击第二个Listitem按钮,则应该隐藏第一个答案TextView。我希望一次只能看到一个答案。

问题1(按钮)

答案

问题2(按钮)

答案

问题3(按钮)

答案

问题4(按钮)

答案

TextView

1 个答案:

答案 0 :(得分:0)

我没有测试它,因为我没有您的布局文件,但是您应该执行以下操作:

public class BuyerFaqsAdapter extends RecyclerView.Adapter<BuyerFaqsAdapter.MyViewHolder> {

    private ArrayList<BuyerMemberShipFAQParent> brandList;

    private Context context;
    private MyViewHolder lastClickedHolder;


    public BuyerFaqsAdapter(final Context context, final ArrayList<BuyerMemberShipFAQParent> brandList) {
        this.brandList = brandList;
        this.context = context;

    }

    @Override
    public BuyerFaqsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_layout_faq_expand_parent, parent, false);


        return new BuyerFaqsAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final BuyerFaqsAdapter.MyViewHolder holder, final int position) {
        holder.tvfaq.setText(brandList.get(position).getFAQParentText());
        holder.tvans.setVisibility(View.GONE);
        holder.tvans.setText(brandList.get(position).getFAQAnswerText());
        holder.itemView.setId(position);

        holder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastClickedHolder != null) {
                    collapse(lastClickedHolder);
                }
                expand(holder);
                lastClickedHolder = holder;
            }
        });
    }

    @Override
    public int getItemCount() {
        return brandList.size();
    }

    private void collapse(BuyerFaqsAdapter.MyViewHolder holder) {
        holder.tvans.setVisibility(View.VISIBLE);
        holder.ivImage.setImageResource(R.drawable.ic_arrow_faq_up);
    }

    private void expand(BuyerFaqsAdapter.MyViewHolder holder) {
        holder.tvans.setVisibility(View.GONE);
        holder.ivImage.setImageResource(R.drawable.ic_arrow_faq);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tvans, tvfaq;
        public ImageView ivImage;

        MyViewHolder(View view) {
            super(view);
            tvfaq = (TextView) view.findViewById(R.id.row_layout_faqtv);
            tvans = (TextView) view.findViewById(R.id.row_layout_answertv);
            ivImage = (ImageView) view.findViewById(R.id.row_layout_ivarrow);
        }
    }

}