【问题标题】:Play with fragments in onTabSelected在 onTabSelected 中播放片段
【发布时间】:2014-01-20 07:43:02
【问题描述】:

亲爱的,

我搜索这个问题超过一天,但没有运气。 我完全实现了这里发布的代码:

Adding Navigation Tabs

我的 onTabSelected 代码如下所示:

public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(R.id.alert_fragment_container, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }

        // prepare adapter for ExpandableListView

        Log.i("After Adapter Created", "Passed");

        final ExpandableListAdapter expListAdapter = new AlertsAdapter(
                mActivity, myAlerts, violations);

        Log.i("After Adapter Initialized", "Passed");

       ((MyCustomFragment)mFragment).violations.setAdapter(expListAdapter);
    }

代码运行良好,直到最后一行,我需要在onCreateView 中为在MyCustomFragment 中初始化的公共静态列表设置适配器,这是我的片段代码:

public class MyCustomFragment extends Fragment {

    public MyCustomFragment() {
    }

    public static ExpandableListView violations; 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_alerts_poi, container, false);

        violations = (ExpandableListView) rootView.findViewById(R.id.POIAlertList);

        Log.i("onCreateView POI", "Called");

        return rootView;
    }
}

它给出空指针错误。通过我的调试日志,我注意到这个日志Log.i("onCreateView POI", "Called"); 出现在这个Log.i("After Adapter Initialized", "Passed"); 之后。这意味着我正在尝试为尚未初始化的片段设置适配器。

这正是我面临的问题,我需要根据onTabSelected 中的选项卡选择向ExpandableListView 提供数据。

我做错了什么?什么是最好的解决方案?

问候,

【问题讨论】:

标签: android android-fragments


【解决方案1】:

看来你需要一个ViewPager,我前几天刚刚实现了一个导航标签,这是我的代码,它在4个片段之间导航:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
private ActionBar actionBar;
private ViewPager mViewPager;
private AppSectionsPagerAdapter mAppSectionsPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
    actionBar=getActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon1).setTabListener(this));
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon2).setTabListener(this));
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon3).setTabListener(this));
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon4).setTabListener(this));     
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getMenuInflater().inflate(R.menu.action_menu, menu);
    return true;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    mViewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

这是适配器:

public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    switch (i) {
        case 0:
            return new Fragment1();
        case 1:
            return new Fragment2();
        case 2:
            return new Fragment3();
        case 3: 
            return new Fragment4();
    }
    return null;
}

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

}

【讨论】:

  • 我可以在哪里放置准备和设置适配器的代码?另外,除了这个,还有什么解决方案吗?如果我将应用该解决方案,我需要更改我的结构中的许多内容。为什么我的方法不对?
  • 你不需要改变很多东西,适配器在MainActivity中初始化,我不知道你的主要活动是什么样的,所以很难弄清楚你的问题是什么代码,在上面的MainActivity中,还有一个onTabSelected()方法,我也跟着你的教程,希望你能通过上面的代码和你的代码对比找到bug
  • 我使用ViewPager 将我的代码转换为新活动。它适用于第二个、第三个和第四个选项卡。第一个仍然给我空指针。我把初始化适配器的代码放在ViewPager监听器的onPageSelected(int position)中。我试图强迫听众调用onPageSelected(int position) 的第一个位置= 0 使用pageChangeListener.onPageSelected(0); 没有运气。
【解决方案2】:

查看@此Tablayout.onTabselected 以获取最新的 API 更新。 ActionBar.TabListener 是一个旧的实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多