daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Oct 1st, 2018 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class UI_Fragment extends Fragment {
  2.  
  3.     private com.github.clans.fab.FloatingActionButton myFab;
  4.     private RecyclerView mRecyclerView;
  5.     private com.github.clans.fab.FloatingActionMenu mFabMenu;
  6.     private UI_Fragment_Adapter mAdapter;
  7.     private EditText mMachineName;
  8.     private static final String EXTRA_USER_ID = "com.example.nathan.exerciseCoach.user_id";
  9.     private User mUser;
  10.     private UserViewModel mUserViewModel;
  11.     private boolean happened;
  12.     private ArrayList<UserContents> mCustomObjects;
  13.  
  14.  
  15.     public static Intent newIntent(Context packageContext, ParcelUuid id) {
  16.         Intent intent = new Intent(packageContext, UI_Activity.class);
  17.         intent.putExtra(EXTRA_USER_ID, id);
  18.         return intent;
  19.     }
  20.  
  21.  
  22.     @Nullable
  23.     @Override
  24.     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  25.         View v = inflater.inflate(R.layout.activity_dashboard_ui_fragment, container, false);
  26.         setHasOptionsMenu(true);
  27.  
  28.         mMachineName = (EditText) v.findViewById(R.id.Machine_name);
  29.         myFab = (com.github.clans.fab.FloatingActionButton) v.findViewById(R.id.fab_newTypeItem);
  30.         mFabMenu = (com.github.clans.fab.FloatingActionMenu) v.findViewById(R.id.floatingMenu);
  31.         mRecyclerView = (RecyclerView) v.findViewById(R.id.myRecyclerView);
  32.  
  33.         ParcelUuid userID = (ParcelUuid) getActivity().getIntent().getParcelableExtra(EXTRA_USER_ID);
  34.  
  35.  
  36.         mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  37.         mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  38.             @Override
  39.             public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  40.                 if (dy > 0) {
  41.                     mFabMenu.hideMenuButton(true);
  42.                 } else if ((dy < 0))
  43.                     mFabMenu.showMenuButton(true);
  44.  
  45.             }
  46.         });
  47.         happened = false;
  48.         mUserViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
  49.         mUserViewModel.getUserItemById(userID).observe(this, new Observer<User>() {
  50.             @Override
  51.             public void onChanged(@Nullable final User testUser) {
  52.                 // Update the cached copy of the words in the adapter.
  53.                 if (testUser != null && !happened) {
  54.                     mUser = testUser;
  55.                     happened = true;
  56.                     doTheStuff();
  57.                     //recyclerview adapter
  58.                     mAdapter = new UI_Fragment_Adapter(getActivity(), mUser.getContents());
  59.  
  60.                     //set adpater for recyclerview
  61.                     mRecyclerView.setAdapter(mAdapter);
  62.                 }
  63.             }
  64.         });
  65.  
  66.         return v;
  67.     }
  68.  
  69.     private void doTheStuff() {
  70.         myFab.setOnClickListener(new View.OnClickListener() {
  71.             @Override
  72.             public void onClick(View v) {
  73.                 mCustomObjects.add(new UserContents(1));
  74.                 mUserViewModel.update(mUser);
  75.                 mAdapter.notifyDataSetChanged();
  76.             }
  77.         });
  78.         mMachineName.setText(mUser.getMachineName());
  79.         mMachineName.addTextChangedListener(new TextWatcher() {
  80.             @Override
  81.             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  82.  
  83.             }
  84.  
  85.             @Override
  86.             public void onTextChanged(CharSequence s, int start, int before, int count) {
  87.                 mUser.setMachineName(s.toString());
  88.                 mUserViewModel.update(mUser);
  89.             }
  90.  
  91.             @Override
  92.             public void afterTextChanged(Editable s) {
  93.             }
  94.         });
  95.     }
  96.  
  97.  
  98.     private class UI_Fragment_Adapter extends RecyclerView.Adapter<UI_Fragment_Adapter.CustomViewHolder> {
  99.         private Context mContext;
  100.  
  101.         public UI_Fragment_Adapter(Context context, ArrayList<UserContents> CustomObjects) {
  102.             mCustomObjects = CustomObjects;
  103.             this.mContext = context;
  104.  
  105.         }
  106.  
  107.         @Override
  108.         public int getItemViewType(int position) {
  109.             return mCustomObjects.get(position).getType();
  110.         }
  111.  
  112.         @Override
  113.         public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  114.             switch (viewType) {
  115.                 case 1:
  116.                     View viewONE = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_template_one, parent, false);
  117.                     CustomViewHolder rowONE = new CustomViewHolder(viewONE);
  118.                     return rowONE;
  119.  
  120. //                case 2:
  121. //                    View viewTWO = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_template_two, parent, false);
  122. //                    CustomViewHolder rowTWO = new CustomViewHolder(viewTWO);
  123. //                    return rowTWO;
  124.  
  125. //                case 3:
  126. //                    View viewTHREE = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_template_three, parent, false);
  127. //                    CustomViewHolder rowTHREE = new CustomViewHolder(viewTHREE);
  128. //                    return rowTHREE;
  129.  
  130.             }
  131.             return null;
  132.         }
  133.  
  134.  
  135.         @Override
  136.         public void onBindViewHolder(CustomViewHolder holder, int position) {
  137. //            UserContents dc_list = mUser.getContents().get(position);
  138.  
  139. //            final int pos = position * 3;
  140.  
  141.  
  142.         }
  143.  
  144.  
  145.         @Override
  146.         public int getItemCount() {
  147.             if (mCustomObjects != null)
  148.                 return mCustomObjects.size();
  149.             else
  150.                 return 0;
  151.         }
  152.  
  153.         class CustomViewHolder extends RecyclerView.ViewHolder {
  154.  
  155.             ImageView imgl;
  156.             TextView tvspecies;
  157.             FrameLayout container;
  158.  
  159.             public CustomViewHolder(View itemView) {
  160.                 super(itemView);
  161.  
  162.             }
  163.         }
  164.     }
  165.  
  166.     public void collapseFabs(MotionEvent event) {
  167.         if (mFabMenu.isOpened()) {
  168.             Rect outRect = new Rect();
  169.             mFabMenu.getGlobalVisibleRect(outRect);
  170.  
  171.  
  172.             if(!outRect.contains((int)(event.getRawX()),(int)(event.getRawY())))//if (outRect.contains((int) event.getRawX(), (int) event.getRawY()))
  173.                 mFabMenu.close(true);
  174.  
  175.         }
  176.     }
  177.  
  178.     public boolean closeFab() {
  179.         if (mFabMenu.isOpened()) {
  180.             mFabMenu.close(true);
  181.             return true;
  182.         } else
  183.             return false;
  184.     }
  185. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top