延迟片段的onCreateView()

时间:2018-04-26 10:12:48

标签: java android android-fragments

我的代码通过加载程序从Internet获取数据,然后通过bundle传递到片段中,然后应该显示在片段的视图中。

问题是,在我的数据准备好之前,已经调用了fragement的 onCreateView() -Method,所以bundle参数为null。

如何推迟该过程,以便在调用 onCreateView()时数据准备就绪?

我尝试发布必要的代码,告诉我你是否需要更多代码。

PS。最初,我试图将整个 List NewsEntry 传递给片段,但是这种情况非常糟糕,因此我不得不将其转换为StringArray以便将其放入包中。如果你能告诉我一个更好的方法将这个列表传递给片段,我也将非常感激:)

谢谢

SpinnerActivity.java

@Override
public void onItemSelected(
        AdapterView<?> parent, 
        View view, 
        int position, 
        long id
) {
    mQuery = parent.getItemAtPosition(position).toString();
    buildQuery();
    loaderManager.destroyLoader(0);
    Loader loader = loaderManager.initLoader(0, null, this);
}

public void onLoadFinished(
        Loader<List<NewsEntry>> loader, 
        List<NewsEntry> fetchedNewsEntries
) {
    Bundle bundle = new Bundle();
    ArrayList<String> newsEntriesArrayList = new ArrayList<>();

    ...

    bundle.putStringArrayList("newsEntries", newsEntriesArrayList);
    NewsEntryFragment fragment = new NewsEntryFragment();
    fragment.setArguments(bundle);
}

NewsEntryFragment.java

public NewsEntryFragment() {}

public static void newInstance(int columnCount) {

    NewsEntryFragment fragment = new NewsEntryFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
}

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

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(
        @NonNull LayoutInflater inflater, 
        ViewGroup container,
        Bundle savedInstanceState
) {
    View view = inflater.inflate(R.layout.fragment_newsentry_list, container, false);

    if (getArguments() != null) {
        ArrayList<String> newsEntriesStringArrayList = 
            new ArrayList<>(
                Objects.requireNonNull( 
                    getArguments().getStringArrayList( "newsEntries" ) 
                )
            )
        ;

        ArrayList<NewsEntry> newsEntriesArrayList = new ArrayList<>();
            for (int i = 0; i < newsEntriesStringArrayList.size(); i++){
                StringBuilder builder = new StringBuilder();
                String sArray[];
                sArray = newsEntriesStringArrayList.get(i).split(",");
                NewsEntry newsEntry = 
                    new NewsEntry(
                        sArray[0], 
                        sArray[1], 
                        sArray[2], 
                        sArray[3], 
                        sArray[4]
                    )
                ;

                newsEntriesArrayList.add(newsEntry);
            }
    }


    // Set the adapter
    if (view instanceof RecyclerView) {
        Context context = view.getContext();
        RecyclerView recyclerView = (RecyclerView) view;
        if (mColumnCount <= 1) {
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
        }
        recyclerView
            .setAdapter(
                new MyNewsEntryRecyclerViewAdapter(
                    mNewsEntries, 
                    mListener
                )
            )
        ;
    }
    return view;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnListFragmentInteractionListener) {
        mListener = (OnListFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnListFragmentInteractionListener");
    }
}

activity_main.xml中

    ...

    <FrameLayout
    android:id="@+id/main_fragment_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

    <fragment android:name="com.example.bjonic.guardiannewsfeed.NewsEntryFragment"
        android:id="@+id/headlines_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

2 个答案:

答案 0 :(得分:0)

移动代码以从onCreateView()获取数据到onViewCreated() 并且为了发送arrayList你需要使NewsEntry类Parcelable然后你可以做以下事情 -

    args.putParcelableArrayList("list", yourList);

答案 1 :(得分:0)

在片段中进行接口回调,并在任务完成后触发接口