Xamarin Android : How to change the position of an item on the contextual action mode

I've implemented a recyclerview and inserted in it a contextual action mode.

In the action mode I removed the back button and inserted a checkbox. I want to change the position of the checkbox to be in the beginning of the contextual action mode and move the title of contextual action mode to the center.

How can I change the position of these two items?

I wish to achieve like the image below:

My contextual action mode:

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

}
My RecyclerView Adapter:

public class RecyclerAdapter : RecyclerView.Adapter, View.IOnClickListener, View.IOnLongClickListener
{
private View view;
private Boolean isSelected = false;

public Boolean IsSelected()
{
    return isSelected;
}

public void setSelected(Boolean selected)
{
    isSelected = selected;
}

public static RecyclerView.Adapter mAdapter;
public static bool isActionMode = true;
private int viewType;
private ViewGroup parent;
public static bool unselect = false;
private Activity mActivity;
private MyActionMode mActionMode;
private RecyclerView.ViewHolder holder;
private List<Email> mEmails;
private Context context;
private View p;
private ActionMode mode;
public static bool count = false;
public static int CountAuxiliar = 0;
private MyActionMode myActionMode;

public event EventHandler<int> ItemClick;
public RecyclerAdapter(List<Email> emails, Context context)
{
    mEmails = emails;
    this.context = context;
}

public RecyclerAdapter(List<Email> emails, Activity activity)
{
    mEmails = emails;
    mActivity = activity;
}

public RecyclerAdapter(List<Email> mEmails, MyActionMode myActionMode)
{
    this.mEmails = mEmails;
    this.myActionMode = myActionMode;
}

public class MyView : RecyclerView.ViewHolder
{
    public View mMainView { get; set; }
    public TextView mName { get; set; }
    public TextView mSubject { get; set; }
    public TextView mMessage { get; set; }

    public MyView(View view) : base(view)
    {
        mMainView = view;
    }

}

public override int ItemCount
{
    get { return mEmails.Count; }
}

public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
    LayoutInflater inflater = LayoutInflater.From(parent.Context);
    View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
    RecyclerViewHolder vh = new RecyclerViewHolder(row);
    return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
    RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
    myHolder.cbx.Visibility = mEmails[position].IsSelected() ? ViewStates.Visible : ViewStates.Gone;
    myHolder.cbx.Checked = mEmails[position].IsSelected();


    myHolder.mName.Text = mEmails[position].Name;
    myHolder.mSubject.Text = mEmails[position].Subject;
    myHolder.mMessage.Text = mEmails[position].Message;


    myHolder.ItemView.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
    myHolder.ItemView.Tag = position;
    myHolder.ItemView.SetOnClickListener(this);
    myHolder.ItemView.SetOnLongClickListener(this);

}

void View.IOnClickListener.OnClick(View v)
{
    if (CountAuxiliar > 0 && mode != null)
    {
        int position = (int)v.Tag;
        mEmails[position].setSelected(!mEmails[position].IsSelected());
        v.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
        v.FindViewById(Resource.Id.checkBox1).Visibility = mEmails[position].IsSelected() ? ViewStates.Visible : ViewStates.Invisible;
        if (mEmails[position].IsSelected())
        {
            CountAuxiliar++;
        }
        else
        {
            CountAuxiliar--;
        }
        mode.Title = CountAuxiliar.ToString() + " " + "Selecionados";
        Toast.MakeText(v.Context, "Click : " + CountAuxiliar + "---" + position, ToastLength.Short).Show();
    }
    if (CountAuxiliar < 1 && count == true)
    {
        count = false;
        mode.Finish();
        MainActivity.bottomnavigationview1.Visibility = ViewStates.Gone;
        MainActivity.floatinactionbutton1.Visibility = ViewStates.Visible;
    }
}
public void removeSelection()
{
    if (mEmails != null)
    {
        foreach (Email email in mEmails)
        {
            email.setSelected(false);

        }
    }
    MyActionMode.mAdapter.NotifyDataSetChanged();
    CountAuxiliar = 0;
    count = false;
    MainActivity.bottomnavigationview1.Visibility = ViewStates.Gone;
    MainActivity.floatinactionbutton1.Visibility = ViewStates.Visible;

}

public void checkall()
{
    if (mEmails != null)
    {
        foreach (Email email in mEmails)
        {
            email.setSelected(true);

        }
    }
    MyActionMode.mAdapter.NotifyDataSetChanged();
}


