select 怎么没有查到单选组i RecyclerView?

how select no checked radio group i RecyclerView?

我有 RecyclerView 和 60 个项目,包括 RadioGroup,在 RadioGroup 我有两个 RadioButton... 我在 RadioGroup 上设置 Listener,直到用户不勾选 60 radio group,show snack to say "no, you have not permission to go to next activity, please answer all questions"! 现在我需要添加另一个这样的选项...... 显示未选中的 radioGroup(例如 cardView 背景更改或任何想法)...

qustions_item_row.xml

<RadioGroup
    android:id="@+id/ques_radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_ques_title"
    android:layout_gravity="end|center"
    android:gravity="start"
    android:orientation="vertical"
    android:textAlignment="textStart"
    android:textDirection="rtl">

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/first_ques"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|right"
        android:layout_margin="@dimen/quest_margin"
        android:checked="false"
        android:gravity="end|right"
        android:padding="@dimen/quest_padding"
        android:text="@{question.firstQuest}"
        android:textColor="@color/ques"
        android:textDirection="ltr"
        android:textSize="@dimen/text_ques"
        />

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/second_ques"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|right"
        android:layout_margin="@dimen/quest_margin"
        android:checked="false"
        android:gravity="end|right"
        android:padding="@dimen/quest_padding"
        android:text="@{question.secondQuest}"
        android:textColor="@color/ques"
        android:textDirection="ltr"
        android:textSize="@dimen/text_ques"
        />


</RadioGroup>

完整的适配器代码 (MbtiQuestAdapter.java)

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.List;



public class MbtiQuestAdapter extends RecyclerView.Adapter<MbtiQuestAdapter.MbtiQuestViewHolder> {

  private Context context;
  private List<Question> questionList;
  private LayoutInflater layoutInflater;
  private FloatingActionButton fabResult;
  private CoordinatorLayout coordinator;
  private int checkRadio = 0;
  private Snackbar snackbar;


  private static final String TAG = "MbtiQuestAdapter";

  public MbtiQuestAdapter(Context context, List<Question> questionList, FloatingActionButton fab, CoordinatorLayout coordinator) {
    this.context = context;
    this.questionList = questionList;
    this.fabResult = fab;
    this.coordinator = coordinator;
  }


  @NonNull
  @Override
  public MbtiQuestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if (layoutInflater == null) {
      layoutInflater = LayoutInflater.from(context);
    }

    QuesItemRowBinding binding = DataBindingUtil.inflate(layoutInflater,
      R.layout.ques_item_row, parent, false);

    return new MbtiQuestViewHolder(binding);
  }

  @Override
  public void onBindViewHolder(@NonNull final MbtiQuestViewHolder holder, final int position) {
    holder.binding.setQuestion(questionList.get(position));


    holder.binding.quesRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (holder.binding.firstQues.isChecked() || holder.binding.secondQues.isChecked()) {
          checkRadio++;

        } else {
          snackbar = Snackbar.make(coordinator, R.string.choose_one_of_theme, Snackbar.LENGTH_INDEFINITE);
          snackbar.show();
        }
      }
    });


    if (fabResult != null) {

      fabResult.setOnClickListener(new View.OnClickListener() {
                                     @Override
                                     public void onClick(View v) {
                                       if (checkRadio == 60) {
                                         context.startActivity(new Intent(context, ResultActivity.class));
                                         Log.i(TAG, "onClick: " + checkRadio);
                                       } else {
                                         snackbar = Snackbar.make(coordinator, R.string.check_all_ques_err, Snackbar.LENGTH_SHORT);
                                         snackbar.show();


                                       }
                                     }
                                   }

      );
    }


  }

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

  public class MbtiQuestViewHolder extends RecyclerView.ViewHolder {

    private QuesItemRowBinding binding;

    public MbtiQuestViewHolder(QuesItemRowBinding binding) {
      super(binding.getRoot());
      this.binding = binding;
    }
  }


}

我们开始吧,这是您目标的完整代码

ObjectListItem.java

