gpt4 book ai didi

java - fragment 未完全膨胀

转载 作者:太空宇宙 更新时间:2023-11-04 10:50:50 24 4
gpt4 key购买 nike

我创建了一个具有三个选项卡的 Activity 。单击选项卡 1 时,它会打开一个包含两个按钮的 fragment (TaskFragment),每个按钮都有一个关联的文本。

当我单击“创建评估报告”按钮时,它会正确地膨胀另一个 fragment (AppraisalReportFragment) 的两个文本行(现在取代前两个文本行)和页面标题(评估报告),但两个初始按钮仍保留在原位。

我试图将初始 fragment 上的所有内容更改为全新的 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>

最佳答案

当您膨胀 fragment 时,在 Activity 中提交 FragmentManager 事务后,您应该将之前布局中的按钮的可见性设置为 gone

你可以这样做,然后在返回时再次将它们更改为visible

b1.setVisibility(View.GONE);
b2.setVisibility(View.GONE);

根据您的代码更改按钮的名称

关于java - fragment 未完全膨胀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47887823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com