简体   繁体   English

RecyclerViewAdapter上的SwitchCompat

[英]SwitchCompat on RecyclerViewAdapter

I'm using only one SwitchCompat in my RecyclerView adapter. 我在RecyclerView适配器中仅使用一个SwitchCompat I need position of the clicked item on RecyclerView and it's working fine when I use TextView instead of SwitchCompat . 我需要单击项目在RecyclerView上的位置,当我使用TextView而不是SwitchCompat时,它的工作正常。 But SwitchCompat have no response and no position returned. 但是SwitchCompat没有响应,也没有返回位置。

Can anyone help me please? 谁能帮我吗? Here's the Adapter file. 这是适配器文件。

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);
}

And the RecyclerView layout 还有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>

You need to set the onSetCheckedChangeListener along with onTouchListener for your SwitchCompat . 您需要为SwitchCompat设置onSetCheckedChangeListeneronTouchListener So the implementation inside your ViewHolder class should look like the following. 因此, 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
                    }
                }
            }
        });
    }
}

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM