我创建了一个具有三个标签的活动。单击选项卡1时,将打开一个包含两个按钮的片段(TaskFragment),每个按钮都有一个关联的文本。
当我单击“创建评估报告”按钮时,它会适当地使另一个Fragment的(AppraisalReportFragment)两个文本行(现在取代前两个)和页面标题(评估报告)膨胀,但两个初始按钮仍然保留。
我正在尝试将初始Fragment上的所有内容都更改为一个全新的Fragment,并带有自己的按钮和文本),但是现在仅发生了其中一些更改。
任务片段
package usjersey.com.jerseyscore.fragments;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import usjersey.com.jerseyscore.R;
/**
* Created by dhiggins on 11/20/2017.
*/
public class TasksFragment extends Fragment {
private static final String TAG = "TasksFragment";
Intent intent;
private RelativeLayout layoutToAdd;
// public void onCreate (Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
//}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tasks, container, false);
//View rootView = inflater.inflate(R.layout.fragment_tasks, null);
//layoutToAdd = (RelativeLayout) findViewById(R.id.TaskFragment);
Button ID = (Button) rootView.findViewById(R.id.btn_create_appraisal_rpt);
ID.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppraisalReportFragment appraisalRptAct = new AppraisalReportFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//fragmentTransaction.setTransition(fragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
//Fragment newFragment = new AppCompatDialogFragment();
//FragmentTransaction transaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.tasks_page_fragment, appraisalRptAct);
//transaction.replace(R.id.child_fragment, newFragment);
fragmentTransaction.commit();
}
});
Button ID2 = (Button) rootView.findViewById(R.id.btn_check_in_for_today);
ID2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TasksFragment checkInForToday = new TasksFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.herds_check_in_for_today_fragment4, checkInForToday);
//fragmentTransaction.setTransition(fragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//fragmentTransaction.addToBackStack(null);
//Fragment newFragment = new AppCompatDialogFragment();
//FragmentTransaction transaction = getFragmentManager().beginTransaction();
//transaction.replace(R.id.child_fragment, newFragment);
fragmentTransaction.commit();
}
});
/*rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intent = new Intent(getActivity(), AppraisalReportActivity.class);
final Button button = (Button) rootView.findViewById(R.id.button_create_appraisal_rpt);
}
});*/
return rootView;
}
/*public void buttonOnClick(View v){
}*/
}
fragment_tasks.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/tasks_page_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<!-- First A -->
<Button
android:id="@+id/btn_check_in_for_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="27dp"
android:layout_marginTop="44dp"
android:background="@android:color/black"
android:elevation="0dp"
android:onClick="onCreateView"
android:text="@string/btn_check_in_for_today"
android:textAlignment="viewStart"
android:textColor="@android:color/white"
android:textSize="18sp" />
<!-- First B -->
<Button
android:id="@+id/btn_create_appraisal_rpt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/btn_check_in_for_today"
android:layout_below="@+id/btn_check_in_for_today"
android:layout_marginTop="40dp"
android:background="@android:color/black"
android:onClick="onCreateView"
android:text="@string/btn_create_appraisal_report"
android:textColor="@android:color/white"
android:textSize="18sp" />
<!-- First B-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView3"
android:layout_below="@+id/btn_check_in_for_today"
android:text="@string/tasks_check_day_score"
android:textColor="@android:color/white"
android:id="@+id/textView2" />
<!-- First B-->
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_create_appraisal_rpt"
android:layout_centerHorizontal="true"
android:text="@string/tasks_print_email_regmail"
android:textColor="@android:color/white" />
</RelativeLayout>
**AppraisalReportFragment**
package usjersey.com.jerseyscore.fragments;
import android.app.Activity;
//import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import usjersey.com.jerseyscore.R;
/**
* Created by dhiggins on 11/29/2017.
*/
public class AppraisalReportFragment extends Fragment {
private static final String TAG = "Appraisal Report";
TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//return inflater.inflate(R.layout.fragment_information, container, false);
View view = inflater.inflate(R.layout.fragment_appraisal_report, container, false);
TextView output = (TextView)view.findViewById(R.id.textView3);
//output.setText("Frag 2");
return view;
}
}
fragment_appraisal.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/tasks_page_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<Button
android:id="@+id/btn_check_in_for_today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/appraisal_report"
android:layout_marginStart="27dp"
android:layout_marginTop="19dp"
android:background="@android:color/black"
android:text="@string/btn_all_cows_in_herd"
android:textColor="@android:color/white"
android:textSize="18sp" />
<Button
android:id="@+id/btn_cows_scored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/btn_check_in_for_today"
android:layout_below="@+id/textView5"
android:layout_marginTop="23dp"
android:background="@android:color/black"
android:onClick="onCreateView"
android:text="@string/btn_cows_scored"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView3"
android:layout_below="@+id/btn_check_in_for_today"
android:text="@string/label_all_cows_in_herd_report"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_cows_scored"
android:layout_centerHorizontal="true"
android:text="@string/label_checked_scored_cows"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/appraisal_report"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:fontFamily="serif"
android:text="@string/title_appraisal_reports"
android:textAlignment="viewStart"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
最佳答案
向片段充气时,在活动中提交FragmentManager的事务后,应将Visibility设置为上一布局中按钮的gone
您可以这样操作,然后在返回时再次将它们更改为visible
b1.setVisibility(View.GONE);
b2.setVisibility(View.GONE);
根据您的代码更改按钮的名称
关于java - fragment 未完全膨胀,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47887823/