Adaptive/Expandable Recyclerview 问卷调查

Adaptive/Expandable Recyclerview for a questionnaire

信息: 我正在制作一个包含问卷调查的应用程序,其中 Yes/No 个复选框作为答案。问卷是用 RecyclerView 制作的,每个问题都有一个 CardView。 CardView 包含一个 TextView(问题文本)和两个复选框(是和否框)。

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/caseInfoItem
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginTop="30dp"
    android:elevation="0dp"
    app:cardBackgroundColor="@android:color/transparent"
    app:cardElevation="0dp"
    app:layout_constraintTop_toBottomOf="@id/cameraView">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/questionText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/round"
            android:ems="27"
            android:paddingEnd="10dp"
            android:paddingStart="10dp"
            android:textSize="20sp"
            android:gravity="center_vertical"/>

        <TextView
            android:id="@+id/Yes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_toEndOf="@+id/checkYes"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/No"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_toEndOf="@+id/checkNo"
            android:layout_centerVertical="true"/>

        <CheckBox
            android:id="@+id/checkYes"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginStart="50dp"
            android:background="?android:attr/listChoiceIndicatorMultiple"
            android:button="@null"
            android:layout_toEndOf="@id/questionText"/>

        <CheckBox
            android:id="@+id/checkNo"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginStart="50dp"
            android:background="?android:attr/listChoiceIndicatorMultiple"
            android:button="@null"
            android:layout_toEndOf="@id/Yes"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

目标与问题: 根据答案,我的问题列表应该在特定位置(索引)用一两个 CardView 扩展。

示例:如果我回答 'Yes' 一个问题(选中“是”框),则与已回答问题相关的另一个问题应该出现在我的 RecyclerView 下方. 明确一点: 不是列表的末尾,而是在我刚刚回答的具体问题之下。

怎么样??

这是我的适配器问卷的样本Activity:

public class CreateCaseInfoAdapter extends RecyclerView.Adapter<CreateCaseInfoAdapter.CaseHolder> {

ArrayList<String> list;
CustomYesCheckListener yeslistener;
CustomNoCheckListener nolistener;

public CreateCaseInfoAdapter(ArrayList<String> list, CustomYesCheckListener yeslistener, CustomNoCheckListener nolistener) {
    this.list = list;
    this.yeslistener = yeslistener;
    this.nolistener = nolistener;
}

public CreateCaseInfoAdapter(ArrayList<String> list) {
    this.list = list;

}

@NonNull
@Override
public CaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.create_case_info_item, parent, false);

    return new CaseHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final CaseHolder holder, final int position) {
    String _item = list.get(position);
    holder.mSpgText.setText(_item);

    holder.mCheckYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                holder.mCheckNo.setChecked(false);
            }
            yeslistener.onCheckClick(holder.getPosition(), buttonView, isChecked);
        }
    });

    holder.mCheckNo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                holder.mCheckYes.setChecked(false);
            }
            nolistener.onCheckClick(holder.getPosition(), buttonView, isChecked);
        }
    });

}

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

class CaseHolder extends RecyclerView.ViewHolder {
    public final CardView mView;
    public final TextView mSpgText;
    public final TextView mYes;
    public final TextView mNo;
    public final CheckBox mCheckYes;
    public final CheckBox mCheckNo;


    public CaseHolder(View view) {
        super(view);

        mView = view.findViewById(R.id.caseInfoItem);
        mSpgText = view.findViewById(R.id.questionText);
        mYes = view.findViewById(R.id.Yes);
        mNo = view.findViewById(R.id.No);
        mCheckNo = view.findViewById(R.id.checkNo);
        mCheckYes = view.findViewById(R.id.checkYes);

    }
}

}

public void createQuestions() {

    switch (mCase.getReportType().toLowerCase()) {
        case "BIO": // Ignore this
            final ArrayList<String> arrayList = questionsBio(); // method creates a list with 'hardcoded' questions.

            adapter = new CreateCaseInfoAdapter(arrayList,new CustomYesCheckListener() {
                @Override
                public void onCheckClick(int position, CompoundButton buttonView, boolean isChecked) {
                    if (position == 0) {
                        String test = "TEST TEST TEST TEST TEST TEST TEST TEST!!!";

                        if (isChecked) {
                            arrayList.add(1, test);
                            adapter.notifyItemInserted(1);
                        }
                    }
                }
            }, new CustomNoCheckListener() {
                @Override
                public void onCheckClick(int position, CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        Toast.makeText(CreateCaseInfoActivity.this, "No", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            adapter.notifyDataSetChanged();
            recyclerView.setMinimumHeight(Math.round(TypedValue.applyDimension
                    (TypedValue.COMPLEX_UNIT_DIP, calculator(arrayList), getResources().getDisplayMetrics()))); // calculator method here!
            recyclerView.setAdapter(adapter);
            break;

我知道,我可能没有按照预期的方式使用我的 RecyclerView,因为我将 minHeight 设置为列表中所有 CardView 的长度。

自定义监听器: 我制作了两个自定义侦听器。每个复选框一个。我正在这样做,因此可以对每个复选框执行一个操作(并且有效)。

So,我试过 adapter.notifyItemInserted(索引位置)和 adapter.notifyDataSetChanged() 并且逻辑上最后一种方法不起作用,因为它重新加载整个列表,因此取消选中我的答案。

任何suggestions/ideas?

这是我在本网站上提出的第一个问题,如有任何反馈,我们将不胜感激。

编辑 答案:出于某种原因,我让它与adapter.notifyItemInserted(索引位置)一起工作。不知何故它现在有效。原因可能是,我脑袋放屁,改写了 notifyItemSelected ...无论如何,我希望这可以帮助其他人。

出于某种原因,我让它与 adapter.notifyItemInserted(索引位置)一起工作。不知何故它现在有效。原因可能是,我脑袋放屁,改写了 notifyItemSelected ...无论如何,我希望这可以帮助其他人。