public bool OnLongClick(View v)
{

    if (CountAuxiliar < 1)
    {
        CountAuxiliar = 1;
        count = true;
        int position = (int)v.Tag;
        mEmails[position].setSelected(!mEmails[position].IsSelected());
        v.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
        MainActivity.bottomnavigationview1.Visibility = ViewStates.Visible;
        MainActivity.floatinactionbutton1.Visibility = ViewStates.Gone;
        v.FindViewById(Resource.Id.checkBox1).Visibility = mEmails[position].IsSelected() ? ViewStates.Visible : ViewStates.Invisible;
        mActionMode = new MyActionMode(mActivity, this, position, v, mEmails);
        mode = mActivity.StartActionMode(mActionMode);
        mode.Title = CountAuxiliar.ToString() + " " + "Selecionado";
        count = true;

        Toast.MakeText(v.Context, "Long Click : " + mEmails[position].IsSelected() + "---" + position, ToastLength.Short).Show();
    }
    return true;
}

}
The part of the code I am inserting the contextual action mode title:

if (mEmails[position].IsSelected())
{
CountAuxiliar++;
}
else
{
CountAuxiliar--;
}
mode.Title = CountAuxiliar.ToString() + " " + "Selecionados";

my popupmenu xml?

<?xml version="1.0" encoding="utf-8" ?>

my contextual action mode xml:
<?xml version="1.0" encoding="utf-8" ?>

mainactiity class:
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
public static RecyclerView mRecyclerView;
public static RecyclerView.LayoutManager mLayoutManager;
public RecyclerView.ViewHolder holder;
public static CheckBox checkbox1;
public static BottomNavigationView bottomnavigationview1;
public static FloatingActionButton floatinactionbutton1;
public int position;
public static RecyclerView.Adapter mAdapter;
private List mEmails;
private ActionMode mode;
DrawerLayout drawerLayout;
NavigationView navigationView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Essentials.Platform.Init(this, bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

        mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerViwer);
        bottomnavigationview1 = FindViewById<BottomNavigationView>
          (Resource.Id.bottom_navigation);
        bottomnavigationview1.NavigationItemSelected += BottomNavigation_NavigationItemSelected;


        floatinactionbutton1 = FindViewById<FloatingActionButton>
          (Resource.Id.fab);
        bottomnavigationview1.Visibility = ViewStates.Gone;
        mRecyclerView.AddItemDecoration(new DividerItemDecoration(mRecyclerView.Context, DividerItemDecoration.Vertical));
        mRecyclerView.HasFixedSize = true;
        SetupList();


        //Create our layout Manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.SetLayoutManager(mLayoutManager);
        RecyclerAdapter mAdapter = new RecyclerAdapter(mEmails, this);
        mRecyclerView.SetAdapter(mAdapter);


        SupportActionBar.SetDisplayHomeAsUpEnabled(true);
        SupportActionBar.SetDisplayShowTitleEnabled(true);
        SupportActionBar.SetHomeButtonEnabled(true);
        SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.menu_toolbar);
        drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Android.Resource.Id.Home:
                drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.Start);
                return true;
            case (Resource.Id.nav_messages):
                Toast.MakeText(this, "selecionando menu Medium", ToastLength.Short).Show();
                return true;

        }
        return base.OnOptionsItemSelected(item);
    }



    private void SetupList()
    {
        for (int i = 1; i <= 2; i++)
        {
            mEmails = new List<Email>();
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
            mEmails.Add(new Email() { Name = "tom", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });

        }
    }

    private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
    {
        LoadFragment(e.Item.ItemId);
    }

    public void LoadFragment(int id)
    {
        Android.Support.V4.App.Fragment fragment = null;
        switch (id)
        {
            case Resource.Id.big_puppies:
                Toast.MakeText(this, "selecionando menu Home", ToastLength.Short).Show();
                break;
        }

        if (fragment == null)
            return;

        SupportFragmentManager.BeginTransaction()
            .Replace(Resource.Id.content_frame, fragment)
            .Commit();
    }

    public override void OnBackPressed()
    {
        RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
        myHolder.cbx.Visibility = mEmails[position].IsSelected() ? ViewStates.Visible : ViewStates.Gone;
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    @TaniguchiSantos
    Have you solved the problem? If you are facing some issues while implementing, try to post the particular error with the corresponding codes here.

  • TaniguchiSantosTaniguchiSantos Member ✭✭✭

    @jarvan it worked
    sorry for late reply

Sign In or Register to comment.