android-fragments 我怎么才能让cardview消失呢?

rpppsulh  于 2022-11-14  发布在  Android
关注(0)|答案(3)|浏览(164)

当我单击导航抽屉中的任何菜单项时,菜单项的内容出现在卡片下面。卡片应该消失,然后菜单项的内容应该显示在它们的位置上。菜单项的内容以片段的形式写入。有人能帮助我解决这个问题吗?附加下面的代码...
下面是MainActivity Java文件

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    ////////////////////////////////////////////////////////////////////
    navigationView = (NavigationView)findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId())
            {
                case R.id.menu_profile:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new ProfileFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Profile");
                    menuItem.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.menu_language:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new LanguageFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Set Language");
                    menuItem.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
                case R.id.menu_tutorial:
                    fragmentTransaction=getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.main_container, new TutorialFragment());
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Tutorial");
                    menuItem.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;

            }
            return true;
        }
    });
}

下面是XML文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/drawer_layout"
    >
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
         android:id="@+id/main_container">
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"
            />
        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_fixed"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            card_view:cardCornerRadius="10dp"
            card_view:cardBackgroundColor="@android:color/background_light">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/card_fixed_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="Sensor Details"
                    android:fontFamily="serif"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    android:textColor="#000"/>
                <TextView
                    android:id="@+id/card_fixed_details"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:padding="5dp"
                    android:text="pH Level - \nTemperature - \nMoisture -"
                    android:fontFamily="serif"
                    android:textSize="16sp"
                    android:textColor="#000"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_notification"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            card_view:cardCornerRadius="10dp"
            card_view:cardBackgroundColor="@android:color/background_light">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/card_notification_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Water The Plants!"
                    android:fontFamily="serif"
                    android:textSize="24sp"
                    android:layout_gravity="center"
                    android:textStyle="bold"
                    android:textColor="#000"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Done"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:textStyle="bold"
                    android:fontFamily="serif"
                    android:layout_gravity="center_horizontal"
                    />
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_prediction"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            card_view:cardCornerRadius="10dp"
            card_view:cardBackgroundColor="@android:color/background_light">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/card_market_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Market Prediction"
                    android:fontFamily="serif"
                    android:textSize="24sp"
                    android:layout_gravity="center"
                    android:textStyle="bold"
                    android:textColor="#000"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Click Here!"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:textStyle="bold"
                    android:fontFamily="serif"
                    android:layout_gravity="center_horizontal"
                    android:onClick="marketPrediction"
                    />
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_pest"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            card_view:cardCornerRadius="10dp"
            card_view:cardBackgroundColor="@android:color/background_light">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/card_pest_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Pest Details"
                    android:fontFamily="serif"
                    android:textSize="24sp"
                    android:layout_gravity="center"
                    android:textStyle="bold"
                    android:textColor="#000"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Click Here!"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:textStyle="bold"
                    android:fontFamily="serif"
                    android:layout_gravity="center_horizontal"
                    android:onClick="pestDetails"
                    />
            </LinearLayout>
        </android.support.v7.widget.CardView>
     </LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/navigation_view"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/navigation_drawer_header"
        >
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
cld4siwp

cld4siwp1#

1.您需要获取CardView的ID并将其存储在变量中。
如果是,请使用以下命令:
1.在这样做之后,您需要将setVisibility转换为GONE,以便将CardView隐藏在任何您想隐藏的地方。
(视图.已删除);
1.每当你setVisibility到你的View时,有三种选择,
一个六个月一个月一个月一个月一个月一个月一个月七个月一个月一个月一个月八个月一个月
1.您可以使用setVisibility(View.VISIBLE)使CardView或任何其他Views重新出现。

  1. View.INVISIBLE会占用布局空间,但View.GONE不会占用任何空间。

(由于某种原因,我无法格式化代码以使CardViewGONE可见。)

oxf4rvwz

oxf4rvwz2#

每个Android View都有出现和消失的能力,只需在java代码中使用setVisibility()并将其设置为View.GONE即可
示例:

CardView cardView1 = findViewById(R.id.card_pest);

cardView1.setVisibility(View.GONE)
bfnvny8b

bfnvny8b3#

val cardView = findViewById<CardView>(R.id.cardview2)
val destroyer = findViewById<ImageView>(R.id.imageView8)
destroyer.setOnClickListener {
     cardView.visibility = View.GONE
}

相关问题