EditText fire focus event inside RecycleView - xamarin.android

I have a EditText field inside a RecycleView.
When I add the OnFocusChange event listener the event doesn't fire when the text box receive focus.

When I add the OnFocusChange event listener the event doesn't fire when the text box receive focus.
You could set the OnFocusChangeListener in the OnBindViewHolder() method in the adapter.
For example:
public class MainActivity : AppCompatActivity
{
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
MyAdapter mAdapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(this);
mRecyclerView.SetAdapter(mAdapter);
}
public class MyViewHolder : RecyclerView.ViewHolder
{
public EditText Box1 { get; private set; }
public EditText Box2 { get; private set; }
public MyViewHolder(View itemView)
: base(itemView)
{
Box1 = itemView.FindViewById<EditText>(Resource.Id.Box1);
Box2 = itemView.FindViewById<EditText>(Resource.Id.Box2);
}
}
public class MyAdapter : RecyclerView.Adapter
{
Context context;
public MyAdapter(Context context)
{
this.context = context;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.RecyclerItem, parent, false);
MyViewHolder vh = new MyViewHolder(itemView);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
MyViewHolder vh = holder as MyViewHolder;
var box1 = vh.ItemView.FindViewById<EditText>(Resource.Id.Box1);
box1.OnFocusChangeListener = new MyOnFocusChangeListener(context);
}
public override int ItemCount
{
get { return 4; }
}
}
public class MyOnFocusChangeListener : Java.Lang.Object, IOnFocusChangeListener
{
Context context;
public MyOnFocusChangeListener(Context context)
{
this.context = context;
}
public void OnFocusChange(View v, bool hasFocus)
{
Toast.MakeText(context, "FocusChanged", ToastLength.Short).Show();
}
}
}
And the xaml of recyclerview item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/Box1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ABC"
android:textAlignment="center" />
<EditText
android:id="#+id/Box2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="DEF"
android:textAlignment="center" />
</LinearLayout>

Related

Jetpack compose LazyColumn+Coil-Compose doesn't work

Jetpack compose project
I use Coil-Compose rememberImagePainter
dependencies {
implementation 'io.coil-kt:coil-compose:1.4.0'
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ApplicationTheme {
Surface(color = MaterialTheme.colors.background) {
Image(
painter = rememberImagePainter("https://api.dujin.org/bing/1366.php"),
contentDescription = null
)
}
}
}
}
}
enter image description here
add LazyColumn doesn't work
…………
import coil.compose.rememberImagePainter
…………
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ApplicationTheme {
Surface(color = MaterialTheme.colors.background) {
+ LazyColumn {
+ items(10) {
Text(text = "test")
// doesn't work
Image(
painter = rememberImagePainter("https://api.dujin.org/bing/1366.php"),
contentDescription = null
)
+ }
+ }
}
}
}
}
}
enter image description here
Please help me
In LazyColumn or LazyRow is no problem with local resources, but network pictures you need to specify the width and height
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ApplicationTheme {
Surface(color = MaterialTheme.colors.background) {
LazyColumn {
items(10) {
Text(text = "test")
// doesn't work
Image(
painter = rememberImagePainter("https://api.dujin.org/bing/1366.php"),
contentDescription = null,
Modifier.fillMaxSize().height(300.dp)
)
}
}
}
}
}
}
}
if useful for you too, I've solved this problem with recovering images with Coil from remote resources putting in lazycolumn/lazyrow
modifier = Modifier.fillMaxHeight(0.5f),
with different percentage.

Xamarin Android: recyclerview deselect all when press the back button on contextual action mode don t work

