Xamarin.android Xamarin Android:如何在上下文操作模式中更改项目的位置
我已经实现了一个recyclerview,并在其中插入了上下文操作模式 在动作模式中,我删除了后退按钮并插入了一个复选框。我想将复选框的位置更改为上下文操作模式的开头,并将上下文操作模式的标题移到中间 如何更改这两个项目的位置 我希望实现如下图像所示的目标: 我的上下文操作模式:Xamarin.android Xamarin Android:如何在上下文操作模式中更改项目的位置,xamarin.android,Xamarin.android,我已经实现了一个recyclerview,并在其中插入了上下文操作模式 在动作模式中,我删除了后退按钮并插入了一个复选框。我想将复选框的位置更改为上下文操作模式的开头,并将上下文操作模式的标题移到中间 如何更改这两个项目的位置 我希望实现如下图像所示的目标: 我的上下文操作模式: public class MyActionMode : Java.Lang.Object, ActionMode.ICallback { private bool ischeck = false; p
public class MyActionMode : Java.Lang.Object, ActionMode.ICallback
{
private bool ischeck = false;
private Context mContext;
public static RecyclerView.Adapter mAdapter;
private int currentPosition;
private Button button;
private Button buttonTitle;
private Button buttonCheckbox;
public View mView;
private List<Email> mEmails;
public MyActionMode(Activity mActivity, Context context)
{
}
public MyActionMode(Context context, RecyclerView.Adapter adapter, int position, View v, List<Email> Emails)
{
mContext = context;
mAdapter = adapter;
currentPosition = position;
mView = v;
mEmails = Emails;
}
public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.itemTwoId:
// do Update
return true;
case Resource.Id.itemOneId:
// do Update
return true;
default:
return false;
}
}
public bool OnCreateActionMode(ActionMode mode, IMenu menu)
{
mode.MenuInflater.Inflate(Resource.Menu.ContextualMenu, menu);
button = (Button)menu.FindItem(Resource.Id.itemTwoId).ActionView;
button.Background = null;
button.TranslationX = 100;
var draw = ContextCompat.GetDrawable(mContext, Resource.Drawable.three_dots);
button.SetCompoundDrawablesWithIntrinsicBounds(draw, null, null, null);
button.Click += delegate
{
PopupMenu menu1 = new PopupMenu(mContext, button);
menu1.Inflate(Resource.Menu.popup_menu);
menu1.Show();
};
buttonCheckbox = (Button)menu.FindItem(Resource.Id.itemOneId).ActionView;
buttonCheckbox.Text = "Todos";
buttonCheckbox.TranslationY = -30;
buttonCheckbox.TextAlignment = TextAlignment.Center;
buttonCheckbox.Click += delegate
{
if (ischeck)
{
RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
mAdapter.removeSelection();
ischeck = false;
}
else
{
RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
mAdapter.checkall();
ischeck = true;
}
};
return true;
}
public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
{
return false;
}
public void OnDestroyActionMode(ActionMode mode)
{
RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
mAdapter.removeSelection();
mode.Dispose();
}
}
我能够解决这个问题,创建第二个工具栏,并将其用作上下文操作模式 axml的主要功能:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#E8E8E8"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckedTextView
android:text="Todos"
android:layout_width="60sp"
android:layout_height="20sp"
android:layout_marginTop="30sp"
android:textSize="15sp"/>
<CheckBox
android:layout_marginLeft="5sp"
android:layout_width="30sp"
android:layout_height="30sp"
android:id="@+id/checkBox2"
android:clickable ="true"/>
<TextView
android:id="@+id/Title"
android:textColor="#000"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Teste"
android:layout_marginLeft="120sp"
android:layout_marginTop="10sp"
android:textSize="25sp"/>
<Button
android:id="@+id/menubutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="345sp"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
BTW,在上下文操作模式下,如何在左侧放置复选框,而在右侧放置其他两个菜单?您可以发布一些代码片段吗?复选框位于左侧并不是有意的,当我在xml上声明复选框项时,它保留在左侧您可以尝试:ActionMode.setCustomView()
到自定义ActionMode。我尝试了ActionMode.setCustomView(),但当我声明:mode.CustomView.TextAlignment=TextAlignment.Center;是否将上下文操作模式替换为工具栏?是的,我创建了一个工具栏作为上下文操作模式。在此工具栏中,我可以将元素放置在需要的位置。此工具栏保持隐藏状态,当我长时间单击recyclerview项时,工具栏将显示为“完美”。恭喜。
if (mEmails[position].IsSelected())
{
CountAuxiliar++;
}
else
{
CountAuxiliar--;
}
mode.Title = CountAuxiliar.ToString() + " " + "Selecionados";
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#E8E8E8"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckedTextView
android:text="Todos"
android:layout_width="60sp"
android:layout_height="20sp"
android:layout_marginTop="30sp"
android:textSize="15sp"/>
<CheckBox
android:layout_marginLeft="5sp"
android:layout_width="30sp"
android:layout_height="30sp"
android:id="@+id/checkBox2"
android:clickable ="true"/>
<TextView
android:id="@+id/Title"
android:textColor="#000"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Teste"
android:layout_marginLeft="120sp"
android:layout_marginTop="10sp"
android:textSize="25sp"/>
<Button
android:id="@+id/menubutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="345sp"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>