如何在recyclerview中使用水平滚动获得工作日?
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.widget_list);
adapter = new MyAdapter(this,getList());
LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(horizontalLayoutManager);
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (isMultiSelect){
//if multiple selection is enabled then select item on single click else perform normal click on item.
multiSelect(position);
}
}
@Override
public void onItemLongClick(View view, int position) {
if (!isMultiSelect){
selectedIds = new ArrayList<>();
isMultiSelect = true;
if (actionMode == null){
actionMode = startActionMode(MainActivity.this); //show ActionMode.
}
}
multiSelect(position);
}
}));
}
private void multiSelect(int position) {
MyData data = adapter.getItem(position);
if (data != null){
if (actionMode != null) {
if (selectedIds.contains(data.getId()))
selectedIds.remove(Integer.valueOf(data.getId()));
else
selectedIds.add(data.getId());
if (selectedIds.size() > 0)
actionMode.setTitle(String.valueOf(selectedIds.size())); //show selected item count on action mode.
else{
actionMode.setTitle(""); //remove item count from action mode.
actionMode.finish(); //hide action mode.
}
adapter.setSelectedIds(selectedIds);
}
}
}
/**
* @see MyData Create dummy List of type MyData.
* @return list
*/
String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays();
private List<MyData> getList(){
List<MyData> list = new ArrayList<>();
list.add(new MyData(1,"Mon"));
list.add(new MyData(2,"Tue"));
list.add(new MyData(3,"Wed"));
list.add(new MyData(4,"Thu"));
list.add(new MyData(5,"Fri"));
list.add(new MyData(6,"Sat"));
list.add(new MyData(7,"Sun"));
return list;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_select, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.action_delete:
//just to show selected items.
StringBuilder stringBuilder = new StringBuilder();
for (MyData data : getList()) {
if (selectedIds.contains(data.getId()))
stringBuilder.append("\n").append(data.getTitle());
}
Toast.makeText(this, "Selected items are :" + stringBuilder.toString(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
isMultiSelect = false;
selectedIds = new ArrayList<>();
adapter.setSelectedIds(new ArrayList<Integer>());
}