java - RecyclerView 适配器上的 SwitchCompat

标签 java android switchcompat

我在 RecyclerView 适配器中仅使用一个 SwitchCompat。我需要在 RecyclerView 上单击的项目的位置,并且当我使用 TextView 而不是 SwitchCompat 时它工作正常。但SwitchCompat没有响应,也没有返回位置。

有人可以帮我吗?这是适配器文件。

public class Setting_Recycler_Adapter extends RecyclerView.Adapter<Setting_Recycler_Adapter.ViewHolder> {

    private static final String TAG = "val" ;
    private Context mContext;
    private ArrayList<Channel_Model> channelsData;
    private Setting_Recycler_Adapter.ClickInterface click;

    public Setting_Recycler_Adapter(Context mContext, ArrayList<Channel_Model> data,Setting_Recycler_Adapter.ClickInterface clik) {
        this.mContext = mContext;
        this.channelsData = data;
        this.click = clik;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        SwitchCompat mSwitchCompat;

        public ViewHolder(View itemView) {
            super(itemView);

            mSwitchCompat = (SwitchCompat) itemView.findViewById(R.id.setting_channel_switch);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            click.posClicked((short)getAdapterPosition());
        }
    }

    @Override
    public Setting_Recycler_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.setting_recycler, parent, false);
        return new Setting_Recycler_Adapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final Setting_Recycler_Adapter.ViewHolder holder, int position) {
        holder.mSwitchCompat.setText(channelsData.get(position).getTitle());
        holder.mSwitchCompat.setChecked(channelsData.get(position).isChannel());
    }

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

    interface ClickInterface{void posClicked(short p);
}

以及RecyclerView布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutDirection="rtl"
    android:textDirection="rtl">

    <android.support.v7.widget.SwitchCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/setting_channel_switch"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:text="channel Name"
        android:textSize="17sp"/>
</LinearLayout>

最佳答案

您需要为 SwitchCompat 设置 onSetCheckedChangeListeneronTouchListener。因此,ViewHolder 类中的实现应如下所示。

public class ViewHolder extends RecyclerView.ViewHolder {
    SwitchCompat mSwitchCompat;
    Boolean isTouched = false;

    public ViewHolder(View itemView) {
        super(itemView);
        mSwitchCompat = (SwitchCompat) itemView.findViewById(R.id.setting_channel_switch);

        mSwitchCompat.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                isTouched = true;
                return false;
            }
        });

        mSwitchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isTouched) {
                    isTouched = false;

                    if (isChecked) {
                        click.posClicked((short)getAdapterPosition());
                    } else {
                        // Do something on un-checking the SwitchCompat
                    }
                }
            }
        });
    }
}

希望有帮助!

关于java - RecyclerView 适配器上的 SwitchCompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49962944/

相关文章:

android - 具有不同状态图像的自定义 switchcompat

java - JComboBox 项目监听器

c# - 如何在协作/通信图中发出对象实例化信号?

android - 从子 Activity 中获取主要 Activity 的推荐方法是什么

android - JobScheduler 每次满足条件最多运行一次作业

android - 安卓上同时应用两个pjsua

android - 创建自己的 SwitchCompat 首选项

Android SwitchCompat 在关闭状态下不呈现轨道

java - JPA - 使用 mappedBy 属性定义拥有实体的区别

java - 如何使用 java PDFBox 以编程方式将图像插入到 AcroForm 字段中?