如何在项目长按时更改recyclerview中的工具栏?

时间:2018-02-12 13:32:59

标签: java android android-recyclerview


我想删除recyclerview中的项目。
当我长按时,我希望改变工具栏的形状 我使用可见性来做到这一点,它工作成功,但我不确定这种方式是否正确。

这是使用数据绑定的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的项目longclick事件。

    adapter.getPublishSubject()
            .subscribe(data -> {
                binding.toolbar.setIsDeleteToolbar(true);
            });

有更好的方法吗?

2 个答案:

答案 0 :(得分:0)

最佳解决方案是创建工具栏ActionMode

var getProperty = {!! json_encode($get_property) !!}

<强> ActionMode:

ActionMode mActionMode;
Menu context_menu;

在ListView LongClick Listener上执行:

 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();

    }
};

答案 1 :(得分:0)

如果您已在reyclerview适配器中实现了操作模式,那么这就是当用户长按并进入操作模式时设置工具栏颜色的方式:

将此代码放在您的活动主题的values-v21 / styles.xml文件

    <item name="actionModeBackground">@color/colorAccent</item>

此处@ color / colorAccent是我希望工具栏颜色更改为的颜色的引用。在这里放置您想要的颜色。