Makes Add Skill button a FloatingActionButton.
This commit is contained in:
		| @@ -1,11 +1,7 @@ | ||||
| package com.majinnaibu.monstercards.ui.editmonster; | ||||
|  | ||||
| import android.content.Context; | ||||
| import android.os.Bundle; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.Menu; | ||||
| import android.view.MenuInflater; | ||||
| import android.view.MenuItem; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
|  | ||||
| @@ -18,6 +14,7 @@ import androidx.navigation.Navigation; | ||||
| import androidx.recyclerview.widget.LinearLayoutManager; | ||||
| import androidx.recyclerview.widget.RecyclerView; | ||||
|  | ||||
| import com.google.android.material.floatingactionbutton.FloatingActionButton; | ||||
| import com.majinnaibu.monstercards.R; | ||||
|  | ||||
| /** | ||||
| @@ -25,46 +22,47 @@ import com.majinnaibu.monstercards.R; | ||||
|  */ | ||||
| public class EditSkillsFragment extends Fragment { | ||||
|     private EditMonsterViewModel mViewModel; | ||||
| //    private ViewHolder mHolder; | ||||
|     private ViewHolder mHolder; | ||||
|  | ||||
|     @Override | ||||
|     public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
|                              Bundle savedInstanceState) { | ||||
|         View view = inflater.inflate(R.layout.fragment_edit_skills_list, container, false); | ||||
|  | ||||
|         NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment); | ||||
|         NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.edit_monster_navigation); | ||||
|         mViewModel = new ViewModelProvider(backStackEntry).get(EditMonsterViewModel.class); | ||||
|         setHasOptionsMenu(true); | ||||
|  | ||||
|         // Set the adapter | ||||
|         if (view instanceof RecyclerView) { | ||||
|             Context context = view.getContext(); | ||||
|             RecyclerView recyclerView = (RecyclerView) view; | ||||
|             recyclerView.setLayoutManager(new LinearLayoutManager(context)); | ||||
|             mViewModel.getSkills().observe(getViewLifecycleOwner(), skills -> recyclerView.setAdapter(new EditSkillsRecyclerViewAdapter(mViewModel.getSkillsArray()))); | ||||
|         } | ||||
|         View root = inflater.inflate(R.layout.fragment_edit_skills_list, container, false); | ||||
|  | ||||
|         return view; | ||||
|         mHolder = new ViewHolder(root); | ||||
|         setupRecyclerView(mHolder.list); | ||||
|         setupAddSkillButton(mHolder.addSkill); | ||||
|  | ||||
|         return root; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { | ||||
|         inflater.inflate(R.menu.edit_skills_menu, menu); | ||||
|         super.onCreateOptionsMenu(menu, inflater); | ||||
|     private void setupRecyclerView(@NonNull RecyclerView recyclerView) { | ||||
|         recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); | ||||
|         mViewModel.getSkills().observe(getViewLifecycleOwner(), skills -> recyclerView.setAdapter(new EditSkillsRecyclerViewAdapter(mViewModel.getSkillsArray()))); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean onOptionsItemSelected(@NonNull MenuItem item) { | ||||
|         if (item.getItemId() == R.id.menu_action_add_skill) { | ||||
|     private void setupAddSkillButton(@NonNull FloatingActionButton fab) { | ||||
|         fab.setOnClickListener(view -> { | ||||
|             mViewModel.addNewSkill(); | ||||
|             // TODO: navigate to editing the new skill | ||||
| //            NavDirections action = MonsterDetailFragmentDirections.actionNavigationMonsterToEditMonsterFragment(monsterDetailViewModel.getId().getValue().toString()); | ||||
| //            View view = getView(); | ||||
| //            assert view != null; | ||||
| //            Navigation.findNavController(view).navigate(action); | ||||
|             return true; | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     private static class ViewHolder { | ||||
|         RecyclerView list; | ||||
|         FloatingActionButton addSkill; | ||||
|  | ||||
|         ViewHolder(View root) { | ||||
|             this.list = root.findViewById(R.id.list); | ||||
|             this.addSkill = root.findViewById(R.id.add_skill); | ||||
|         } | ||||
|         return super.onOptionsItemSelected(item); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -13,10 +13,8 @@ import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * {@link RecyclerView.Adapter} that can display a {@link Skill}. | ||||
|  * TODO: Replace the implementation with code for your data type. | ||||
|  */ | ||||
| public class EditSkillsRecyclerViewAdapter extends RecyclerView.Adapter<EditSkillsRecyclerViewAdapter.ViewHolder> { | ||||
|  | ||||
|     private final List<Skill> mValues; | ||||
|  | ||||
|     public EditSkillsRecyclerViewAdapter(List<Skill> items) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user