0

我有一个主/详细视图来显示我的用户。成员详细信息片段包含滑动选项卡以在其他 3 个片段之间切换 - 消息、个人资料和照片(按此顺序,默认情况下会激活中间选项卡)。

当我在平板电脑上打开主/详细视图并单击第一个用户时,所有片段都已正确创建。

但是,当我然后单击另一个用户时,正确创建了包含滑动选项卡的“详细信息”片段,但其中的所有片段都是空的,并且不会调用选项卡中任何片段中的回调。似乎 SectionsPagerAdapter 没有实例化片段,除非它是第一次创建的。

调试时,我发现我的 SectionsPagerAdapter 肯定在我的“详细”片段中被调用:

            Log.d(CLS, "Creating the ViewPager");
            // Create the adapter that will return a fragment for each of the three
            // primary sections of the app.
            mSectionsPagerAdapter = new SectionsPagerAdapter(
            getActivity().getSupportFragmentManager());

            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);

但是,我的 SectionsPagerAdapter 上的 getItem 只会为第一个用户调用。它总是正确地调用 SectionsPagerAdapter.getPageTitle。

更多代码:

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    // The IDs of the strings to show for each tab 
    private int[] tabStringIDs = new int[]{
        R.string.message,
        R.string.profile,
        R.string.photos
    };

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

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        Bundle args = null;
        Log.d(CLS, "SectionsPagerAdapter.getItem called for position: " + position);

        switch (position) {
        case 0:                     // message thread
            Log.d(CLS, "Adding fragment for position: " + position);

            fragment = new MessageDetailFragment();
            args = new Bundle();
            // TODO: Set the user ID properly (for the ID of the user the message thread is between)
            String otherUserId = "123453124aaaa";
            args.putString(MessageDetailFragment.ARG_ITEM_ID, otherUserId);
            fragment.setArguments(args);
            break;
        case 1:                     // profile
            fragment = new UserDetailProfileFragment();
            userDetailFragment = (UserDetailProfileFragment)fragment;
            if (mItem != null) {
                Log.d(CLS, "Setting userDetailFragment user object in getItem()");
                ((UserDetailProfileFragment)fragment).setUser(mItem);
            }
            break;
        case 2:                     // image grid
            fragment = new UserDetailPhotoListFragment();
            photoListFragment = (UserDetailPhotoListFragment)fragment;
            if (mItem != null) {
                Log.d(CLS, "Setting photoListFragment user object in getItem()");
                photoListFragment.setUser(mItem);
            }
            break;
        } 

        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Log.d(CLS, "SectionsPagerAdapter.getPageTitle called for position: " + position);
        try {
            return getString(tabStringIDs[position]);
        } catch (Exception e) {
            Log.e(CLS, "Caught an exception accessing the pager title: " + e.getMessage());
            return null;
        }
    }
}

有谁知道我的片段是空白的/没有被创建?

如果我在选项卡之间滚动,离我最远的选项卡会暂停,然后当它恢复时,数据会正确显示。

基本上我正在寻找一种强制片段管理器重新实例化它的片段的方法 - 或者至少在它们上调用一个回调方法。

更新

这只会影响我的两个窗格主/详细视图。在启动新活动以显示配置文件的较小设备上,一切正常。也许这与我处理切换细节视图的方式有关。这是我的用户列表活动中的点击处理程序:

    @Override
public void onUserItemSelected(String id, String title) {
    if (mTwoPane) {
        // In two-pane mode, show the detail view in this activity by
        // adding or replacing the detail fragment using a
        // fragment transaction.
        Log.d(CLS, "Creating new user detail fragment for two-pane mode for user ID: " + id);
        Bundle arguments = new Bundle();
        arguments.putString(UserDetailFragment.ARG_ITEM_ID, id);
        UserDetailFragment fragment = new UserDetailFragment();
        fragment.setArguments(arguments); 
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.user_detail_container, fragment).commit();

    } else {
        // In single-pane mode, simply start the detail activity
        // for the selected item ID.
        Intent detailIntent = new Intent(this, UserDetailActivity.class);
        detailIntent.putExtra(UserDetailFragment.ARG_ITEM_ID, id);
        Log.d(CLS, "Adding item title " + title + " to bundle");
        detailIntent.putExtra(UserDetailFragment.ARG_ITEM_TITLE, title);
        startActivity(detailIntent);
    }
}
4

0 回答 0