2

我正在尝试使用 PagerSlidingTabStrip 而不是 TabView 来实现 viewpager。viewpager 具有三个选项卡,其中每个列表视图显示事件列表。这三个选项卡称为过去、今晚和未来。

我已经按照github页面的建议设置了滑块:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        View v = inflater.inflate(R.layout.all_events_main_strip, container, false);     

        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        pager = (ViewPager) v .findViewById(R.id.pager_main);
        tabs = (PagerSlidingTabStrip) v.findViewById(R.id.tabs);
        adapter = new MyPagerAdapter(getFragmentManager());

        pager.setAdapter(adapter);
        tabs.setViewPager(pager);

        // Set Present tab as default
        pager.setCurrentItem(1);
        return v;
    }

当应用程序启动时,主活动第一次添加这个片段,一切都很好。3 个带有 3 个列表视图的可滑动标签。(参见代码部分)

这是问题所在

我注意到,当我按下后退按钮并再次替换片段时,为了重新打开 viewpager,中间的选项卡不会显示任何列表视图。如果我向左或向右滑动,其他选项卡中的内容将被加载并显示,但当前选项卡仍为空。当我调试 ToNightEvents ListFragment 时根本没有被调用。你们有什么解决问题的建议吗?

编码:

代码结构如下:在 onCreateView 之后,我添加了一个 OnDestroyView 方法来删​​除片段,但它不起作用......然后在 fragmentPagerAdapter 中,每个页面在 getItem 方法中被称为片段。最后,在代码的末尾,您可以看到三个 ListFragment 类,其中通过 AsyncTask 填充列表视图

public class FragmentAllEvents extends Fragment 
{
    private static final String TAG_UID = "uid";
    private static final String TAG_LOGO = "logo";
    private static final String TAG_POKUID = "pokuid";

    static ArrayList<HashMap<String, String>> userList;
    ArrayList<HashMap<String, String>> userListTotal;
    private final Handler handler = new Handler();
    HashMap<String, String> userSelected;
    EventsFunctions eventsFunctions;
    UserFunctions userFunctions;
    static ListView lv;
    ActionBar actionBar;
    MyPagerAdapter adapter;
    ViewPager pager;
    PagerSlidingTabStrip tabs;
    private Drawable oldBackground = null;
    private int currentColor = 0xFF666666;
    //Context context = this;

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

        // Set up the action bar.
        actionBar = getActivity().getActionBar();
        actionBar.setHomeButtonEnabled(true);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        View v = inflater.inflate(R.layout.all_events_main_strip, container, false);
        pager = (ViewPager) v .findViewById(R.id.pager_main);
        tabs = (PagerSlidingTabStrip) v.findViewById(R.id.tabs);
        adapter = new MyPagerAdapter(getFragmentManager());

        pager.setAdapter(adapter);

        tabs.setViewPager(pager);

