Есть вот такой RecyclerView c 3 типами view. На скрине показано только один тип View.
Подскажите как привязаться к Switch элементам, и поставить на них Listener?
package com.vocabulary.enso.Main.WordSetSelection;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.vocabulary.enso.Learn.LearnActivity;
import com.vocabulary.enso.Main.MainActivity;
import com.vocabulary.enso.R;
import java.util.ArrayList;
import java.util.List;
import model.Data.UnitSet;
import model.Database.Lab;
import model.Database.Model;
import static com.vocabulary.enso.Learn.LearnActivity.LEARN_AKTIVITY_KEY_INDEX;
public class UnitSetSelectionFragment extends Fragment implements UnitSetSelectionContract.ViewLearnFragment {
UnitSetSelectionContract.PresenterLearnFragment mPresenterLearnFragment;
private static int TYPE_SET_WITH_CATEGORY = 1;
private static int TYPE_SET_WITHOUT_CATEGORY = 2;
private static int EMPTY_TYPE_SET_WITH_CATEGORY = 3;
RecyclerView mSetRecyclerView;
SetsAdapter mAdapter;
FloatingActionButton startLearnButton;
List<Integer> unitSetIdList = new ArrayList<>();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Model model = Lab.get(getContext());
mPresenterLearnFragment = new UnitSetSelectionFragmentPresenter(model);
mPresenterLearnFragment.attachView(this);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.learn_menu, menu);
}
@Override
public void onDetach() {
super.onDetach();
mPresenterLearnFragment.detachView();
mPresenterLearnFragment = null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@SuppressLint("RestrictedApi")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_learn, container, false);
mSetRecyclerView = (RecyclerView)view.findViewById(R.id.flow_rw);
mSetRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mPresenterLearnFragment.viewIsReady();
startLearnButton = view.findViewById(R.id.learnAllSetsButton);
startLearnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BeginExercise();
}
});
return view;
}
public static UnitSetSelectionFragment newInstance() {
Bundle args = new Bundle();
UnitSetSelectionFragment fragment = new UnitSetSelectionFragment();
fragment.setArguments(args);
return fragment;
}
public void BeginExercise(){
Intent intent = new Intent(getActivity(), LearnActivity.class);
ArrayList<Integer> unitSetIdList = new ArrayList<>();
unitSetIdList.add(0);
intent.putIntegerArrayListExtra(LEARN_AKTIVITY_KEY_INDEX,unitSetIdList);
((MainActivity) getActivity()).startActivity(intent);
}
@SuppressLint("RestrictedApi")
public void UpdateUI(List<UnitSet> unitSetList) {
List<UnitSet> unitSet = unitSetList;
mAdapter = new SetsAdapter(unitSet);
mSetRecyclerView.setAdapter(mAdapter);
if(unitSet.size()>0) {
startLearnButton.setVisibility(View.VISIBLE);
}
}
public boolean isFirstWordSetFragmentInFolder(UnitSet unitSet, List<UnitSet> unitSets){
List<UnitSet> subWordsets = new ArrayList<>();
int minFolderID;
//Создание коллекции WordSets с FolderId таким же как и у unitSet.
for(UnitSet tmpWordset : unitSets){
if(unitSet.getFolderId() == tmpWordset.getFolderId()){
subWordsets.add(tmpWordset);
}
}
//Поиск минимального значения. WordSetID не может быть равен нулю. Только у каталогов без категории
minFolderID = subWordsets.get(0).getUnitSetId();//инициализация первым значением, чтобы было с чем сравнивать.
for(UnitSet tmpWordset : subWordsets){
if(tmpWordset.getUnitSetId() < minFolderID){
minFolderID = tmpWordset.getUnitSetId();
}
}
if (unitSet.getUnitSetId() > minFolderID) {
return false;
}
else {
return true;
}
}
private class SetsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<UnitSet> mUnitSets;
public SetsAdapter(List<UnitSet> unitSet){
mUnitSets = unitSet;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if(viewType==TYPE_SET_WITH_CATEGORY){
view = LayoutInflater.from(getActivity()).inflate(R.layout.category_item,parent, false);
return new SetWithCategoryHolder(view);
}
if(viewType==EMPTY_TYPE_SET_WITH_CATEGORY){
view = LayoutInflater.from(getActivity()).inflate(R.layout.empty_category_item,parent, false);
return new EmptySetWithCategoryHolder(view);
}
else{
view = LayoutInflater.from(getActivity()).inflate(R.layout.category_item_inner, parent, false);
return new SetWithoutCategoryHolder(view);
}
}
@Override
public int getItemViewType(int position) {
if (mUnitSets.get(position).getFolderId()==0&& isFirstWordSetFragmentInFolder(mUnitSets.get(position), mUnitSets)){
return EMPTY_TYPE_SET_WITH_CATEGORY;
}
else if(isFirstWordSetFragmentInFolder(mUnitSets.get(position), mUnitSets)){
return TYPE_SET_WITH_CATEGORY;
}
else{
return TYPE_SET_WITHOUT_CATEGORY;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if(getItemViewType(position)==TYPE_SET_WITH_CATEGORY){
((SetWithCategoryHolder)holder).bind(mUnitSets.get(position));
}
else if(getItemViewType(position)==EMPTY_TYPE_SET_WITH_CATEGORY){
((EmptySetWithCategoryHolder)holder).bind(mUnitSets.get(position));
}
else{
((SetWithoutCategoryHolder)holder).bind(mUnitSets.get(position));
}
}
@Override
public int getItemCount() {
return mUnitSets.size();
}
class SetWithCategoryHolder extends RecyclerView.ViewHolder{
private UnitSet mUnitSet;
private CardView mCardView;
private TextView mSetsName;
private TextView mCategoryName;
public SetWithCategoryHolder (@NonNull View itemView){
super(itemView);
mCardView = (CardView)itemView.findViewById(R.id.card_view_with_category);
mSetsName = (TextView)itemView.findViewById(R.id.set_name_with_category);
mCategoryName = (TextView)itemView.findViewById(R.id.category_name);
}
public void bind(UnitSet unitSet){
mUnitSet = unitSet;
mSetsName.setText(unitSet.getUnitSetName());
mCategoryName.setText(mPresenterLearnFragment.loadFolderNameById(unitSet.getFolderId()));
}
}
class EmptySetWithCategoryHolder extends RecyclerView.ViewHolder{
private UnitSet mUnitSet;
private CardView mCardView;
private TextView mSetsName;
public EmptySetWithCategoryHolder (@NonNull View itemView){
super(itemView);
mCardView = (CardView)itemView.findViewById(R.id.card_view_without_category);
mSetsName = (TextView)itemView.findViewById(R.id.set_name_without_category);
}
public void bind(UnitSet unitSet){
mUnitSet = unitSet;
mSetsName.setText(unitSet.getUnitSetName());
}
}
class SetWithoutCategoryHolder extends RecyclerView.ViewHolder{
private UnitSet mUnitSet;
private CardView mCardView;
private TextView mSetsName;
public SetWithoutCategoryHolder (@NonNull View itemView){
super(itemView);
mCardView = (CardView)itemView.findViewById(R.id.card_view_without_category);
mSetsName = (TextView)itemView.findViewById(R.id.set_name_without_category);
}
public void bind(UnitSet unitSet){
mUnitSet = unitSet;
mSetsName.setText(unitSet.getUnitSetName());
}
}
}
}