public class ObjectListItem {

private String itemTitle = "";
private boolean opt1, opt2, opt3;
private int selectedAnswerPosition = -1;
private boolean shine;

public String getItemTitle() {
    return itemTitle;
}

public void setItemTitle(String itemTitle) {
    this.itemTitle = itemTitle;
}

public boolean isOpt1() {
    return opt1;
}

public void setOpt1(boolean opt1) {
    this.opt1 = opt1;
    if (opt1) {
        setOpt2(false);
        setOpt3(false);
    }
}

public boolean isOpt2() {
    return opt2;
}

public void setOpt2(boolean opt2) {
    this.opt2 = opt2;
    if (opt2) {
        setOpt1(false);
        setOpt3(false);
    }
}

public boolean isOpt3() {
    return opt3;
}

public void setOpt3(boolean opt3) {
    this.opt3 = opt3;
    if (opt3) {
        setOpt1(false);
        setOpt2(false);
    }
}

public int getSelectedAnswerPosition() {
    return selectedAnswerPosition;
}

public void setSelectedAnswerPosition(int selectedAnswerPosition) {
    this.selectedAnswerPosition = selectedAnswerPosition;
}

public boolean isShine() {
    return shine;
}

public void setShine(boolean shine) {
    this.shine = shine;
}}

layout_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/root"
    android:layout_height="wrap_content"
    android:layout_marginEnd="7dp"
    android:layout_marginStart="7dp"
    android:layout_marginTop="7dp"
    android:background="#989898"
    android:orientation="vertical"
    android:padding="7dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="This is title" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="Option 1" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="Option 2" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="Option 3" />

    </LinearLayout>

</LinearLayout>

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.listItemsViewHolder> {

private Context context;

private ArrayList<ObjectListItem> objectListItems;

private OnRadioItemClicked onRadioItemClicked;

public void setOnRadioItemClicked(OnRadioItemClicked onRadioItemClicked) {
    this.onRadioItemClicked = onRadioItemClicked;
}

public interface OnRadioItemClicked {
    void radioItemChecked(int itemPosition, int optNum);
}

public MyAdapter(Context context, ArrayList<ObjectListItem> objectListItems) {
    this.context = context;
    this.objectListItems = objectListItems;
}

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

    return new MyAdapter.listItemsViewHolder(itemView);
}

@Override
public void onBindViewHolder(final listItemsViewHolder holder, int position) {
    final ObjectListItem objectListItem = objectListItems.get(position);

    holder.title.setText("This is my awesome title " + position);
    holder.radioButton1.setChecked(objectListItem.isOpt1());
    holder.radioButton2.setChecked(objectListItem.isOpt2());
    holder.radioButton3.setChecked(objectListItem.isOpt3());

    if (objectListItem.isShine()) {
        holder.root.setBackgroundColor(ContextCompat.getColor(context, R.color.shine));
    } else {
        holder.root.setBackgroundColor(ContextCompat.getColor(context, R.color.normal));
    }
}

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

public class listItemsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private LinearLayout root;
    private TextView title;
    private RadioButton radioButton1, radioButton2, radioButton3;

    public listItemsViewHolder(View itemView) {
        super(itemView);
        root = itemView.findViewById(R.id.root);
        title = itemView.findViewById(R.id.title);
        radioButton1 = itemView.findViewById(R.id.radioButton1);
        radioButton2 = itemView.findViewById(R.id.radioButton2);
        radioButton3 = itemView.findViewById(R.id.radioButton3);
        radioButton1.setOnClickListener(this);
        radioButton2.setOnClickListener(this);
        radioButton3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.radioButton1:
                if (onRadioItemClicked != null)
                    onRadioItemClicked.radioItemChecked(getAdapterPosition(), 1);
                break;
            case R.id.radioButton2:
                if (onRadioItemClicked != null)
                    onRadioItemClicked.radioItemChecked(getAdapterPosition(), 2);
                break;
            case R.id.radioButton3:
                if (onRadioItemClicked != null)
                    onRadioItemClicked.radioItemChecked(getAdapterPosition(), 3);
                break;
        }
    }
}}

