简体   繁体   中英

Use card views as buttons

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?

Declare every CardView from your XML in java activity and put on them OnClickListener.

CardView cview1,cview2;
cview1=findViewById(R.id.cardViewOne);
cview1.setOnClickListener(......){
......}

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM