问题
On the home screen of my app i have a list of card views that i want to use as buttons to switch to their related fragment.
i want to do it similar to this instead of writing a onclick method for every card.
private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
Fragment fragment = null;
switch (e.Item.ItemId)
{
case Resource.Id.menu_home:
fragment = homeFragment;
break;
case Resource.Id.menu_favourites:
fragment = favFragment;
break;
case Resource.Id.menu_more:
fragment = moreFragment;
break;
}
FragmentTransaction fragmentTx = SupportFragmentManager.BeginTransaction();
fragmentTx.Replace(Resource.Id.container, fragment);
fragmentTx.Commit();
}
I am doing this inside the home fragment and my cardviews are the paceCard, wattsCard and weightadjustCard. So how can i implement this for my cards?
回答1:
Declare every CardView from your XML in java activity and put on them OnClickListener.
CardView cview1,cview2;
cview1=findViewById(R.id.cardViewOne);
cview1.setOnClickListener(......){
......}
回答2:
You can try this:
public void onClickCardView(View v){
switch(v.getId()){
case R.id.YOUR_ID:
//statement
break;
switch(v.getId()){
case R.id.YOUR_ID:
//statement
break;
switch(v.getId()){
case R.id.YOUR_ID:
//statement
break;
}
}
and in your xml give all the card onClick: like this
android:onClick="onClickCardView";
来源:https://stackoverflow.com/questions/52390899/use-card-views-as-buttons