Search in sources :

Example 1 with ViewItemDecoration

use of carbon.recycler.ViewItemDecoration in project Carbon by ZieIony.

the class BottomSheetLayout method updateRecycler.

private void updateRecycler() {
    if (menu == null)
        return;
    recycler.setLayoutManager(style == Style.List ? new LinearLayoutManager(getContext()) : new GridLayoutManager(getContext(), 3));
    ArrayList<Serializable> items = new ArrayList<>();
    for (int i = 0; i < menu.size(); i++) {
        if (menu.getItem(i).isVisible())
            items.add(new Item(menu.getItem(i)));
    }
    if (style == Style.List) {
        for (int i = 0; i < recycler.getItemDecorationCount(); i++) recycler.removeItemDecorationAt(0);
        ViewItemDecoration dividerItemDecoration = new ViewItemDecoration(getContext(), R.layout.carbon_menustrip_hseparator_item);
        dividerItemDecoration.setDrawAfter(position -> position < items.size() - 1 && items.get(position) instanceof NavigationView.Item && items.get(position + 1) instanceof NavigationView.Item && ((NavigationView.Item) items.get(position)).getGroupId() != ((NavigationView.Item) items.get(position + 1)).getGroupId());
        recycler.addItemDecoration(dividerItemDecoration);
        ViewItemDecoration paddingItemDecoration = new ViewItemDecoration(getContext(), R.layout.carbon_row_padding);
        paddingItemDecoration.setDrawAfter(position -> position == items.size() - 1);
        recycler.addItemDecoration(paddingItemDecoration);
    }
    RowListAdapter<Serializable> adapter = new RowListAdapter<>();
    adapter.putFactory(Item.class, style == Style.List ? BottomSheetRow::new : BottomSheetCell::new);
    adapter.setItems(items);
    recycler.setAdapter(adapter);
}
Also used : MenuItem(android.view.MenuItem) Serializable(java.io.Serializable) NavigationView(carbon.widget.NavigationView) GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) ViewItemDecoration(carbon.recycler.ViewItemDecoration) ArrayList(java.util.ArrayList) RowListAdapter(carbon.recycler.RowListAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Example 2 with ViewItemDecoration

use of carbon.recycler.ViewItemDecoration in project Carbon by ZieIony.

the class NavigationView method initItems.

private void initItems() {
    if (items == null)
        return;
    RowListAdapter<Serializable> adapter = new RowListAdapter<>(Item.class, itemFactory);
    adapter.putFactory(CustomHeaderItem.class, parent -> new CustomHeaderRow(header));
    adapter.setOnItemClickedListener(Item.class, (view, menuItem, position) -> {
        if (onItemClickedListener != null)
            onItemClickedListener.onItemClicked(view, menuItem, position);
    });
    List<Serializable> items = new ArrayList<>(Arrays.asList(this.items));
    for (int i = 0; i < getItemDecorationCount(); i++) removeItemDecorationAt(0);
    ViewItemDecoration dividerItemDecoration = new ViewItemDecoration(getContext(), R.layout.carbon_menustrip_hseparator_item);
    dividerItemDecoration.setDrawAfter(position -> position < items.size() - 1 && items.get(position) instanceof Item && items.get(position + 1) instanceof Item && ((Item) items.get(position)).getGroupId() != ((Item) items.get(position + 1)).getGroupId());
    addItemDecoration(dividerItemDecoration);
    ViewItemDecoration paddingItemDecoration = new ViewItemDecoration(getContext(), R.layout.carbon_row_padding);
    paddingItemDecoration.setDrawBefore(position -> position == 0);
    paddingItemDecoration.setDrawAfter(position -> position == items.size() - 1);
    addItemDecoration(paddingItemDecoration);
    if (header != null) {
        items.add(0, new CustomHeaderItem());
    }
    adapter.setItems(items);
    setAdapter(adapter);
}
Also used : MenuItem(android.view.MenuItem) Serializable(java.io.Serializable) ViewItemDecoration(carbon.recycler.ViewItemDecoration) ArrayList(java.util.ArrayList) RowListAdapter(carbon.recycler.RowListAdapter)

Example 3 with ViewItemDecoration

use of carbon.recycler.ViewItemDecoration in project Carbon by ZieIony.

the class SampleListActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences preferences = getSharedPreferences("samples", Context.MODE_PRIVATE);
    allPreferences = (Map<String, String>) preferences.getAll();
    RecyclerView recyclerView = findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new RowListAdapter<>();
    adapter.putFactory(SampleActivityGroup.class, parent -> new DataBindingComponent<>(parent, R.layout.row_main));
    adapter.putFactory(SampleActivityItem.class, parent -> {
        return new DataBindingComponent<SampleActivityItem>(parent, R.layout.row_sample) {

            @Override
            public void bind(SampleActivityItem data) {
                super.bind(data);
                getView().findViewById(R.id.star).setOnClickListener(v -> {
                    data.setStarred(!data.isStarred());
                    if (data.isStarred()) {
                        preferences.edit().putString(data.getActivityClass().getName(), getString(data.getName())).commit();
                    } else {
                        preferences.edit().remove(data.getActivityClass().getName()).commit();
                    }
                    for (int i = 0; i < adapter.getItemCount(); i++) {
                        if (adapter.getItems().get(i) == data) {
                            adapter.notifyItemChanged(i);
                            break;
                        }
                    }
                });
            }
        };
    });
    adapter.putFactory(String.class, parent -> new DataBindingComponent<>(parent, R.layout.row_description));
    adapter.setOnItemClickedListener((view, item, position) -> {
        if (item instanceof SampleActivityGroup) {
            Class activityClass = ((SampleActivityGroup) item).getActivityClass();
            Intent intent = new Intent(view.getContext(), activityClass);
            startActivity(intent);
        }
    });
    recyclerView.setAdapter(adapter);
    ViewItemDecoration itemDecoration = new ViewItemDecoration(this, R.layout.carbon_row_padding);
    itemDecoration.setDrawBefore(position -> position == 0);
    itemDecoration.setDrawAfter(position -> position == adapter.getItemCount() - 1);
    recyclerView.addItemDecoration(itemDecoration);
}
Also used : ViewItemDecoration(carbon.recycler.ViewItemDecoration) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) RecyclerView(carbon.widget.RecyclerView)

Aggregations

ViewItemDecoration (carbon.recycler.ViewItemDecoration)3 MenuItem (android.view.MenuItem)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 RowListAdapter (carbon.recycler.RowListAdapter)2 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)1 NavigationView (carbon.widget.NavigationView)1 RecyclerView (carbon.widget.RecyclerView)1