Hello Everyone my name is Taniguchi.
I've created a recyclerview and inserted a contextual action mode.
when i select a item on my recyclerview the contextual action mode appears and there is no items selected the contextual action mode finishes.
But if a click on the back button on the contextual action mode the items stays selected.
My mainactivity class:
public class MainActivity : AppCompatActivity
{
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
public RecyclerView.ViewHolder holder;
public static CheckBox checkbox1;
public static BottomNavigationView bottomnavigationview1;
public static FloatingActionButton floatinactionbutton1;
public int position;
private RecyclerView.Adapter mAdapter;
private List<Email> mEmails;
private ActionMode mode;
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);
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);
}
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!!" });
}
}
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 bool unselect = false;
private Activity mActivity;
private MyActionMode mActionMode;
private List<Email> mEmails;
private Context context;
private View v;
private ActionMode mode;
public static bool count = false;
public static int CountAuxiliar = 0;
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 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();
if(MyActionMode.ismenuactivated == false){
myHolder.cbx.Visibility = ViewStates.Gone;
}
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 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);
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;
}
}
My Contextual Action Mode Class:
public class MyActionMode : Java.Lang.Object, ActionMode.ICallback
{
private Activity mActivity;
private RecyclerViewHolder holder;
private Context mContext;
private RecyclerView mView;
private RecyclerView.Adapter mAdapter;
private int currentPosition;
private Button button;
public View v;
public static bool ismenuactivated = true;
private IMenu menu;
private View menuItemView;
private List<Email> mEmails;
public CheckBox cbx;
public View itemView;
private RecyclerView mRecyclerView;
public MyActionMode(Context context) : this(context, null, 0)
{
}
public MyActionMode(Context context, RecyclerView.Adapter adapter, int position)
{
mContext = context;
mAdapter = adapter;
currentPosition = position;
}
public MyActionMode(List<Email> emails, Activity activity)
{
mEmails = emails;
mActivity = activity;
}
public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.itemOneId:
return true;
case Resource.Id.itemTwoId:
// 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;
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();
};
return true;
}
public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
{
return false;
}
public void OnDestroyActionMode(ActionMode mode)
{
mEmails[currentPosition].setSelected(false);
mView.SetBackgroundColor(Color.Transparent);
mView.FindViewById(Resource.Id.checkBox1).Visibility = ViewStates.Invisible;
mAdapter.NotifyItemChanged(currentPosition);
mode.Dispose();
}
The errors is occurring on the OnDestroyActionMode class:
In the lines:
mEmails[currentPosition].setSelected(false);
mView.SetBackgroundColor(Color.Transparent);
mView.FindViewById(Resource.Id.checkBox1).Visibility = ViewStates.Invisible;
mAdapter.NotifyItemChanged(currentPosition);
The error message says:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
When we press the back button in context action mode, we should deselect all the options, right?Then we can achieve like this:
In MyActionMode,modify the function OnDestroyActionMode as follows:
public void OnDestroyActionMode(ActionMode mode)
{
mAdapter.removeSelection();//removeSelection is a function of RecyclerAdapter
mode.Dispose();
}
And add function removeSelection in the RecyclerAdapter
public void removeSelection()
{
//assign all the value of `IsSelected` to false of mEmails list
if (mEmails!=null ) {
foreach (Email email in mEmails) {
email.setSelected(false);
}
}
NotifyDataSetChanged();
}
Meanwhile, if we want to deselect some special position Item, we can add the following method in RecyclerAdapter:
public void removeSpecialSelection(int position) {
mEmails[position].setSelected(false);
// other logic code
NotifyDataSetChanged();
}
And call above method in method OnDestroyActionMode
public void OnDestroyActionMode(ActionMode mode)
{
// mAdapter.removeSelection();
mAdapter.removeSpecialSelection(currentPosition);
mode.Dispose();
}

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<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();
}
}
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";
I was able to solve this problem creating a second toolbar and using as a contextual action mode
the main ativity 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>

Xamarin Android: Recyclerview multiple selection its not working

