【问题标题】:Passing ArrayList to Fragments将 ArrayList 传递给片段
【发布时间】:2015-11-09 17:11:42
【问题描述】:

我的 MainActivity 有两个片段(Tab1 和 Tab2)。 我已经通过 XML 解析 2 次从 URL 获取数据,并存储在 MainActivity 的 2 个不同的 ArrayList 中。

这两个片段有列表视图。现在我必须将 1 个 Arraylist 传递给一个片段,并将第 2 个 ArrayList 传递给第二个片段,然后将这些 ArrayList 显示到这两个片段的 ListViews 中。

我如何将 2 个 Arraylists 传递给这两个片段,以及如何在这两个片段中显示它们。

这是 MinActivity 的 onCreate 方法

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

    loadPage();
    // Creating The Toolbar and setting it as the Toolbar for the activity

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);


    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);

}

我在另一个函数(OnPostExecute)中有两个 ArrayList,我通过 XML 解析接收数据。

我没有在两个片段中编辑任何内容。到目前为止,我只是用列表视图创建了这两个片段

【问题讨论】:

    标签: java android android-fragments arraylist


    【解决方案1】:

    Fragments 有一个名为setArguments(Bundle) 的方法。 Bundle 内置了对各种ArrayList 的支持。您只需使用您选择的键将ArrayList 放入Bundle

    但是,我确实建议您遵循 newInstance(<parameters>) 模式,其中您的 Fragment 有一个 static newInstance() 方法,该方法接受 Fragment 运行所需的任何参数,在这种情况下是某种 ArrayList .

    引用official Fragment documentation中的一个例子:

    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();
    
        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);
    
        return f;
    }
    

    这个Bundle 可以稍后(在任何时候)检索,但通常在onCreate() 或类似中完成。上面存储的"index"可以通过如下方式获取:

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Bundle args = getArguments();
        if (args != null) {
            mIndex = args.getInt("index");
        }
    }
    

    在上面的示例中,参数是int,但ArrayList 可以遵循相同的逻辑。

    【讨论】:

    • 我会注意到,您可以通过调用getArguments() 在片段的onCreate() 方法中读取这些参数。所以在这种情况下int index = getArguments().getLong("index", 0); 0 是默认值。
    • @McAdams331 好主意,我已经相应地更新了答案。谢谢。
    • 我必须将 List 从 Activity 传递到 Fragment,所以我创建了详细片段的对象并在我拥有该 arrayList 的 Main Activity 方法中设置参数?正确的??谢谢!!!
    • @user3585510 这个答案是否解决了您的问题?如果是这样,您能否将其标记为答案。如果没有,请告诉我原因,我会看看能否进一步帮助您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多