gpt4 book ai didi

java - 如何动态地使这个子 Fragment 的 TextView 可见/不可见?

转载 作者:搜寻专家 更新时间:2023-11-01 08:35:39 25 4
gpt4 key购买 nike

我有一个包含父 Fragment 的主 Activity,而父 Fragment 又包含一个子 Fragment。我正在尝试使子 Fragment 中的 TextView 动态可见/不可见。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This text belongs to the Activity"
android:id="@+id/textView"/>

<FrameLayout
android:id="@+id/parent_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

fragment 父类.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This text belongs to the parent fragment"
android:id="@+id/textView"/>


<FrameLayout
android:id="@+id/child_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

fragment_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This text belongs to the child fragment"
android:id="@+id/textView"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="But make this text visible dynamically!"
android:id="@+id/make_this_text_visible"
android:visibility="invisible"/>
</LinearLayout>

主 Activity .java

public class MainActivity extends AppCompatActivity {
public static final String PARENT_TAG = "parent_tag";
private ParentFragment mParentFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
mParentFragment = ParentFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.parent_container, mParentFragment, PARENT_TAG).commit();
}
else {
mParentFragment = (ParentFragment) getSupportFragmentManager().findFragmentByTag(PARENT_TAG);
}
}
}

父 fragment .java

public class ParentFragment extends Fragment {
public static final String CHILD_TAG = "child_tag";
private ChildFragment mChildFragment;
private List<Integer> mList;

public static ParentFragment newInstance() {

Bundle args = new Bundle();

ParentFragment fragment = new ParentFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_parent, container, false);
mList = new ArrayList<Integer>();

if (savedInstanceState == null) {
mChildFragment = ChildFragment.newInstance();
getChildFragmentManager().beginTransaction().replace(R.id.child_container, mChildFragment, CHILD_TAG).commit();
}
else {
mChildFragment = (ChildFragment) getChildFragmentManager().findFragmentByTag(CHILD_TAG);
}
getChildFragmentManager().executePendingTransactions(); //doesn't seem to do anything!
doStuff();

return view;
}

void doStuff() {
mList.add(4); //pretend this is actually querying a database.
//for simplicity it just receives a single 4.

if (mList.size() > 0) { //the list is not empty, display the text!
mChildFragment.setTextVisible(); //error! the textivew of the child fragment is null right now
}
else {
mChildFragment.setTextInvisible(); //error! the textivew of the child fragment is null right now
}
}
}

子 fragment .java

public class ChildFragment extends Fragment {
private TextView mTextView;

public static ChildFragment newInstance() {

Bundle args = new Bundle();

ChildFragment fragment = new ChildFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_child, container, false);
mTextView = (TextView) view.findViewById(R.id.make_this_text_visible);
return view;
}

public void setTextVisible() {
mTextView.setVisibility(View.VISIBLE);
}
public void setTextInvisible() {
mTextView.setVisibility(View.INVISIBLE);
}
}

如何确保在调用 ParentFragment 中的 doStuff() 时子 fragment 已形成?

最佳答案

我要做的是保持 TextView 可见性的状态,以便在创建 View 后(如果尚未创建)可以正确更新它。 setTextVisible()setTextInvisible 没有单独的方法,而是使用单个方法 setTextVisibile(boolean isVisible) 并按如下方式实现它:

public class ChildFragment extends Fragment {
private TextView mTextView;
private boolean mIsTextVisible;

public static ChildFragment newInstance() {
Bundle args = new Bundle();
ChildFragment fragment = new ChildFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_child, container, false);
mTextView = (TextView) view.findViewById(R.id.make_this_text_visible);
setTextVisible(mIsTextVisible);

return view;
}

public void setTextVisible(boolean isVisible) {
mIsTextVisible = isVisible;
if(mTextView != null) {
mTextView.setVisibility(isVisible ? View.VISIBLE : View.GONE);
}
}
}

然后在父 fragment 中,您可以调用 doStuff(),而不必担心自 mIsTextVisible 以来 ChildFragment 中 View 的当前状态设置正确。如果在调用 setTextVisible()mTextView 为 null,可见性仍将在 onCreateView() 中正确设置。

在重新创建 fragment (例如设备旋转时)时,只需注意保存和恢复 mIsTextVisible 标志。


使用回调的替代答案

使用回调更新ParentFragment来实现ChildFragment.OnViewCreatedListener 及其方法。

public class ParentFragment extends Fragment
implements ChildFragment.OnViewCreatedListener {

@Override
public void onViewCreated() {
doStuff();
}
}

然后在ChildFragment

public class ChildFragment extends Fragment {
public interface OnViewCreatedListener {
void onViewCreated();
}

public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_child, container, false);
mTextView = (TextView) view.findViewById(R.id.make_this_text_visible);

if(getParentFragment() instanceof OnViewCreatedListener) {
((OnViewCreatedListener) getParentFragment()).onViewCreated();
} else if (getActivity() instanceof OnViewCreatedListener) {
((OnViewCreatedListener) getActivity()).onViewCreated();
}

return view;
}
}

关于java - 如何动态地使这个子 Fragment 的 TextView 可见/不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37017131/

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