I want to delete items in recyclerview.
I expect change toolbar shape when i longclick.
I did that using visibility, it work sucessful but I'm not sure this way is right.
this is toolbar.xml using databinding.
<data>
<import type = "android.view.View" />
<variable
name = "isDeleteToolbar"
type = "boolean"/>
</data>
<android.support.v7.widget.Toolbar
android:id = "@+id/toolbar_search"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
>
<android.support.v7.widget.CardView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
cardView:cardElevation = "2dp"
>
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:gravity = "center"
android:orientation = "horizontal"
>
<ImageView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:src="@drawable/ic_back"
android:visibility = "@{ isDeleteToolbar ? View.VISIBLE : View.GONE }"
/>
<TextView
android:id = "@+id/text_toolbar_search"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:layout_weight = "1"
android:visibility = "@{ !isDeleteToolbar ? View.VISIBLE : View.GONE }"
/>
<TextView
android:id = "@+id/text_toolbar_search_count"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
android:visibility = "@{ isDeleteToolbar ? View.VISIBLE : View.GONE }"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.v7.widget.Toolbar>
it's fragment.xml
<android.support.constraint.ConstraintLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
>
<include
android:id = "@+id/toolbar"
style = "@style/ConstraintTop"
layout = "@layout/toolbar_search"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
/>
...
</android.support.constraint.ConstraintLayout>
and this is fragment. it handles recyclerview's item longclick event.
adapter.getPublishSubject()
.subscribe(data -> {
binding.toolbar.setIsDeleteToolbar(true);
});
Is there a better way to do this ?
Best solution is to Create Toolbar ActionMode
ActionMode mActionMode;
Menu context_menu;
ActionMode:
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = actionMode.getMenuInflater();
inflater.inflate(R.menu.muliselect, menu);
context_menu = menu;
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_delete:
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
mActionMode = null;
isMultiSelect = false;
selectEmailArrayList = new ArrayList<Email>();
refreshAdapter();
}
};
On ListView LongClick Listener do:
@Override
public void onItemLongClick(View view, int position) {
if (!isMultiSelect) {
selectEmailArrayList = new ArrayList<Email>();
isMultiSelect = true;
if (mActionMode == null) {
mActionMode = startActionMode(mActionModeCallback);
}
}
multi_select(position);
}
If you have implemented the Action mode in your reyclerview adapter, then this is how you set the toolbar colour when the user long presses and gets into the Action mode:
Place this code inside your activity theme in the values-v21/styles.xml file
<item name="actionModeBackground">@color/colorAccent</item>
Here @color/colorAccent is the reference to the colour that I want the toolbar colour to change to. Place your desired colour here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.