首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwitchCompat on RecyclerViewAdapter

SwitchCompat on RecyclerViewAdapter
EN

Stack Overflow用户
提问于 2018-04-22 05:49:20
回答 1查看 1.2K关注 0票数 0

我只在我的SwitchCompat适配器中使用一个RecyclerView。我需要点击项目在RecyclerView上的位置,当我使用TextView而不是SwitchCompat时,它工作得很好。但SwitchCompat没有回应,也没有返回位置。

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

代码语言:javascript
复制
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布局

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-22 06:03:25

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

代码语言:javascript
复制
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
                    }
                }
            }
        });
    }
}

希望这能帮上忙!

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49962944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档