如何在recyclerview中为togglebutton.setOnCheckedChangedListener创建接口?

时间:2019-03-26 13:50:04

标签: java android android-recyclerview android-togglebutton

我在recylerview中有一个切换按钮,其setOnCheckedChangeListener需要通过一个接口设置,该接口将在Activity中为自定义代码调用。我只能找到有关onClick界面的信息。有关代码或信息的任何帮助都将有所帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

我正在使用适配器中的Click事件接口,并在活动中执行如下操作:-

创建一个像这样的接口,这是我的接口类:-

public interface FriendClickListner {
    void onClick(String type, String id);
}

并在活动中创建其对象,如下所示:-

private FriendClickListner clickListner = new FriendClickListner() {
        @Override
        public void onClick(String type, String id) {
            // do your stuff here here after click on adapter item
        }
    };

并像这样在适配器构造函数中传递clickListner对象:-

allFriendsAdapter = new AddGroupAllFriendsAdapter(CreateGroupActivity.this, friendsList, mUserReferance, clickListner);

现在在adater中创建如下的适配器构造函数:-

private Context context;
    private ArrayList<? extends ParticipantList> participantLists;
    private DatabaseReference mUserDatabase;
    private FriendClickListner friendClickListner;

    public AddGroupAllFriendsAdapter(Context context, ArrayList<? extends ParticipantList> idList,
                                     DatabaseReference mUserDatabase, FriendClickListner friendClickListner) {
        this.context = context;
        this.participantLists = idList;
        this.mUserDatabase = mUserDatabase;
        this.friendClickListner = friendClickListner;
    }

并按如下所示在friendClickListner中传递值:-

if(friendClickListner != null){
                    friendClickListner.onClick("group", participantLists.get(pos).getFriend_id());
                }