![](/img/trans.png)
[英]How to change items' properties in RecyclerView when chosing a specified item programatically?
[英]How to change toolbar in recyclerview when item is longclicked?
我想刪除recyclerview中的項目。
我希望在我單擊longclick時更改工具欄形狀。
我使用可見性做到了這一點,但工作成功,但是我不確定這種方式是否正確。
這是使用數據綁定的toolbar.xml。
<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>
這是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>
這是片段。 它處理recyclerview的item longclick事件。
adapter.getPublishSubject()
.subscribe(data -> {
binding.toolbar.setIsDeleteToolbar(true);
});
有一個更好的方法嗎 ?
最好的解決方案是創建工具欄ActionMode
ActionMode mActionMode;
Menu context_menu;
動作模式:
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();
}
};
在ListView LongClick Listener上執行以下操作:
@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);
}
如果您已經在reyclerview適配器中實現了“動作”模式,那么當用戶長按並進入“動作”模式時,這是設置工具欄顏色的方式:
將此代碼放在values-v21 / styles.xml文件中的活動主題內
<item name="actionModeBackground">@color/colorAccent</item>
這里@ color / colorAccent是我希望工具欄顏色更改為的顏色的引用。 在此處放置所需的顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.