Search in sources :

Example 1 with FruitAdapter

use of tk.zielony.carbonsamples.applibrary.FruitAdapter in project Carbon by ZieIony.

the class QuickReturnActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quickreturn);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    recyclerView.setAdapter(new FruitAdapter(fruits, null));
    final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    recyclerView.addOnScrollListener(new android.support.v7.widget.RecyclerView.OnScrollListener() {

        int yscroll = 0;

        @Override
        public void onScrolled(android.support.v7.widget.RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (fab.getVisibility() == View.VISIBLE && fab.getAnimator() == null && yscroll > 50 * Carbon.getDip(getApplicationContext())) {
                yscroll = 0;
                fab.setVisibility(View.GONE);
                toolbar.setVisibility(View.GONE);
            }
            if (fab.getVisibility() != View.VISIBLE && fab.getAnimator() == null && yscroll < -50 * Carbon.getDip(getApplicationContext())) {
                yscroll = 0;
                fab.setVisibility(View.VISIBLE);
                toolbar.setVisibility(View.VISIBLE);
            }
            if (Math.signum(dy) != Math.signum(yscroll))
                yscroll = 0;
            yscroll += dy;
        }
    });
}
Also used : FruitAdapter(tk.zielony.carbonsamples.applibrary.FruitAdapter) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) FloatingActionButton(carbon.widget.FloatingActionButton) RecyclerView(carbon.widget.RecyclerView) Toolbar(carbon.widget.Toolbar)

Example 2 with FruitAdapter

use of tk.zielony.carbonsamples.applibrary.FruitAdapter in project Carbon by ZieIony.

the class RecyclerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

        @Override
        public int getMovementFlags(android.support.v7.widget.RecyclerView recyclerView, android.support.v7.widget.RecyclerView.ViewHolder viewHolder) {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
            return makeMovementFlags(dragFlags, swipeFlags);
        }

        @Override
        public boolean onMove(android.support.v7.widget.RecyclerView recyclerView, android.support.v7.widget.RecyclerView.ViewHolder viewHolder, android.support.v7.widget.RecyclerView.ViewHolder target) {
            Collections.swap(fruits, viewHolder.getAdapterPosition(), target.getAdapterPosition());
            fruitAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
            fruits.remove(viewHolder.getAdapterPosition());
            fruitAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
        }

        @Override
        public void onSelectedChanged(android.support.v7.widget.RecyclerView.ViewHolder viewHolder, int actionState) {
            super.onSelectedChanged(viewHolder, actionState);
        }
    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(recyclerView);
    fruitAdapter = new FruitAdapter(fruits, itemTouchHelper);
    recyclerView.setAdapter(fruitAdapter);
    recyclerView.setHeader(R.layout.header_scrollview);
}
Also used : ItemTouchHelper(carbon.recycler.ItemTouchHelper) FruitAdapter(tk.zielony.carbonsamples.applibrary.FruitAdapter) RecyclerView(carbon.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager)

Aggregations

LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 RecyclerView (carbon.widget.RecyclerView)2 FruitAdapter (tk.zielony.carbonsamples.applibrary.FruitAdapter)2 ItemTouchHelper (carbon.recycler.ItemTouchHelper)1 FloatingActionButton (carbon.widget.FloatingActionButton)1 Toolbar (carbon.widget.Toolbar)1