Adds ability to remove a skill.
This commit is contained in:
		| @@ -903,6 +903,12 @@ public class EditMonsterViewModel extends ViewModel { | ||||
|         newSkills.add(newSkill); | ||||
|         Collections.sort(newSkills, (skill1, skill2) -> skill1.name.compareToIgnoreCase(skill2.name)); | ||||
|         mSkills.setValue(newSkills); | ||||
|     } | ||||
|  | ||||
|     public void removeSkill(int position) { | ||||
|         List<Skill> skills = mSkills.getValue(); | ||||
|         ArrayList<Skill> newSkills = new ArrayList<>(skills); | ||||
|         newSkills.remove(position); | ||||
|         mSkills.setValue(newSkills); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -13,11 +13,13 @@ import androidx.navigation.NavBackStackEntry; | ||||
| import androidx.navigation.NavController; | ||||
| import androidx.navigation.Navigation; | ||||
| import androidx.recyclerview.widget.DividerItemDecoration; | ||||
| import androidx.recyclerview.widget.ItemTouchHelper; | ||||
| import androidx.recyclerview.widget.LinearLayoutManager; | ||||
| import androidx.recyclerview.widget.RecyclerView; | ||||
|  | ||||
| import com.google.android.material.floatingactionbutton.FloatingActionButton; | ||||
| import com.majinnaibu.monstercards.R; | ||||
| import com.majinnaibu.monstercards.ui.shared.SwipeToDeleteCallback; | ||||
|  | ||||
| /** | ||||
|  * A fragment representing a list of Items. | ||||
| @@ -54,6 +56,9 @@ public class EditSkillsFragment extends Fragment { | ||||
|  | ||||
|         DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, layoutManager.getOrientation()); | ||||
|         recyclerView.addItemDecoration(dividerItemDecoration); | ||||
|  | ||||
|         ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback(context, mViewModel::removeSkill)); | ||||
|         itemTouchHelper.attachToRecyclerView(recyclerView); | ||||
|     } | ||||
|  | ||||
|     private void setupAddSkillButton(@NonNull FloatingActionButton fab) { | ||||
|   | ||||
| @@ -16,9 +16,11 @@ import java.util.List; | ||||
|  */ | ||||
| public class EditSkillsRecyclerViewAdapter extends RecyclerView.Adapter<EditSkillsRecyclerViewAdapter.ViewHolder> { | ||||
|     private final List<Skill> mValues; | ||||
|     private final ItemCallback mOnDelete; | ||||
|  | ||||
|     public EditSkillsRecyclerViewAdapter(List<Skill> items) { | ||||
|     public EditSkillsRecyclerViewAdapter(List<Skill> items, ItemCallback onDelete) { | ||||
|         mValues = items; | ||||
|         mOnDelete = onDelete; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -37,6 +39,17 @@ public class EditSkillsRecyclerViewAdapter extends RecyclerView.Adapter<EditSkil | ||||
|         return mValues.size(); | ||||
|     } | ||||
|  | ||||
|     public void removeItem(int position) { | ||||
|         if (mOnDelete != null) { | ||||
|             Skill skill = mValues.get(position); | ||||
|             mOnDelete.onItemCallback(skill); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public interface ItemCallback { | ||||
|         void onItemCallback(Skill skill); | ||||
|     } | ||||
|  | ||||
|     public class ViewHolder extends RecyclerView.ViewHolder { | ||||
|         public final TextView mContentView; | ||||
|         public Skill mItem; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user