如何更新页面查看器中的所有页面?

时间:2015-06-02 10:37:35

标签: java android

我有一个页面查看器,其中有三个页面,其中包含ArrayLists,表示为"明天" "今天"和"昨天"。我还有一个抽屉,允许更改列表的设置。

当抽屉关闭时,我希望更新ListsViews(或整个页面片段)以显示在应用新设置后创建的三个新ArrayLists。

到目前为止,我设法能够更新"昨天"和"明天" (当滑到昨天,明天更新,反之亦然),我认为这是因为"今天"永远不会被摧毁。

无论哪种方式,我都希望在抽屉关闭后立即看到所有三个更新。

以下是我的适配器的代码:`

    private class MyPagerAdapter extends FragmentPagerAdapter {

    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
    ListFragmentOfTomorrow torrowFragment =new ListFragmentOfTomorrow();
    ListFragmentOfToday todayFragment = new ListFragmentOfToday();
    ListFragmentOfYesterday yestFragment = new ListFragmentOfYesterday();

    public MyPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos)
    {
     switch (pos) {

        case 0:
            torrowFragment.newInstance(tomorrowArrayList, MainActivity.this);
            registeredFragments.put(0, torrowFragment);
            return torrowFragment;
        case 1:
            todayFragment.newInstance(todayArrayList, MainActivity.this);
            registeredFragments.put(1, todayFragment);
            return todayFragment;
        case 2:
            yestFragment.newInstance(yesterdayArrayList, MainActivity.this);
            registeredFragments.put(2, yestFragment);
            return yestFragment;
        default: 
            todayFragment.newInstance(todayArrayList, MainActivity.this);
            registeredFragments.put(3, todayFragment);
            return todayFragment;
            }
    }

    @Override
    public int getCount() {
        return 3;
    }

    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }
}`

这是其中一个页面片段的代码(这三个基本相同):

public class ListFragmentOfToday extends Fragment
{
static ExpandListAdapter ExpAdapter;
static ExpandableListView expndList;
static Context context;
static ArrayList<Game> todayArrayList;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragment_list_of_today, container,  false);
    expndList = new ExpandableListView(context);
    expndList = (ExpandableListView)v.findViewById(R.id.FragmentedExpandableListView);
    ExpAdapter = new ExpandListAdapter(context,todayArrayList);
    expndList.setAdapter(ExpAdapter);
    return v;
}

public static ListFragmentOfToday newInstance(ArrayList<Game> todayArrayListIn, Context contextIn)
{
    ListFragmentOfToday todayFragment = new ListFragmentOfToday();
    context = contextIn;
    todayArrayList = todayArrayListIn;
    return todayFragment;
}

public void RefreshList(ArrayList<Game> todayArrayListIn)
{
    todayArrayList=todayArrayListIn;
    ExpAdapter.notifyDataSetChanged();
}
}

这是抽屉关闭时的代码:

    @Override
public void onDrawerClosed(View drawerView)
{
    //Here, the new ArrayLists are created(...)
    ListFragmentOfTomorrow tomorrowFragmentToUpdate = (ListFragmentOfTomorrow)pagerAdapter.getRegisteredFragment(0);
    tomorrowFragmentToUpdate.RefreshList(updatedTomorrowArrayList);
    ListFragmentOfToday todayFragmentToUpdate = (ListFragmentOfToday)pagerAdapter.getRegisteredFragment(1);
    todayFragmentToUpdate.RefreshList(updatedTodayArrayList);
    ListFragmentOfYesterday yesterdayFragmentToUpdate = (ListFragmentOfYesterday)pagerAdapter.getRegisteredFragment(2);
    yesterdayFragmentToUpdate.RefreshList(updatedYesterdayArrayList);
}

问题:如果抽屉关闭,我怎样才能让所有三个页面显示新的更新的arraylists?

作为一名新开发人员和新的StackOverflow用户,我还希望获得有关我的代码编写和问题格式的任何反馈。谢谢。

1 个答案:

答案 0 :(得分:0)

谢谢托马斯!那很有效。我尝试过notifyDataSetChanged()和getItemPosition(Object对象),但从不在一起。 dhke - 当我在寻找答案时,我看到了实例化的片段,但是理解了与newInstance()的区别......