我尝试从当前片段中打开一个新片段。但是当我这样做时,我遇到了一个问题,上一个片段中的按钮没有返回到新片段
我做了this,但对我来说却不是那么有益
片段容器XML
<fragment
android:id="@+id/fragment_container_navbar"
class="com.example.poplify.baby_guru_sample.Bottom_Tabs.Paytab_frag"
android:layout_width="match_parent"
android:layout_height="@dimen/_385sdp"
android:layout_marginEnd="@dimen/_2sdp"
android:layout_marginStart="@dimen/_2sdp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/toolbar1"
app:layout_constraintVertical_bias="0.97" />
将第一个片段替换为第二个
final Fragment fragment = new Reff_code_frag();
fragmentManager = getActivity().getSupportFragmentManager();
reff_code.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fragmentManager.beginTransaction().replace(R.id.fragment_container_navbar,fragment).addToBackStack("Paytab_frag").commit();
}
});
底部导航栏代码
private BottomNavigationView.OnNavigationItemSelectedListener navlistener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectFrag=null;
switch (menuItem.getItemId())
{
case R.id.tab1:
selectFrag = new Paytab_frag();
break;
case R.id.tab2 :
selectFrag = new Add_child_tab_frag();
break;
case R.id.tab3:
Toast.makeText(getApplicationContext(),"tab3",Toast.LENGTH_LONG).show();
break;
case R.id.tab4:
Toast.makeText(getApplicationContext(),"tab4",Toast.LENGTH_LONG).show();
break;
}
fragmentManager.beginTransaction().replace(R.id.fragment_container_navbar,selectFrag).commit();
return true;
}
};
第一个片段的xml
<android.support.constraint.ConstraintLayout 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"
android:background="@color/dark_grey"
tools:context=".Bottom_Tabs.Paytab_frag">
<TextView
android:id="@+id/textView"
android:layout_width="@dimen/_214sdp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="128dp"
android:gravity="center_horizontal"
android:lineSpacingExtra="@dimen/_4sdp"
android:text="@string/Paytxt1"
android:textSize="@dimen/_14sdp"
app:layout_constraintEnd_toStartOf="@+id/guideline6"
app:layout_constraintHorizontal_bias="0.531"
app:layout_constraintStart_toStartOf="@+id/guideline5"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
<EditText
android:id="@+id/amt_txt"
android:layout_width="@dimen/_150sdp"
android:layout_height="@dimen/_40sdp"
android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="20dp"
android:background="@drawable/button_with_border"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Enter Amount"
android:inputType="textPersonName"
android:paddingTop="@dimen/_10sdp"
app:layout_constraintEnd_toStartOf="@+id/guideline6"
app:layout_constraintHorizontal_bias="0.516"
app:layout_constraintStart_toStartOf="@+id/guideline5"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView2"
android:layout_width="@dimen/_214sdp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="21dp"
android:gravity="center_horizontal"
android:text="@string/paytxt2"
android:textSize="@dimen/_14sdp"
app:layout_constraintEnd_toStartOf="@+id/guideline6"
app:layout_constraintHorizontal_bias="0.531"
app:layout_constraintStart_toStartOf="@+id/guideline5"
app:layout_constraintTop_toBottomOf="@+id/amt_txt" />
<TextView
android:id="@+id/ref_code_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="25dp"
android:text="@string/refe_link"
app:layout_constraintEnd_toStartOf="@+id/guideline6"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="@+id/guideline5"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/pay_btn"
android:layout_width="@dimen/_215sdp"
android:layout_height="@dimen/_40sdp"
android:layout_marginBottom="@dimen/_8sdp"
android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="22dp"
android:background="@drawable/button_backgrond"
android:text="@string/pay"
android:textAllCaps="false"
android:textSize="@dimen/_18sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline6"
app:layout_constraintHorizontal_bias="0.545"
app:layout_constraintStart_toStartOf="@+id/guideline5"
app:layout_constraintTop_toBottomOf="@+id/ref_code_txt"
app:layout_constraintVertical_bias="0.04000002" />
第二个片段
<android.support.constraint.ConstraintLayout 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"
android:background="@drawable/frag_background"
tools:context=".Bottom_Tabs.Reff_code_frag">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="76dp"
android:text="@string/enter_ref"
app:layout_constraintEnd_toStartOf="@+id/guideline16"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="@+id/guideline15"
app:layout_constraintTop_toTopOf="@+id/guideline3" />
<EditText
android:id="@+id/ref_code_txt"
android:layout_width="@dimen/_150sdp"
android:layout_height="@dimen/_40sdp"
android:background="@drawable/button_with_border"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:paddingTop="@dimen/_10sdp"
android:gravity="center_horizontal"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/ref_txt_hint"
app:layout_constraintEnd_toStartOf="@+id/guideline16"
app:layout_constraintStart_toStartOf="@+id/guideline15"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="36dp"
android:text="@string/ref_code"
app:layout_constraintEnd_toStartOf="@+id/guideline16"
app:layout_constraintHorizontal_bias="0.48"
app:layout_constraintStart_toStartOf="@+id/guideline15"
app:layout_constraintTop_toBottomOf="@+id/ref_code_txt" />
<Button
android:id="@+id/apply_btn"
android:layout_width="@dimen/_215sdp"
android:layout_height="@dimen/_40sdp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="68dp"
android:background="@drawable/button_backgrond"
android:text="@string/apply"
android:textAllCaps="false"
app:layout_constraintEnd_toStartOf="@+id/guideline16"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="@+id/guideline15"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<Button
android:id="@+id/cancel_btn"
android:layout_width="@dimen/_215sdp"
android:layout_height="@dimen/_40sdp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/cancel"
android:textAllCaps="false"
android:background="@drawable/button_with_border"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline16"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="@+id/guideline15"
app:layout_constraintTop_toBottomOf="@+id/apply_btn"
app:layout_constraintVertical_bias="0.164" />