use of com.thevarunshah.simplebucketlist.internal.SimpleItemTouchHelperCallback in project SimpleBucketList-material by thevarunshah.
the class BucketItemListView method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bucket_item_listview);
// fetch toolbar and set it as the action bar
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setContentInsetsRelative(72, 72);
setSupportActionBar(toolbar);
// check if tablet view is being used
boolean tablet = (findViewById(R.id.coordLayout_tablet) != null);
// obtain list view and create new bucket list custom adapter
recyclerView = findViewById(R.id.recycler_view);
recyclerAdapter = new BucketItemListAdapter(this, Utility.getBucketList(), tablet, this);
// attach adapter to list view
recyclerView.setAdapter(recyclerAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(recyclerAdapter);
itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(recyclerView);
// obtain add button and attach a on-tap listener to it
final FloatingActionButton addButton = findViewById(R.id.add_item);
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// inflate layout with customized alert dialog view
LayoutInflater layoutInflater = LayoutInflater.from(BucketItemListView.this);
final View dialog = layoutInflater.inflate(R.layout.input_dialog, null);
final AlertDialog.Builder newItemDialogBuilder = new AlertDialog.Builder(BucketItemListView.this, R.style.AppCompatAlertDialogStyle);
// customize alert dialog and set its view
newItemDialogBuilder.setTitle("New Item");
newItemDialogBuilder.setIcon(R.drawable.ic_launcher);
newItemDialogBuilder.setView(dialog);
// fetch and set up edittext
final EditText input = dialog.findViewById(R.id.input_dialog_text);
input.setHint("Enter Details");
input.setFocusableInTouchMode(true);
input.requestFocus();
// set up actions for dialog buttons
newItemDialogBuilder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int whichButton) {
// create new item
String itemText = input.getText().toString();
Item item = new Item(itemText);
// add item to main list and update view
Utility.getBucketList().add(item);
recyclerAdapter.notifyDataSetChanged();
// backup data
Utility.writeData(getApplicationContext());
}
});
newItemDialogBuilder.setNegativeButton("CANCEL", null);
// create and show the dialog
AlertDialog newItemDialog = newItemDialogBuilder.create();
newItemDialog.show();
// show keyboard
newItemDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
// add specific listeners only if tablet is not being used
if (!tablet) {
// moving fab out of the way when scrolling listview
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
// scrolling down
addButton.animate().translationY(addButton.getHeight() * 2);
} else {
// scrolling up
addButton.animate().translationY(0);
}
}
});
}
}
Aggregations