Exemplo n.º 1
0
  private void update(ArrayList<Category> newItems) {
    ArrayList<Category> arrayForRemoving = new ArrayList<>();
    for (int i = 0; i < publishItems.size(); i++) {
      Category category = publishItems.get(i);
      if (!newItems.contains(category)) {
        arrayForRemoving.add(category);
      }
    }
    for (Category category : arrayForRemoving) {
      publishItems.remove(category);
    }

    for (Category category : newItems) {
      if (publishItems.indexOf(category) == SortedList.INVALID_POSITION) {
        publishItems.add(category);
      }
    }
    if (publishItems.size() == 0) {
      if (currentFilter.equals(FILTER_ALL)) {
        callback.onEmpty(
            R.string.empty_no_categories_restart_app,
            R.drawable.ic_menu_gallery,
            R.string.button_restart_app,
            null);
      } else {
        callback.onEmpty(
            R.string.empty_no_favorite_categories,
            R.drawable.ic_menu_gallery,
            R.string.button_show_all_categories,
            null);
      }
    }
  }
Exemplo n.º 2
0
  public CategoriesAdapter(
      Context context, CategoryFavoriteCallback callback, ArrayList<Category> objects) {
    this.context = context;
    this.callback = callback;
    this.objects = objects;
    publishItems =
        new SortedList<>(
            Category.class,
            new SortedList.Callback<Category>() {
              @Override
              public boolean areContentsTheSame(Category oneItem, Category twoItem) {
                return oneItem.contentEquals(twoItem);
              }

              @Override
              public boolean areItemsTheSame(Category oneItem, Category twoItem) {
                return oneItem.equals(twoItem);
              }

              @Override
              public int compare(Category oneItem, Category twoItem) {
                return oneItem.compareTo(twoItem);
              }

              @Override
              public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
              }

              @Override
              public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
              }

              @Override
              public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
              }

              @Override
              public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
              }
            });
    for (Category category : objects) {
      publishItems.add(category);
    }
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    final View rootView =
        inflater.inflate(R.layout.fragment_recyclerview_sorted_list, container, false);
    final RecyclerView rcRecyclerView = (RecyclerView) rootView.findViewById(R.id.rvRecyclerView);
    final FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);

    rcRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    adapter =
        new RecyclerViewSortedListAdapter(
            sortedList,
            new View.OnLongClickListener() {
              @Override
              public boolean onLongClick(View v) {
                final int itemPosition = rcRecyclerView.getChildAdapterPosition(v);
                final String item = sortedList.get(itemPosition);

                View view =
                    LayoutInflater.from(getActivity())
                        .inflate(R.layout.dialog_recycler_view_modify, null, false);

                ListView lvListView = (ListView) view.findViewById(R.id.lvListView);
                lvListView.setAdapter(
                    new ArrayAdapter<String>(
                        getActivity(),
                        android.R.layout.simple_list_item_1,
                        android.R.id.text1,
                        new String[] {
                          "Rename", "Delete",
                        }));
                lvListView.setOnItemClickListener(
                    new AdapterView.OnItemClickListener() {
                      @Override
                      public void onItemClick(
                          AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) { // rename
                          final View view2 =
                              LayoutInflater.from(getActivity())
                                  .inflate(R.layout.dialog_recycler_view_add, null, false);
                          new AlertDialog.Builder(getActivity())
                              .setTitle("Rename " + item + " to")
                              .setView(view2)
                              .setPositiveButton(
                                  android.R.string.ok,
                                  new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                      EditText etRename =
                                          (EditText) view2.findViewById(R.id.etItem);
                                      sortedList.updateItemAt(
                                          itemPosition, etRename.getText().toString());
                                      dialog.dismiss();
                                    }
                                  })
                              .setNegativeButton(
                                  android.R.string.cancel,
                                  new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                      dialog.dismiss();
                                    }
                                  })
                              .create()
                              .show();
                        } else if (position == 1) { // delete
                          sortedList.removeItemAt(itemPosition);
                        }
                        mDialog.dismiss();
                      }
                    });

                mDialog =
                    new AlertDialog.Builder(getActivity()).setTitle(item).setView(view).create();
                mDialog.show();

                return false;
              }
            });
    sortedList.add("Banana");
    sortedList.add("Apple");
    rcRecyclerView.setAdapter(adapter);

    fab.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            final View view =
                LayoutInflater.from(getActivity())
                    .inflate(R.layout.dialog_recycler_view_add, null, false);
            new AlertDialog.Builder(getActivity())
                .setTitle("Add Item")
                .setView(view)
                .setNegativeButton(
                    android.R.string.cancel,
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                      }
                    })
                .setPositiveButton(
                    android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        EditText etItem = (EditText) view.findViewById(R.id.etItem);
                        sortedList.add(etItem.getText().toString());
                        dialog.dismiss();
                      }
                    })
                .create()
                .show();
          }
        });

    return rootView;
  }