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;
}
}
Related
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();
}
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>
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>
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();
}
problem: I use ASP MVC Code First
One Base Model With 50 fields
100 SubModels extend from Base Model
Base Model:
public class SPBase
{
public int Id { get; set; }
public string ttt { get; set; }
public string s1 { get; set; }
public decimal d1 { get; set; }
public DateTime t1 { get; set; }
public int i1 { get; set; }
public string s2 { get; set; }
public decimal d2 { get; set; }
public DateTime t2 { get; set; }
public int i2 { get; set; }
public string s3 { get; set; }
public decimal d3 { get; set; }
public DateTime t3 { get; set; }
public int i3 { get; set; }
public string s4 { get; set; }
public decimal d4 { get; set; }
public DateTime t4 { get; set; }
public int i4 { get; set; }
public string s5 { get; set; }
public decimal d5 { get; set; }
public DateTime t5 { get; set; }
public int i5 { get; set; }
public string s6 { get; set; }
public decimal d6 { get; set; }
public DateTime t6 { get; set; }
public int i6 { get; set; }
public string s7 { get; set; }
public decimal d7 { get; set; }
public DateTime t7 { get; set; }
public int i7 { get; set; }
public string s8 { get; set; }
public decimal d8 { get; set; }
public DateTime t8 { get; set; }
public int i8 { get; set; }
public string s9 { get; set; }
public decimal d9 { get; set; }
public DateTime t9 { get; set; }
public int i9 { get; set; }
public string s10 { get; set; }
public decimal d10 { get; set; }
public DateTime t10 { get; set; }
public int i10 { get; set; }
public string s11 { get; set; }
public decimal d11 { get; set; }
public DateTime t11 { get; set; }
public int i11 { get; set; }
public string s12 { get; set; }
public decimal d12 { get; set; }
public DateTime t12 { get; set; }
public int i12 { get; set; }
public string s13 { get; set; }
public decimal d13 { get; set; }
public DateTime t13 { get; set; }
public int i13 { get; set; }
public string s14 { get; set; }
public decimal d14 { get; set; }
public DateTime t14 { get; set; }
public int i14 { get; set; }
public string s15 { get; set; }
public decimal d15 { get; set; }
public DateTime t15 { get; set; }
public int i15 { get; set; }
public string s16 { get; set; }
public decimal d16 { get; set; }
public DateTime t16 { get; set; }
public int i16 { get; set; }
public string s17 { get; set; }
public decimal d17 { get; set; }
public DateTime t17 { get; set; }
public int i17 { get; set; }
public string s18 { get; set; }
public decimal d18 { get; set; }
public DateTime t18 { get; set; }
public int i18 { get; set; }
public string s19 { get; set; }
public decimal d19 { get; set; }
public DateTime t19 { get; set; }
public int i19 { get; set; }
public string s20 { get; set; }
public decimal d20 { get; set; }
public DateTime t20 { get; set; }
public int i20 { get; set; }
public string s21 { get; set; }
public decimal d21 { get; set; }
public DateTime t21 { get; set; }
public int i21 { get; set; }
public string s22 { get; set; }
public decimal d22 { get; set; }
public DateTime t22 { get; set; }
public int i22 { get; set; }
public string s23 { get; set; }
public decimal d23 { get; set; }
public DateTime t23 { get; set; }
public int i23 { get; set; }
public string s24 { get; set; }
public decimal d24 { get; set; }
public DateTime t24 { get; set; }
public int i24 { get; set; }
public string s25 { get; set; }
public decimal d25 { get; set; }
public DateTime t25 { get; set; }
public int i25 { get; set; }
public string s26 { get; set; }
public decimal d26 { get; set; }
public DateTime t26 { get; set; }
public int i26 { get; set; }
public string s27 { get; set; }
public decimal d27 { get; set; }
public DateTime t27 { get; set; }
public int i27 { get; set; }
public string s28 { get; set; }
public decimal d28 { get; set; }
public DateTime t28 { get; set; }
public int i28 { get; set; }
public string s29 { get; set; }
public decimal d29 { get; set; }
public DateTime t29 { get; set; }
public int i29 { get; set; }
public string s30 { get; set; }
public decimal d30 { get; set; }
public DateTime t30 { get; set; }
public int i30 { get; set; }
public string s31 { get; set; }
public decimal d31 { get; set; }
public DateTime t31 { get; set; }
public int i31 { get; set; }
public string s32 { get; set; }
public decimal d32 { get; set; }
public DateTime t32 { get; set; }
public int i32 { get; set; }
public string s33 { get; set; }
public decimal d33 { get; set; }
public DateTime t33 { get; set; }
public int i33 { get; set; }
public string s34 { get; set; }
public decimal d34 { get; set; }
public DateTime t34 { get; set; }
public int i34 { get; set; }
public string s35 { get; set; }
public decimal d35 { get; set; }
public DateTime t35 { get; set; }
public int i35 { get; set; }
public string s36 { get; set; }
public decimal d36 { get; set; }
public DateTime t36 { get; set; }
public int i36 { get; set; }
public string s37 { get; set; }
public decimal d37 { get; set; }
public DateTime t37 { get; set; }
public int i37 { get; set; }
public string s38 { get; set; }
public decimal d38 { get; set; }
public DateTime t38 { get; set; }
public int i38 { get; set; }
public string s39 { get; set; }
public decimal d39 { get; set; }
public DateTime t39 { get; set; }
public int i39 { get; set; }
public string s40 { get; set; }
public decimal d40 { get; set; }
public DateTime t40 { get; set; }
public int i40 { get; set; }
public string s41 { get; set; }
public decimal d41 { get; set; }
public DateTime t41 { get; set; }
public int i41 { get; set; }
public string s42 { get; set; }
public decimal d42 { get; set; }
public DateTime t42 { get; set; }
public int i42 { get; set; }
public string s43 { get; set; }
public decimal d43 { get; set; }
public DateTime t43 { get; set; }
public int i43 { get; set; }
public string s44 { get; set; }
public decimal d44 { get; set; }
public DateTime t44 { get; set; }
public int i44 { get; set; }
public string s45 { get; set; }
public decimal d45 { get; set; }
public DateTime t45 { get; set; }
public int i45 { get; set; }
public string s46 { get; set; }
public decimal d46 { get; set; }
public DateTime t46 { get; set; }
public int i46 { get; set; }
public string s47 { get; set; }
public decimal d47 { get; set; }
public DateTime t47 { get; set; }
public int i47 { get; set; }
public string s48 { get; set; }
public decimal d48 { get; set; }
public DateTime t48 { get; set; }
public int i48 { get; set; }
public string s49 { get; set; }
public decimal d49 { get; set; }
public DateTime t49 { get; set; }
public int i49 { get; set; }
public string s50 { get; set; }
public decimal d50 { get; set; }
public DateTime t50 { get; set; }
public int i50 { get; set; }
}
SubModels:
public class SP2 : SPBase { }
public class SP3 : SPBase { }
public class SP4 : SPBase { }
public class SP5 : SPBase { }
public class SP6 : SPBase { }
public class SP7 : SPBase { }
public class SP8 : SPBase { }
public class SP9 : SPBase { }
public class SP10 : SPBase { }
public class SP11 : SPBase { }
public class SP12 : SPBase { }
public class SP13 : SPBase { }
public class SP14 : SPBase { }
public class SP15 : SPBase { }
public class SP16 : SPBase { }
public class SP17 : SPBase { }
public class SP18 : SPBase { }
public class SP19 : SPBase { }
public class SP20 : SPBase { }
public class SP21 : SPBase { }
public class SP22 : SPBase { }
public class SP23 : SPBase { }
public class SP24 : SPBase { }
public class SP25 : SPBase { }
public class SP26 : SPBase { }
public class SP27 : SPBase { }
public class SP28 : SPBase { }
public class SP29 : SPBase { }
public class SP30 : SPBase { }
public class SP31 : SPBase { }
public class SP32 : SPBase { }
public class SP33 : SPBase { }
public class SP34 : SPBase { }
public class SP35 : SPBase { }
public class SP36 : SPBase { }
public class SP37 : SPBase { }
public class SP38 : SPBase { }
public class SP39 : SPBase { }
public class SP40 : SPBase { }
public class SP41 : SPBase { }
public class SP42 : SPBase { }
public class SP43 : SPBase { }
public class SP44 : SPBase { }
public class SP45 : SPBase { }
public class SP46 : SPBase { }
public class SP47 : SPBase { }
public class SP48 : SPBase { }
public class SP49 : SPBase { }
public class SP50 : SPBase { }
public class SP51 : SPBase { }
public class SP52 : SPBase { }
public class SP53 : SPBase { }
public class SP54 : SPBase { }
public class SP55 : SPBase { }
public class SP56 : SPBase { }
public class SP57 : SPBase { }
public class SP58 : SPBase { }
public class SP59 : SPBase { }
public class SP60 : SPBase { }
public class SP61 : SPBase { }
public class SP62 : SPBase { }
public class SP63 : SPBase { }
public class SP64 : SPBase { }
public class SP65 : SPBase { }
public class SP66 : SPBase { }
public class SP67 : SPBase { }
public class SP68 : SPBase { }
public class SP69 : SPBase { }
public class SP70 : SPBase { }
public class SP71 : SPBase { }
public class SP72 : SPBase { }
public class SP73 : SPBase { }
public class SP74 : SPBase { }
public class SP75 : SPBase { }
public class SP76 : SPBase { }
public class SP77 : SPBase { }
public class SP78 : SPBase { }
public class SP79 : SPBase { }
public class SP80 : SPBase { }
public class SP81 : SPBase { }
public class SP82 : SPBase { }
public class SP83 : SPBase { }
public class SP84 : SPBase { }
public class SP85 : SPBase { }
public class SP86 : SPBase { }
public class SP87 : SPBase { }
public class SP88 : SPBase { }
public class SP89 : SPBase { }
public class SP90 : SPBase { }
public class SP91 : SPBase { }
public class SP92 : SPBase { }
public class SP93 : SPBase { }
public class SP94 : SPBase { }
public class SP95 : SPBase { }
public class SP96 : SPBase { }
public class SP97 : SPBase { }
public class SP98 : SPBase { }
public class SP99 : SPBase { }
public class SP100 : SPBase { }
DBContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<SP2> SP2 { get; set; }
public DbSet<SP3> SP3 { get; set; }
public DbSet<SP4> SP4 { get; set; }
public DbSet<SP5> SP5 { get; set; }
public DbSet<SP6> SP6 { get; set; }
public DbSet<SP7> SP7 { get; set; }
public DbSet<SP8> SP8 { get; set; }
public DbSet<SP9> SP9 { get; set; }
public DbSet<SP10> SP10 { get; set; }
public DbSet<SP11> SP11 { get; set; }
public DbSet<SP12> SP12 { get; set; }
public DbSet<SP13> SP13 { get; set; }
public DbSet<SP14> SP14 { get; set; }
public DbSet<SP15> SP15 { get; set; }
public DbSet<SP16> SP16 { get; set; }
public DbSet<SP17> SP17 { get; set; }
public DbSet<SP18> SP18 { get; set; }
public DbSet<SP19> SP19 { get; set; }
public DbSet<SP20> SP20 { get; set; }
public DbSet<SP21> SP21 { get; set; }
public DbSet<SP22> SP22 { get; set; }
public DbSet<SP23> SP23 { get; set; }
public DbSet<SP24> SP24 { get; set; }
public DbSet<SP25> SP25 { get; set; }
public DbSet<SP26> SP26 { get; set; }
public DbSet<SP27> SP27 { get; set; }
public DbSet<SP28> SP28 { get; set; }
public DbSet<SP29> SP29 { get; set; }
public DbSet<SP30> SP30 { get; set; }
public DbSet<SP31> SP31 { get; set; }
public DbSet<SP32> SP32 { get; set; }
public DbSet<SP33> SP33 { get; set; }
public DbSet<SP34> SP34 { get; set; }
public DbSet<SP35> SP35 { get; set; }
public DbSet<SP36> SP36 { get; set; }
public DbSet<SP37> SP37 { get; set; }
public DbSet<SP38> SP38 { get; set; }
public DbSet<SP39> SP39 { get; set; }
public DbSet<SP40> SP40 { get; set; }
public DbSet<SP41> SP41 { get; set; }
public DbSet<SP42> SP42 { get; set; }
public DbSet<SP43> SP43 { get; set; }
public DbSet<SP44> SP44 { get; set; }
public DbSet<SP45> SP45 { get; set; }
public DbSet<SP46> SP46 { get; set; }
public DbSet<SP47> SP47 { get; set; }
public DbSet<SP48> SP48 { get; set; }
public DbSet<SP49> SP49 { get; set; }
public DbSet<SP50> SP50 { get; set; }
public DbSet<SP51> SP51 { get; set; }
public DbSet<SP52> SP52 { get; set; }
public DbSet<SP53> SP53 { get; set; }
public DbSet<SP54> SP54 { get; set; }
public DbSet<SP55> SP55 { get; set; }
public DbSet<SP56> SP56 { get; set; }
public DbSet<SP57> SP57 { get; set; }
public DbSet<SP58> SP58 { get; set; }
public DbSet<SP59> SP59 { get; set; }
public DbSet<SP60> SP60 { get; set; }
public DbSet<SP61> SP61 { get; set; }
public DbSet<SP62> SP62 { get; set; }
public DbSet<SP63> SP63 { get; set; }
public DbSet<SP64> SP64 { get; set; }
public DbSet<SP65> SP65 { get; set; }
public DbSet<SP66> SP66 { get; set; }
public DbSet<SP67> SP67 { get; set; }
public DbSet<SP68> SP68 { get; set; }
public DbSet<SP69> SP69 { get; set; }
public DbSet<SP70> SP70 { get; set; }
public DbSet<SP71> SP71 { get; set; }
public DbSet<SP72> SP72 { get; set; }
public DbSet<SP73> SP73 { get; set; }
public DbSet<SP74> SP74 { get; set; }
public DbSet<SP75> SP75 { get; set; }
public DbSet<SP76> SP76 { get; set; }
public DbSet<SP77> SP77 { get; set; }
public DbSet<SP78> SP78 { get; set; }
public DbSet<SP79> SP79 { get; set; }
public DbSet<SP80> SP80 { get; set; }
public DbSet<SP81> SP81 { get; set; }
public DbSet<SP82> SP82 { get; set; }
public DbSet<SP83> SP83 { get; set; }
public DbSet<SP84> SP84 { get; set; }
public DbSet<SP85> SP85 { get; set; }
public DbSet<SP86> SP86 { get; set; }
public DbSet<SP87> SP87 { get; set; }
public DbSet<SP88> SP88 { get; set; }
public DbSet<SP89> SP89 { get; set; }
public DbSet<SP90> SP90 { get; set; }
public DbSet<SP91> SP91 { get; set; }
public DbSet<SP92> SP92 { get; set; }
public DbSet<SP93> SP93 { get; set; }
public DbSet<SP94> SP94 { get; set; }
public DbSet<SP95> SP95 { get; set; }
public DbSet<SP96> SP96 { get; set; }
public DbSet<SP97> SP97 { get; set; }
public DbSet<SP98> SP98 { get; set; }
public DbSet<SP99> SP99 { get; set; }
public DbSet<SP100> SP100 { get; set; }
}
It takes more than 10mins to launch when deploy to iis and even more time to launch when debug in Visual Studio 2013
I'v made some testing, seems the time cost is depending on the numbers of Models
So any suggestion is appreciated
You can try changing the database initializer in the Application_start method in the Global.asax.cs file.
Something along the lines of:
protected void Application_Start() {
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
...
}
but you should remember that this particular example would destroy all your data if you change the database model. If this proves to be efficient and works you could enable migrations and create a custom migration migration configuration and use it to update the db without losing all the data. For more info on migrations you could take a look here: http://www.codeproject.com/Articles/674760/Code-First-Migration-and-Extending-Identity-Accoun
Finally Solved!
Trick is:
upgrade the EntityFramework from 6.0.0.0 to 6.1.0