从viewpager

时间:2015-07-25 15:18:40

标签: android android-fragments android-viewpager baseadapter multichoiceitems

我使用baseadapter创建了一个自定义列表视图。列表项包含multichoice的purpouse的复选框。我在baseadapter中添加了一个返回所选项目位置的函数。

为了catogarize listitems,我尝试将listview放在Fragments中,使用带有FragmentPagerAdapter的Viewpager.Now每个Fragment都有自己的multichoice listview。用户需要能够在页面之间滑动并选择项目然后最终移动到下一个活动我必须将所有选定的项目位置收集到二维列表或数组中。

MyProblem:  似乎Viewpager在当前页面的两侧只在内存中维护了2个页面。如果我的viewpager在一个角落或者有超过3个页面,我就无法收集某些片段中所选项目的列表。

我将不胜感激。

1 个答案:

答案 0 :(得分:0)

当用户离开时,让每个Fragment将所选项目保存到SharedPreferences。稍后让您的其他ActivitySharedPreferences检索项目。

int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
{
    if (checked.get(i)) 
    {
        // Get the item
        // Deal with the checked item. 
        // ex: add to ArrayList
        arrayList.add(item);
    }
}

Set<String> set = new HashSet<String>();
set.addAll(arrayList);
mSharedPreferences.edit().putStringSet("key", set).apply();
相关问题