【问题标题】:using multiple RadioButtons in a recyclerview在回收站视图中使用多个 RadioButtons
【发布时间】:2016-04-18 23:27:46
【问题描述】:

我正在制作一个程序,我想在其中保存哪些球员正在训练,这只是教练的日记。所以我想在 recyclerview 的每一行都有三个单选按钮(一个用于玩家在场时,一个在他不在时,一个在他有理由不在那里时)。

我想将 PersonItem 中的状态值更改为状态 1,2 或 3,以便稍后在程序中使用它来将其保存到数据库中。

我遇到的问题是 recyclerview 没有正确显示,例如当我有两个 PersonItems 显示时。

/////////////////////////////////////// ////////////////////////////////.

PersonItem1.

(单选按钮)存在。

/////////////////////////////////////// /////////////////////////////////////////////。

PersonItem1.

(单选按钮)存在,(单选按钮)不存在。

/////////////////////////////////////// /////////////////////////////////////////。

单选按钮也有奇怪的行为。

代码如下:

public NewTrainingAdapter(Context context, ArrayList<PersonItem> feedItemList) {
    this.feedItemList = feedItemList;
    this.mContext = context;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.vrstica_new_training_row, null);

    view.setClickable(true);

    CustomViewHolder viewHolder = new CustomViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(final CustomViewHolder customViewHolder,final int i) {
    PersonItem feedItem = feedItemList.get(i);

    customViewHolder.name.setText(feedItem.getName().toString() + " " + feedItem.getLastName().toString());

    setRadio(customViewHolder, 1);



    customViewHolder.present.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            feedItemList.get(i).setState(0);
            setRadio(customViewHolder, feedItemList.get(i).getState());
        }
    });
    customViewHolder.absent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            feedItemList.get(i).setState(0);
            setRadio(customViewHolder, feedItemList.get(i).getState());
        }
    });
    customViewHolder.forgiven.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            feedItemList.get(i).setState(0);
            setRadio(customViewHolder, feedItemList.get(i).getState());
        }
    });



}


@Override
public int getItemCount() {
    return (null != feedItemList ? feedItemList.size() : 0);
}


public interface OnItemClickListener {
    public void onItemClick(View view, int position);
}

public void setOnItemClickListener(final OnItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

private void setRadio(final CustomViewHolder holder, int selection) {

    RadioButton b1 = holder.present;
    RadioButton b2 = holder.absent;
    RadioButton b3 = holder.forgiven;

    if (selection == 0) b1.setChecked(true);

    if (selection == 1) b2.setChecked(true);

    if (selection == 2) b3.setChecked(true);

}



public class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    protected TextView name;
    protected RadioButton present,absent,forgiven;
    protected RadioGroup presence;

    public CustomViewHolder(View view) {
        super(view);
        this.name = (TextView) view.findViewById(R.id.textViewPlayerName);
        this.presence = (RadioGroup) view.findViewById(R.id.radioGroupPlayerInTraining);
        this.present = (RadioButton) view.findViewById(R.id.radioButtonPresent);
        this.absent = (RadioButton) view.findViewById(R.id.radioButtonAbsent);
        this.forgiven = (RadioButton) view.findViewById(R.id.radioButtonForgiven);
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (itemClickListener != null) {
            itemClickListener.onItemClick(view, getAdapterPosition());
        }
    }
}

vrstica_new_training_row 中的代码也是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textViewPlayerName" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/radioGroupPlayerInTraining">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Prisoten"
            android:id="@+id/radioButtonPresent"
            android:checked="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Odsoten"
            android:id="@+id/radioButtonAbsent"
            android:checked="false" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Opravičen"
            android:id="@+id/radioButtonForgiven"
            android:checked="false" />

    </RadioGroup>
</LinearLayout>

有没有人有任何建议我需要更改以使我的代码按我想要的方式工作。 感谢您提前回答。 :)

【问题讨论】:

    标签: android radio-button android-recyclerview


    【解决方案1】:

    我看到你还没有答案。我还不能完全理解您的问题,因为我只在您的结果中看到“///////”。但是,如果您遇到与我相同的问题,那就是 RecyclerView 的回收功能正在将我的选择复制到回收视图中。如果您的应用不太依赖该功能,那么解决此问题的一种简单方法是将 RecyclerView 视图缓存设置为您的列表大小:

    yourRecyclerView..setItemViewCacheSize(yourItemList.size());
    

    如果列表动态更改,请确保更新此内容...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多