RecyclerViewAdapter上的SwitchCompat

时间:2018-04-22 05:49:20

标签: java android switchcompat

我在SwitchCompat适配器中只使用了一个RecyclerView。我需要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>

1 个答案:

答案 0 :(得分:3)

您需要为import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'dropdown', templateUrl: './dropdown.component.html', styleUrls: ['./dropdown.component.scss'] }) export class DropdownComponent implements OnInit { content: DropDownContent[] = new Array<DropDownContent>(); @Input() hasCheckbox:boolean = false; @Input() label:string = null; @Input() id:string = 'defaultId' @Input() selectedId:number = -1; @Input() size: 'lg' | 'md' | 'sm' = 'lg'; @Input() set contentInput(contentInput: DropDownContent[]) { if (contentInput) { this.content = contentInput.map(data => { return <DropDownContent>(data); }); console.log(this.content); } else { //? } } constructor() { } ngOnInit() { console.log(this.id) } } export class DropDownContent { value: number; text: string; } 设置onSetCheckedChangeListeneronTouchListener。因此,SwitchCompat类中的实现应如下所示。

ViewHolder

希望有所帮助!