예제 #1
0
  public void clear() {
    this.departures.beginBatchedUpdates();

    while (departures.size() != 0) {
      departures.removeItemAt(0);
    }

    this.departures.endBatchedUpdates();
  }
예제 #2
0
  public ArrayList<Departure> getDepartures() {
    ArrayList<Departure> list = new ArrayList<>(departures.size());

    for (int i = 0; i < departures.size(); i++) {
      list.add(getItem(i));
    }

    return list;
  }
예제 #3
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);
      }
    }
  }
예제 #4
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);
    }
  }
예제 #5
0
  @Override
  public void onBindViewHolder(final ViewHolder holder, int position) {
    Category category = publishItems.get(position);

    ImageLoader.getInstance()
        .displayImage(category.getImageUrl(), holder.image, UIL.getImageOptions());

    holder.name.setText(category.getName());

    if (category.isFavorite()) {
      holder.favorite.setImageResource(R.mipmap.ic_star_amber_24dp);
    } else {
      holder.favorite.setImageResource(R.mipmap.ic_star_outline_amber_24dp);
    }
    holder.favorite.setTag(category);
    holder.favorite.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (!(v.getTag() instanceof Category))
              throw new RuntimeException("Category in tag instanceof error");
            Category shop = (Category) v.getTag();
            shop.setFavorite(!shop.isFavorite());
            notifyItemChanged(publishItems.indexOf(shop));
            shop.updateOrAddToDB();
            callback.onFavoriteChanged(shop);
          }
        });

    holder.setClickListener(
        new ViewHolder.ClickListener() {
          @Override
          public void onClick(View v, int position) {
            callback.onItemClick(v, position);
          }
        });
  }
  @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;
  }
예제 #7
0
 @Override
 public int getItemCount() {
   return publishItems.size();
 }
예제 #8
0
 public Departure getItem(int position) {
   return departures.get(position);
 }
예제 #9
0
 @Override
 public int getItemCount() {
   return departures == null ? 0 : departures.size();
 }