Hello everyone my name is Taniguchi.
I did a recyclerview and i trying to change the color of the row when its selected but when i click gives a timeout in this line inside the click function.
myHolder.ItemView.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
I trying to multiple select rows in recyclerview.
I followed this documentation How to implement multi-select in RecyclerView?
The documentation is in java and i am using C# but i thought it could would, but it didnt work.
My recyclerview adapter:
My model:
public class RecyclerAdapter : RecyclerView.Adapter
{
private View view;
private Boolean isSelected = false;
public Boolean IsSelected()
{
return isSelected;
}
public void setSelected(Boolean selected)
{
isSelected = selected;
}
private Activity mActivity;
private MyActionMode mActionMode;
private List<Email> mEmails;
private Context context;
private ActionMode mode;
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 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, OnClick, OnLongClick);
return vh;
}
void OnLongClick(object sender, View.LongClickEventArgs args, int position)
{
mActionMode = new MyActionMode(mActivity, this, position);
mode = mActivity.StartActionMode(mActionMode);
((View)sender).Selected = true;
selectedPosition = position;
NotifyDataSetChanged();
return;
}
int selectedPosition = -1;
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
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);
}
public RecyclerViewHolder myHolder;
void OnClick(int position)
{
mEmails[position].setSelected(!mEmails[position].IsSelected());
myHolder.ItemView.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
}
}
public class Email
{
public string Name { get; set; }
public string Subject { get; set; }
public string Message { get; set; }
private Boolean isSelected = false;
public Boolean IsSelected()
{
return isSelected;
}
public void setSelected(Boolean selected)
{
isSelected = selected;
}
}
RecyclerViewHolder vh = new RecyclerViewHolder(row, OnClick, OnLongClick);
i don't know what is defined in your RecyclerViewHolder
,so i only could give you a simple example:
RecyclerViewHolder.cs:
public class RecyclerViewHolder : RecyclerView.ViewHolder
{
public TextView mName;
public TextView mSubject;
public TextView mMessage;
private ItemClickListener itemClickListener;
public RecyclerViewHolder(View itemView) : base(itemView)
{
mName = itemView.FindViewById<TextView>(Resource.Id.textView1);
mSubject = itemView.FindViewById<TextView>(Resource.Id.textView2);
mMessage = itemView.FindViewById<TextView>(Resource.Id.textView3);
}
}
then in RecyclerAdapter :
class RecyclerAdapter : RecyclerView.Adapter,View.IOnClickListener, View.IOnLongClickListener
{
// add this variable
private List<Email> mEmails;
private Context context;
public RecyclerAdapter(List<Email> mEmails, Context context)
{
this.mEmails = mEmails;
this.context = context;
}
public override int ItemCount
{
get { return mEmails.Count; }
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
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);
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
RecyclerViewHolder vh = new RecyclerViewHolder(row);
return vh ;
}
public void OnClick(View v)
{
int position = (int)v.Tag;
mEmails[position].setSelected(!mEmails[position].IsSelected());
v.SetBackgroundColor(mEmails[position].IsSelected() ? Color.LightBlue : Color.Transparent);
Toast.MakeText(context, "Click : " + mEmails[position] + "---" + position, ToastLength.Short).Show();
}
public bool OnLongClick(View v)
{
int position = (int)v.Tag;
Toast.MakeText(v.Context, "Long Click : " + mEmails[position] + "---" + position, ToastLength.Short).Show();
return true;
}
}

how to access different entities of different models in same controller and view in asp mvc5 using ViewModel

i want to show dropdown list in view of design and dropdown list items will be get from Title from brands model.
Design model
public class Design
{
public int DesignId { get; set; }
public string Title { get; set; }
public string Desciption { get; set; }
public int? price { get; set; }
public bool Isdeleted { get; set; }
public string AddedBy { get; set; }
public DateTime? AddedDate { get; set; }
public string UpdatedBy { get; set; }
public DateTime? UpdatedDate { get; set; }
public int UserId { get; set; }
//temp data
public string brandTitle { get; set; }
//[MaxLength]
public string pictureLocation { get; set; }
}
Brand model
public class Brand
{
public int BrandId { get; set; }
public string Title { get; set; }
public bool Isdeleted { get; set; }
public string AddedBy { get; set; }
public DateTime? AddedDate { get; set; }
public string UpdatedBy { get; set; }
public DateTime? UpdatedDate { get; set; }
}
controller of Design
public ActionResult Create()
{
return View();
}
create view of Design.
#model E_Darzi.Models.Design
#Html.EditorFor(model => model.brandTitle, new { htmlAttributes = new { #class = "form-control" } })
access data in controller and send it to view
public ActionResult Index(){
var data = db.DesignBrandsVM.Include(d=>d.Brand).ToList();
returen view(data);
}
Save values into different entities
create vm class
public class DesignBrandsVM
{
public int DesignId { get; set; }
public string Title { get; set; }
public string Desciption { get; set; }
public int? price { get; set; }
public bool Isdeleted { get; set; }
public string AddedBy { get; set; }
public DateTime? AddedDate { get; set; }
public string UpdatedBy { get; set; }
public DateTime? UpdatedDate { get; set; }
public int BrandId { get; set; }
public string Title { get; set; }
public bool Isdeleted { get; set; }
public string AddedBy { get; set; }
public DateTime? AddedDate { get; set; }
public string UpdatedBy { get; set; }
public DateTime? UpdatedDate { get; set; }
}
Controller
assign values in post method and save into db
public ActionResult Create(DesignBrandsVM vm){
var Design = new Design()
{
Title = vm.Title,
Desciption = vm.Desciption,
price = vm.price,
Isdeleted = false
};
var Brand = new Brand()
{
Title = vm.BrandTitle,
Isdeleted = false
};
db.Design.Add(Design);
db.Brand.Add(Brand);
db.SaveChanges();
}

Resources