MainActivity.java

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/button_check"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:text="Check Answers" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity.java

    public class MainActivity extends AppCompatActivity implements MyAdapter.OnRadioItemClicked, View.OnClickListener {

    private MyAdapter myAdapter;

    private RecyclerView myRecyclerView;

    private LinearLayoutManager linearLayoutManager;

    private ArrayList<ObjectListItem> objectListItems;

    private Button buttonCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonCheck = findViewById(R.id.button_check);
        buttonCheck.setOnClickListener(this);
        myRecyclerView = findViewById(R.id.recyclerView);

        generateQuestions();
        myAdapter = new MyAdapter(this, objectListItems);
        myRecyclerView.setHasFixedSize(true);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myAdapter.setOnRadioItemClicked(this);
        myRecyclerView.setLayoutManager(linearLayoutManager);
        myRecyclerView.setAdapter(myAdapter);
    }

    private void generateQuestions() {
        objectListItems = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            ObjectListItem objectListItem = new ObjectListItem();
            objectListItem.setItemTitle("This is my awesome title " + i);
            objectListItems.add(objectListItem);
        }
    }

    @Override
    public void radioItemChecked(int itemPosition, int optNum) {
        objectListItems.get(itemPosition).setSelectedAnswerPosition(optNum);
        objectListItems.get(itemPosition).setShine(false);
        switch (optNum) {
            case 1:
                objectListItems.get(itemPosition).setOpt1(true);
                break;

            case 2:
                objectListItems.get(itemPosition).setOpt2(true);
                break;

            case 3:
                objectListItems.get(itemPosition).setOpt3(true);
                break;
        }
        myAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        int result = checkAnswers();
        if (result == -1) {
            Toast.makeText(MainActivity.this, "We Have All Anwers ;)", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Answer to this question (ISHAK) :|", Toast.LENGTH_SHORT).show();
            linearLayoutManager.smoothScrollToPosition(myRecyclerView, null, result);
            objectListItems.get(result).setShine(true);
            myAdapter.notifyDataSetChanged();
        }
    }

    private int checkAnswers() {
        for (int i = 0; i < objectListItems.size(); i++) {
            if (objectListItems.get(i).getSelectedAnswerPosition() == -1) {
                return i;
            }
        }
        return -1;
    }
}

如果用户点击检查答案按钮,应用会自动滚动到第一个未回答的问题并更改其背景。

my Github

上的完整代码

编码愉快;)

我正在使用数据绑定,我是这样解决的:

  //Card background if Uncheck the question
  private void showHideErrorBackground(boolean show) {
    for (Question question : questions) {
      question.setShowErrorBackground(show);
    }
    mbtiQuestAdapter.notifyDataSetChanged();
  }

在我的 Layout.xml 中,我将 app:cardBackgroundColor="@{question.showErrorBackground &amp;&amp; question.selectedId == 0 ? @color/card_bg : @android:color/transparent}" 设置为卡片视图

   <androidx.cardview.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:layoutDirection="rtl"
            android:textDirection="rtl"
            app:cardBackgroundColor="@{question.showErrorBackground &amp;&amp; question.selectedId == 0   ? @color/card_bg : @android:color/transparent}"
            app:cardCornerRadius="@dimen/card_radius"
            app:cardPreventCornerOverlap="true"
            app:cardUseCompatPadding="true">

这是我的 Question.java

public class Question extends BaseQuestion {

  private int selectedId;
  private boolean showErrorBackground;

 @Bindable
  public void setShowErrorBackground(boolean showErrorBackground) {
    this.showErrorBackground = showErrorBackground;
    notifyPropertyChanged(BR.showErrorBackground);
  }

 @Bindable
  public int getSelectedId() {
    return selectedId;
  }

  public void setSelectedId(int selectedId) {
    this.selectedId = selectedId;
    notifyPropertyChanged(BR.selectedId);
  }


  public boolean isShowErrorBackground() {
    return showErrorBackground;
  }


}

基础Question.java

public class BaseQuestion extends BaseObservable {
}

我解决了我的问题...但我以前解决过它但忘了把它放在这里..祝你好运