我的主要活动中有一个片段容器视图,我想在单击后退按钮时浏览片段
override fun onBackPressed() {
findNavController(R.id.fragmentContainerView2).popBackStack()
}
这是我用于背包导航的代码,但该应用程序不会在HOME片段上退出,如果我按第一个片段上的Back Back按钮,则该应用程序应关闭。如何做?
这是我的mainActivity.xml文件,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene2">
<ImageView
android:id="@+id/imageView7"
android:layout_width="60dp"
android:layout_height="50dp"
android:contentDescription="@string/logo"
android:src="@drawable/notification_icon"
app:layout_constraintBottom_toBottomOf="@+id/imageView8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView8" />
<ImageView
android:id="@+id/image8"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginStart="15dp"
android:background="@color/white"
android:contentDescription="@string/navigation_drawer"
android:src="@drawable/three_lines"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thehelfen"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation2" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="300dp"
android:layout_height="match_parent"
app:cardBackgroundColor="@color/white"
app:cardElevation="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="150dp"
android:clickable="true"
android:contentDescription="@string/logo"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/person"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/my_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/my_profile"
android:textColor="@color/black"
android:textSize="35sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logo" />
<ImageView
android:id="@+id/icon1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/consultnow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_profile" />
<TextView
android:id="@+id/consultnow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/consult_now"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon1"
app:layout_constraintStart_toEndOf="@+id/icon1"
app:layout_constraintTop_toTopOf="@+id/icon1" />
<ImageView
android:id="@+id/icon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/booklabtest"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon1" />
<TextView
android:id="@+id/booklabtest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/book_lab_test"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon2"
app:layout_constraintStart_toEndOf="@+id/icon2"
app:layout_constraintTop_toTopOf="@+id/icon2" />
<ImageView
android:id="@+id/icon3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/bookhealthpackage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon2" />
<TextView
android:id="@+id/bookhealth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/book_health_npackages"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon3"
app:layout_constraintStart_toEndOf="@+id/icon3"
app:layout_constraintTop_toTopOf="@+id/icon3" />
<ImageView
android:id="@+id/icon4"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/info_icon"
app:layout_constraintBottom_toTopOf="@+id/contact_icon"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/about_us"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon4"
app:layout_constraintStart_toEndOf="@+id/icon4"
app:layout_constraintTop_toTopOf="@+id/icon4" />
<ImageView
android:id="@+id/contact_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/contact_us"
app:layout_constraintBottom_toTopOf="@+id/icon5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/contact_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/contact_us"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/contact_icon"
app:layout_constraintStart_toEndOf="@+id/contact_icon"
app:layout_constraintTop_toTopOf="@+id/contact_icon" />
<ImageView
android:id="@+id/icon5"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/faq"
app:layout_constraintBottom_toTopOf="@+id/icon6"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/faq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/faqs"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon5"
app:layout_constraintStart_toEndOf="@+id/icon5"
app:layout_constraintTop_toTopOf="@+id/icon5" />
<ImageView
android:id="@+id/icon6"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/tandc"
app:layout_constraintBottom_toTopOf="@+id/icon7"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/termsandconditions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/terms_and_conditions"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon6"
app:layout_constraintStart_toEndOf="@+id/icon6"
app:layout_constraintTop_toTopOf="@+id/icon6" />
<ImageView
android:id="@+id/icon7"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/log_out"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/log_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/log_out"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon7"
app:layout_constraintStart_toEndOf="@+id/icon7"
app:layout_constraintTop_toTopOf="@+id/icon7" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.motion.widget.MotionLayout>
这是我的Motionscene文件,
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="500">
<KeyFrameSet>
<KeyAttribute
motion:motionTarget="@+id/cardView"
motion:framePosition="0"
android:translationX="-300dp"/>
<KeyAttribute
motion:motionTarget="@+id/cardView"
motion:framePosition="0"
android:translationX="0dp"/>
</KeyFrameSet>
<OnSwipe
motion:targetId="@+id/imageView8"
motion:autoCompleteMode="continuousVelocity"
motion:dragDirection="dragRight"
motion:maxAcceleration="20"
motion:maxVelocity="20"/>
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
motion:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
motion:layout_constraintEnd_toStartOf="@+id/imageView7"
android:id="@+id/textView5"
motion:layout_constraintStart_toEndOf="@+id/image8" />
<Constraint
android:id="@+id/image8"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="15dp"
android:background="@color/white"
android:src="@drawable/three_lines"
motion:altSrc="@drawable/cancel_icon"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
<Constraint
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintHorizontal_bias="0.5"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent"
motion:navGraph="@navigation/navigation2" />
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:visibility="invisible"
motion:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
motion:layout_constraintEnd_toStartOf="@+id/imageView7"
android:id="@+id/textView5"
motion:layout_constraintStart_toEndOf="@+id/image8" />
<Constraint
android:id="@+id/image8"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="300dp"
android:background="@color/white"
android:src="@drawable/three_lines"
motion:altSrc="@drawable/cancel_icon"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
<Constraint
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="250dp"
android:layout_height="750dp"
android:layout_marginTop="55dp"
android:layout_marginStart="400dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintHorizontal_bias="0.5"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent"
motion:navGraph="@navigation/navigation2" />
</ConstraintSet>
</MotionScene>
这是我的主要活动。KT文件
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val contactus = findViewById<TextView>(R.id.contact_us)
val drawer = findViewById<ImageView>(R.id.image8)
val tandc = findViewById<TextView>(R.id.termsandconditions)
val motionLayout = findViewById<MotionLayout>(R.id.motion_layout)
contactus.setOnClickListener {
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragmentContainerView2, contact_us_fragment())
commit()
motionLayout.transitionToStart()
}
}
drawer.setOnClickListener {
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}
tandc.setOnClickListener {
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragmentContainerView2, TandCFragment())
commit()
motionLayout.transitionToStart()
}
}
}
}
是否可以设置不同的导航方案,例如从about_us fragment到家庭片段等等
override fun onBackPressed() {
findNavController(R.id.fragmentContainerView2).popBackStack()
}
this is the code I'm using for backstack navigation but the app don't exit on home fragment, and if I press back button on first fragment, the app should close. How to do this?
This is My mainactivity.xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene2">
<ImageView
android:id="@+id/imageView7"
android:layout_width="60dp"
android:layout_height="50dp"
android:contentDescription="@string/logo"
android:src="@drawable/notification_icon"
app:layout_constraintBottom_toBottomOf="@+id/imageView8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView8" />
<ImageView
android:id="@+id/image8"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginStart="15dp"
android:background="@color/white"
android:contentDescription="@string/navigation_drawer"
android:src="@drawable/three_lines"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thehelfen"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation2" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="300dp"
android:layout_height="match_parent"
app:cardBackgroundColor="@color/white"
app:cardElevation="150dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="150dp"
android:clickable="true"
android:contentDescription="@string/logo"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/person"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/my_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/my_profile"
android:textColor="@color/black"
android:textSize="35sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logo" />
<ImageView
android:id="@+id/icon1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/consultnow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_profile" />
<TextView
android:id="@+id/consultnow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/consult_now"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon1"
app:layout_constraintStart_toEndOf="@+id/icon1"
app:layout_constraintTop_toTopOf="@+id/icon1" />
<ImageView
android:id="@+id/icon2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/booklabtest"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon1" />
<TextView
android:id="@+id/booklabtest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/book_lab_test"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon2"
app:layout_constraintStart_toEndOf="@+id/icon2"
app:layout_constraintTop_toTopOf="@+id/icon2" />
<ImageView
android:id="@+id/icon3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/bookhealthpackage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon2" />
<TextView
android:id="@+id/bookhealth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/book_health_npackages"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon3"
app:layout_constraintStart_toEndOf="@+id/icon3"
app:layout_constraintTop_toTopOf="@+id/icon3" />
<ImageView
android:id="@+id/icon4"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/info_icon"
app:layout_constraintBottom_toTopOf="@+id/contact_icon"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/about_us"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon4"
app:layout_constraintStart_toEndOf="@+id/icon4"
app:layout_constraintTop_toTopOf="@+id/icon4" />
<ImageView
android:id="@+id/contact_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/contact_us"
app:layout_constraintBottom_toTopOf="@+id/icon5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/contact_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/contact_us"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/contact_icon"
app:layout_constraintStart_toEndOf="@+id/contact_icon"
app:layout_constraintTop_toTopOf="@+id/contact_icon" />
<ImageView
android:id="@+id/icon5"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/faq"
app:layout_constraintBottom_toTopOf="@+id/icon6"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/faq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/faqs"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon5"
app:layout_constraintStart_toEndOf="@+id/icon5"
app:layout_constraintTop_toTopOf="@+id/icon5" />
<ImageView
android:id="@+id/icon6"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/tandc"
app:layout_constraintBottom_toTopOf="@+id/icon7"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/termsandconditions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/terms_and_conditions"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon6"
app:layout_constraintStart_toEndOf="@+id/icon6"
app:layout_constraintTop_toTopOf="@+id/icon6" />
<ImageView
android:id="@+id/icon7"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="25dp"
android:clickable="true"
android:contentDescription="@string/icon"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/log_out"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/log_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20sp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:text="@string/log_out"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/icon7"
app:layout_constraintStart_toEndOf="@+id/icon7"
app:layout_constraintTop_toTopOf="@+id/icon7" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.motion.widget.MotionLayout>
This is my motionscene file
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="500">
<KeyFrameSet>
<KeyAttribute
motion:motionTarget="@+id/cardView"
motion:framePosition="0"
android:translationX="-300dp"/>
<KeyAttribute
motion:motionTarget="@+id/cardView"
motion:framePosition="0"
android:translationX="0dp"/>
</KeyFrameSet>
<OnSwipe
motion:targetId="@+id/imageView8"
motion:autoCompleteMode="continuousVelocity"
motion:dragDirection="dragRight"
motion:maxAcceleration="20"
motion:maxVelocity="20"/>
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
motion:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
motion:layout_constraintEnd_toStartOf="@+id/imageView7"
android:id="@+id/textView5"
motion:layout_constraintStart_toEndOf="@+id/image8" />
<Constraint
android:id="@+id/image8"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="15dp"
android:background="@color/white"
android:src="@drawable/three_lines"
motion:altSrc="@drawable/cancel_icon"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
<Constraint
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintHorizontal_bias="0.5"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent"
motion:navGraph="@navigation/navigation2" />
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:visibility="invisible"
motion:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
motion:layout_constraintEnd_toStartOf="@+id/imageView7"
android:id="@+id/textView5"
motion:layout_constraintStart_toEndOf="@+id/image8" />
<Constraint
android:id="@+id/image8"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginStart="300dp"
android:background="@color/white"
android:src="@drawable/three_lines"
motion:altSrc="@drawable/cancel_icon"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent" />
<Constraint
android:id="@+id/fragmentContainerView2"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="250dp"
android:layout_height="750dp"
android:layout_marginTop="55dp"
android:layout_marginStart="400dp"
motion:layout_constraintBottom_toBottomOf="parent"
motion:layout_constraintEnd_toEndOf="parent"
motion:layout_constraintHorizontal_bias="0.5"
motion:layout_constraintStart_toStartOf="parent"
motion:layout_constraintTop_toTopOf="parent"
motion:navGraph="@navigation/navigation2" />
</ConstraintSet>
</MotionScene>
This is my main activity.kt file
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val contactus = findViewById<TextView>(R.id.contact_us)
val drawer = findViewById<ImageView>(R.id.image8)
val tandc = findViewById<TextView>(R.id.termsandconditions)
val motionLayout = findViewById<MotionLayout>(R.id.motion_layout)
contactus.setOnClickListener {
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragmentContainerView2, contact_us_fragment())
commit()
motionLayout.transitionToStart()
}
}
drawer.setOnClickListener {
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}
tandc.setOnClickListener {
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragmentContainerView2, TandCFragment())
commit()
motionLayout.transitionToStart()
}
}
}
}
Is it possible to set different navigation scenarios like from about_us fragment to home fragment and so on
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用导航组件和 NavHostFragment 时,您永远不会执行 FragmentTransaction。相反,您应该按照指南导航到导航中的目的地图(您在
NavHostFragment
上设置的navigation/navigation2.xml
。When using the Navigation Component and a
NavHostFragment
, you never, ever do aFragmentTransaction
. Instead you should follow the guide to navigate to a destination that is in your navigation graph (yournavigation/navigation2.xml
you've set on yourNavHostFragment
.