Use card views as buttons

大城市里の小女人 提交于 2019-12-11 18:14:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!