gpt4 book ai didi

android - 在渲染之前将数据从 Layout 传递到 Fragment

转载 作者:行者123 更新时间:2023-11-30 00:54:11 27 4
gpt4 key购买 nike

我需要将数据传递给 fragment ,在渲染之前在 Activity 布局中声明:

<fragment android:name="com.app.fragments.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myFragment" />

我尝试在 Activity 的 onCreate 中执行此操作,但 fragment 已在此时呈现。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Here fragment is already rendered
mPosts = (MyFragment)getSupportFragmentManager().findFragmentById(R.id.myFragment);
mPosts.setData(someData);
}

我已经看到了在 onCreate Activity 中以编程方式创建 fragment 并将其添加到容器中的方式。这不是一个非常糟糕的解决方案。但是……还有更简单的吗?

最佳答案

对于 XML 布局,这里有一个问题:https://stackoverflow.com/a/23226281/842607

甚至他们不得不说你不能传递参数,但你可以使用回调或自定义属性。

否则

通常,我们以这种方式在参数中使用Bundle:

fragment 类

public class ShopProductListingFragment extends Fragment {

private static final String TAG = ShopProductListingFragment.class.getSimpleName();

public static final String KEY_CATEGORY = "category";
public static final String KEY_URL_PARAMS = "url_params";
public static final String KEY_URL = "url";

public static ShopProductListingFragment getInstance(NewCategory category, @NonNull HashMap<String, String> urlParams) {
Bundle bundle = new Bundle();
if (null != category)
bundle.putLong(KEY_CATEGORY, category.getCategory_id());
bundle.putSerializable(KEY_URL_PARAMS, urlParams);

ShopProductListingFragment fragment = new ShopProductListingFragment();
fragment.setArguments(bundle);
return fragment;
}

public static ShopProductListingFragment getInstance(@NonNull String url) {
Bundle bundle = new Bundle();
bundle.putSerializable(KEY_URL, url);

ShopProductListingFragment fragment = new ShopProductListingFragment();
fragment.setArguments(bundle);
return fragment;
}

public ShopProductListingFragment() {
// Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initVals();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_shop_product_listing, container, false);
ButterKnife.bind(this, rootView);
initViews();
return rootView;
}

private void initVals() {
Bundle bundle = getArguments();

if (null == bundle || Bundle.EMPTY == bundle)
throw new NullPointerException("Invalid or null arguments passed to fragment ShopProductListingFragment");

if (bundle.containsKey(KEY_CATEGORY))
categoryId = bundle.getLong(KEY_CATEGORY);
mCategory = StyFi.getInstance().getCategory(categoryId);
if (bundle.containsKey(KEY_URL_PARAMS))
urlParams = (HashMap<String, String>) bundle.getSerializable(KEY_URL_PARAMS);
if (bundle.containsKey(KEY_URL))
urlFilter = bundle.getString(KEY_URL);
}

}

Activity的布局文件

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

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

这是在Activity中实例化的方式

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

if (null == savedInstanceState) {
ShopProductListingFragment fragment = ShopProductListingFragment.getInstance(category, getUrlParams(data));

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, "SHOP");
fragmentTransaction.commit();
}
}

关于android - 在渲染之前将数据从 Layout 传递到 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40446074/

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