        pager.setCurrentItem(1);
        return v;
    }

    @Override
    public void onDestroyView() 
    {
         super.onDestroyView();
         getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
    }

    public static class MyPagerAdapter extends FragmentPagerAdapter 
    {

        public MyPagerAdapter(FragmentManager fm) 
        {
            super(fm);
        }


        @Override
        public Fragment getItem(int i) 
        {
            switch (i) 
            {
                case 0:
                    return new PastEvents();

                case 1:
                    return new ToNightEvents();

                case 2:
                    return new FutureEvents();

                /*default:
                    // The other sections of the app are dummy placeholders.

                    return new ToNightEvents();
                  */  
            }
            return null;
        }


        /**
         * A fragment that launches past events list.
         */
        public static class PastEvents extends ListFragment implements
        PullToRefreshAttacher.OnRefreshListener 
        {
            private ListView pastList;
            private PullToRefreshAttacher mPullToRefreshAttacher;
            ProgressBar progress;
            String tabTime;

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
            {
                View pastView = inflater.inflate(R.layout.pastlist, container, false);
                progress = (ProgressBar) pastView.findViewById(R.id.loading_spinner_past);
                tabTime="past";
                pastList = (ListView) pastView.findViewById(android.R.id.list);

                // Now get the PullToRefresh attacher from the Activity. An exercise to the reader
                // is to create an implicit interface instead of casting to the concrete Activity
                mPullToRefreshAttacher =  ((Home) getActivity()).getPullToRefreshAttacher();

                // Now set the ScrollView as the refreshable view, and the refresh listener (this)
                mPullToRefreshAttacher.addRefreshableView(pastList, this);

                new AsyncLoadEvents(getActivity(), progress, pastList, mPullToRefreshAttacher).execute(tabTime);
                return pastView;
            }


            @SuppressWarnings("unchecked")
            @Override
            public void onListItemClick(ListView listView, View view, int position, long id) 
            {
                super.onListItemClick (listView, view, position, id);
                HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position);
                //Log.e("AttendList Report", "Clicked list item: " + position +" Content: \n" + map.get(TAG_ID).toString());
                Log.e("PastList Report", "Clicked list item: " + position +" Event's content: \n" + map.get(TAG_UID).toString());
                Intent intent = new Intent(getActivity(), SingleEventActivity.class);

                intent.putExtra("pokuid",map.get(TAG_POKUID)); // Maybe remove attribute toString();
                intent.putExtra("uid", map.get(TAG_UID)); 
                intent.putExtra("logo",map.get(TAG_LOGO));
                getActivity().startActivity(intent);
            }

            @Override
            public void onRefreshStarted(View view) 
            {
                new AsyncLoadEvents(getActivity(), progress, pastList, mPullToRefreshAttacher).execute(tabTime);                
            }

        }

        /**
         * A fragment that launches future event list.
         */
        public static class FutureEvents extends ListFragment  implements
        PullToRefreshAttacher.OnRefreshListener 
        {
            private ListView futureList;
            private PullToRefreshAttacher mPullToRefreshAttacher;
            ProgressBar progress;
            String tabTime;

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
            {
                View futureView = inflater.inflate(R.layout.futurelist, container, false);
                progress = (ProgressBar) futureView.findViewById(R.id.loading_spinner_future);
                tabTime  = "future";
                futureList = (ListView) futureView.findViewById(android.R.id.list); //change to attendlist if needed


                // Now get the PullToRefresh attacher from the Activity. An exercise to the reader
                // is to create an implicit interface instead of casting to the concrete Activity
                mPullToRefreshAttacher =  ((Home) getActivity()).getPullToRefreshAttacher();

                // Now set the ScrollView as the refreshable view, and the refresh listener (this)
                mPullToRefreshAttacher.addRefreshableView(futureList, this);

                new AsyncLoadEvents(getActivity(), progress, futureList, mPullToRefreshAttacher).execute(tabTime);
                return futureView;
            }

            @SuppressWarnings("unchecked")
            @Override
            public void onListItemClick(ListView listView, View view, int position, long id) 
            {
                super.onListItemClick (listView, view, position, id);

                HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position);
                Log.e("PastList Report", "Clicked list item: " + position +" Event's content: \n" + map.get(TAG_UID).toString());
                Intent intent = new Intent(getActivity(), SingleEventActivity.class);


                intent.putExtra("pokuid",map.get(TAG_POKUID)); // Maybe remove attribute toString();
                intent.putExtra("uid", map.get(TAG_UID)); 
                intent.putExtra("logo",map.get(TAG_LOGO));
                getActivity().startActivity(intent);

            }

            @Override
            public void onRefreshStarted(View view) 
            {
                new AsyncLoadEvents(getActivity(), progress, futureList, mPullToRefreshAttacher).execute(tabTime);              
            }
        }

        /**
         * A fragment that launches future event list.
         */
        public static class ToNightEvents extends ListFragment implements
        PullToRefreshAttacher.OnRefreshListener 
        {
            private ListView tonightList;
            private PullToRefreshAttacher mPullToRefreshAttacher;
            ProgressBar progress;
            String tabTime;

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
            {
                View tonightView = inflater.inflate(R.layout.tonightlist, container, false);
                progress = (ProgressBar) tonightView.findViewById(R.id.loading_spinner_tonight);
                tabTime  = "tonight";
                tonightList = (ListView) tonightView.findViewById(android.R.id.list); //change to attendlist if needed

                // Now get the PullToRefresh attacher from the Activity. An exercise to the reader
                // is to create an implicit interface instead of casting to the concrete Activity
                mPullToRefreshAttacher =  ((Home) getActivity()).getPullToRefreshAttacher();

                // Now set the ScrollView as the refreshable view, and the refresh listener (this)
                mPullToRefreshAttacher.addRefreshableView(tonightList, this);

                new AsyncLoadEvents(getActivity(), progress, tonightList, mPullToRefreshAttacher).execute(tabTime);
                return tonightView;
            }


            @SuppressWarnings("unchecked")
            @Override
            public void onListItemClick(ListView listView, View view, int position, long id) 
            {
                super.onListItemClick (listView, view, position, id);

                HashMap<String, String> map = (HashMap<String, String>) getListView().getItemAtPosition(position);
                Log.e("PastList Report", "Clicked list item: " + position +" Event's content: \n" + map.get(TAG_UID).toString());
                Intent intent = new Intent(getActivity(), SingleEventActivity.class);


                intent.putExtra("pokuid",map.get(TAG_POKUID)); // Maybe remove attribute toString();
                intent.putExtra("uid", map.get(TAG_UID)); 
                intent.putExtra("logo",map.get(TAG_LOGO));
                getActivity().startActivity(intent);

            }

            @Override
            public void onRefreshStarted(View view) 
            {
                new AsyncLoadEvents(getActivity(), progress, tonightList, mPullToRefreshAttacher).execute(tabTime);             
            }
        }

        public String[] titles=
        {
            "Past",
            "Tonight",
            "Future"
        };

        @Override
        public int getCount() 
        {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) 
        {
            return titles[position];
        }
    }
}
4

2 回答 2

5

如果您在 Activity 中,这通常会起作用,但是我猜您在 Fragment 中,因为您发布的代码是 onCreateView() 方法。问题是您正在尝试使用 Activity 的 FragmentManager,而您应该使用 Fragment 的 FragmentManager。这个 FragmentManager 不是你需要的。试试这个:

adapter = new MyPagerAdapter(getChildFragmentManager());

我想我有点晚了,但如果有人碰巧有同样的错误,这里就是解决方案=)

问。

于 2014-11-09T19:24:30.667 回答
0

以下为我修复了它:

  • 将 OnBackStackChangedListener 添加到片段管理器。
  • 在 onBackStackChanged 方法中,获取对 ViewPager 和 PagerSlidingTabStrip 的引用(您的示例中的分页器和选项卡)。如果相应的片段当前处于活动状态,请执行以下操作:

    pager.invalidate();
    tabs.invalidate();
    pager.setAdapter(new MyPagerAdapter(getFragmentManager()));
    tabs.setViewPager(pager);
    
于 2014-10-20T16:09:01.847 回答