Android视图寻呼机没有填充视图片段

时间:2013-05-26 15:40:35

标签: android

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

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

然而,当我点击另一个用户时,正确创建了包含滑动选项卡的'detail'片段,但其中的所有片段都是空的,并且选项卡中任何片段中的所有片段都没有调用。似乎SectionsPagerAdapter没有实例化片段,除了第一次创建片段。

调试,我发现我的SectionsPagerAdapter肯定是在我的'detail'片段中调用的:

            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);
    }
}

0 个答案:

没有答案