从BackStack返回后,ListFragment加载空列表

时间:2014-05-23 02:21:42

标签: android android-fragments android-listfragment back-stack android-nested-fragment

这是我在同一件事上的第二篇文章,我还在等人指导我。我面临的问题可能非常简单,简而言之,我有这个ListFragment填充列表调用webservice,这很好。当用户选择任何项目时,我会替换另一个片段并将其添加到BackStack

问题从这里开始,它弹出ListFragment,但它总是空的或空白的。但我已经以各种可能的方式检查了日志,发现数据存在,由于某种原因,列表保持空白。请帮帮我...

在MainActivity中,我使用DrawerItemCustomAdapter,这就是我替换片段的方式

private void selectItem(int position) {

    android.support.v4.app.Fragment fragment = null;

    switch (position) {
    case 0:
    {
        fragment = new EventFragment();
    }
        break;
    case 1:
        fragment = new ScheduleFragment();
        break;
    case 2:
        fragment = new NewsFragment();
        break;
    case 3:
        fragment = new TeamFragment();
        break;
    case 4:
        fragment = new VenueFragment();
        break;
    case 5:
        fragment = new NewsFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();  

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }

}
在EventFragment中

我使用FragmentStatePagerAdapter创建ViewPager

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.event_fragment, container, false);

    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

    viewpager = (ViewPager)rootView.findViewById(R.id.pager);

    viewpager.setAdapter(new MyAdapter(fragmentManager));

    return rootView;
}

和MyAdapter类

    class MyAdapter extends FragmentStatePagerAdapter {

        FragmentManager fragmentManager;
    public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
            fragmentManager = fm;
        }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;

        if(i == 0)
        {
            fragment = new LiveFragment();
        }
        if(i == 1)
        {
            fragment = new UpcomingFragment();
        }
        if(i == 2)
        {
            fragment = new CompletedFragment();
        }

        return fragment;
    }
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 3;
}

@Override
    public CharSequence getPageTitle(int position) {
        if(position == 0)
        {
            return "Live";
        }
        if(position == 1)
        {
            return "Up-Coming";
        }
        if(position == 2)
        {
            return "Completed";
        }

        return null;
    }
    }

所有三个标签都是ListFragment,我们选择第一个

@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {

                LiveMatchList = new ArrayList<HashMap<String, Object>>();
                adapter = null;
                Log.d("LiveMatchList setUserVisibleHint","setUserVisibleHint");
                new LoadUpcomingMatchList().execute();          
        }
    }

我的LoadUpcomingMatchList类扩展了AsyncTask,在onPreExecute和doInBackground之后我设置了这样的适配器

protected void onPostExecute(String file_url) {
            try
            {
                if (pDialog != null) { 
                    pDialog.dismiss();
               }
                //pDialog.dismiss();
                /**
                 * Updating parsed JSON data into ListView
                 * */
                adapter = new SimpleAdapter(
                        getActivity(),LiveMatchList,
                        R.layout.live_list_row, new String[] { TAG_DateTimeLabel, "eventName", TAG_roundName, TAG_Venue, "team1Flag", "team2Flag" },
                        new int[] { R.id.livematchDate, R.id.livematchName, R.id.liveround, R.id.livevenue, R.id.liveteam1flag, R.id.liveteam2flag });
                // updating listview
                setListAdapter(adapter);
                Log.d("LiveMatchList onPostExecute",String.valueOf(LiveMatchList));
            }
            catch(Exception ex)
            {
                Log.d("onPostExecute", ex.toString());
            }

        }

我已经实现onListItemClick以从MainActivity转到详细信息页面,就像你建议的那样。

@Override
    public void onListItemClick(ListView l, View v, int position, long id) { 

        @SuppressWarnings("unchecked")
        HashMap<String,String> selectedItem = (HashMap<String, String>) getListView().getItemAtPosition(position);
        Bundle dataBundle = new Bundle();
        dataBundle.putString("team1Id", selectedItem.get(TAG_homeTeamId));
        dataBundle.putString("team2Id", selectedItem.get(TAG_awayTeamId));


        ((MainActivity)getActivity()).doTransaction("LiveFragment", dataBundle);

        Fragment fragment = new LiveMatchDetailMainFragment();
        fragment.setArguments(dataBundle);
        android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, fragment);
        fragmentTransaction.addToBackStack("LiveFragment");
        fragmentTransaction.commit();
    }

详细信息页面再次是实现了viewPager的片段。现在,当我按下详细页面上的后退按钮时,它会按预期加载前一个片段,并且我已经检查了日志,我可以看到setUserVisibleHint被调用,并且选项卡按预期加载但是选项卡中没有任何内容,应该有一个列表。请参考图表,如果有帮助,非常感谢你的帮助。我已经在这个问题上浪费了3/4天。

   [MainActivity]
         |
         V
   [EventFragment]
   |     |      | 
   V     V      V
[Tab1] [Tab2] [Tab3]

[Tab1]是一个ListFragment实现onListItemClick - &gt;细节片段。

1 个答案:

答案 0 :(得分:6)

您在ListFragment中进行片段交易吗?在FragmentActivity中执行此操作并使用onListItemClick()回调方法调用FragmentActivity方法,该方法应运行事务。

在EventFragment中替换:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
viewpager.setAdapter(new MyAdapter(fragmentManager));

with:

viewpager.setAdapter(new MyAdapter(getChildFragmentManager()));