刷新vi​​ewpager,lsitfragment还是适配器?

时间:2012-09-28 22:08:42

标签: android android-viewpager baseadapter android-listfragment

我有一个主类,它包含一个带有tabs和viewpager的actionbarsherlock

就是这样:

public class HSG_Handball extends SherlockFragmentActivity
{
    ViewPager mViewPager;
    TabsAdapter mTabsAdapter;
    TextView tabCenter;
    TextView tabText;

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

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);

        setContentView(mViewPager);
        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mTabsAdapter = new TabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(bar.newTab().setText("Alle Spiele"), Spiele.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Tabelle"), Tabelle.class, null);
    }

    public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener
    {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo
        {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args)
            {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager)
        {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getSupportActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
        {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }

        @Override
        public int getCount()
        {
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position)
        {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {
        }

        public void onPageSelected(int position)
        {
            mActionBar.setSelectedNavigationItem(position);
        }

        public void onPageScrollStateChanged(int state)
        {
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft)
        {
            Object tag = tab.getTag();
            for (int i = 0; i < mTabs.size(); i++)
            {
                if (mTabs.get(i) == tag)
                {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft)
        {
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft)
        {
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) 
    {

        SubMenu subMenu1 = menu.addSubMenu("Aktive");
        subMenu1.add("Herren 1");
        subMenu1.add("Herren 2");
        subMenu1.add("Herren 3");
        subMenu1.add("Damen 1");
        subMenu1.add("Damen 2");

        MenuItem subMenu1Item = subMenu1.getItem();
        subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        SubMenu subMenu2 = menu.addSubMenu("Jugend");
        subMenu2.add("männl. A");
        subMenu2.add("weibl. A");
        subMenu2.add("männl. B1");
        subMenu2.add("männl. B2");
        subMenu2.add("männl. C");
        subMenu2.add("männl. D");
        subMenu2.add("E-Jugend");

        MenuItem subMenu2Item = subMenu2.getItem();
        subMenu2Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(!item.getTitle().toString().equals("Active") || !item.getTitle().toString().equals("Jugend"))
        {
            new Spiele().FormatiereSpieleSeite(item.getTitle().toString());
        }
        return super.onOptionsItemSelected(item);
    }
}

第二个类(Spiele)是一个带有自定义基本适配器的listfragment。 该课程看起来如此:

public class Spiele扩展了ListFragment {

String ligaId = "0";
SpieleAdapterFuerCustomList adapter;

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

        List<String> dateAndSchiri = new ArrayList<String>();
        dateAndSchiri.add("DATUM  ·  Schiri 1 : Schiri 2");
        dateAndSchiri.add("DATUM  ·  Schiri 1 : Schiri 2");
        dateAndSchiri.add("DATUM  ·  Schiri 1 : Schiri 2");

        List<String> heim = new ArrayList<String>();
        heim.add("HSG Nieder-Roden");
        heim.add("SG Bruchköbel");
        heim.add("LOL Idiotenklupp");

        List<String> gast = new ArrayList<String>();
        gast.add("KG Wallau");
        gast.add("DJ G4bby");
        gast.add("Hanzup Motherf*ckers");

        adapter = new SpieleAdapterFuerCustomList(getActivity(), dateAndSchiri, heim, gast);
        setListAdapter(adapter);

        return view;
}

public void FormatiereSpieleSeite(String mannschaft)
    {
// aktive
        if(mannschaft.equals("Herren 1"))
            ligaId = "X0";
        if(mannschaft.equals("Herren 2"))
            ligaId = "00151350asd0";
        if(mannschaft.equals("Herren 3"))
            ligaId = "00151asdad000000000000000001000";
        if(mannschaft.equals("Damen 1"))
            ligaId = "001513asdad0000000007000";
        if(mannschaft.equals("Damen 2"))
            ligaId = "0015135asd0000000000003000";

// jugend
        if(mannschaft.equals("männl. A"))
            ligaId = "0015135asdasd0000000000001000";
        if(mannschaft.equals("weibl. A"))
            ligaId = "";
        if(mannschaft.equals("männl. B1"))
            ligaId = "001513asdasd0000000000000002000";
        if(mannschaft.equals("männl. B2"))
            ligaId = "00151350asd05502000000000000001000";
        if(mannschaft.equals("männl. C"))
            ligaId = "0015asdasd503000000000000001000";
        if(mannschaft.equals("männl. D"))
            ligaId = "001513504501asdsad0000001000";
        if(mannschaft.equals("E-Jugend"))
            ligaId = "0015135asdasd000000000003000";



    System.out.println(ligaId + "Das ist liga");

    List<String> dateAndSchiri = new ArrayList<String>();
    dateAndSchiri.add("DATUM as Schiri 2");
    dateAndSchiri.add("DATUMasas Schisdfsdfri 2");
    dateAndSchiri.add("DATUMasas Schirsdfsfi 2");

    List<String> heim = new ArrayList<String>();
    heim.add("HSG Nieder-sdfRoden");
    heim.add("SG Brucsdfhköbel");
    heim.add("LOL Idiosdfsdftenklupp");

    List<String> gast = new ArrayList<String>();
    gast.add("KG Walsdfsflau");
    gast.add("DJ G4sdfsfbby");
    gast.add("Hanzusdfsfp Motherf*ckers");

    adapter = new SpieleAdapterFuerCustomList(getActivity(), dateAndSchiri, heim, gast);
    adapter.notifyDataSetChanged();
    setListAdapter(adapter);

}

}

一切都好,运行正常。

在“主”级上。包含viewpager和tabsactionbar的类具有菜单。如果用户单击其中一个,则必须加载新的ListFragment。或者新的BaseAdapter(?)。 我希望ListFragment(类:Spiele)是updatet。 但我怎么能这样做? 如何更新我的ListFragment?

2 个答案:

答案 0 :(得分:0)

为什么这个ArrayList有TabInfo?为什么不直接使用带有Fragments的ArrayList。而不是在适配器中,而是在Activity本身?

在这种情况下,您可以执行以下操作:

1

public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
{
   tab.setTabListener(this);
   Fragment f = Fragment.instantiate(mContext, clss.getName(), args);
   mFragments.add(f);
   mActionBar.addTab(tab);
   notifyDataSetChanged();
}

2

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if(!item.getTitle().toString().equals("Active") || !item.getTitle().toString().equals("Jugend"))
    {
        Fragment f = mFragments.get(getSupportActionBar().getSelectedTab().getPosition());
        ((Spiele)f).FormatiereSpieleSeite(item.getTitle().toString());
    }
    return true;
}

如果我理解正确你想要做的就是更换适配器。正确?

答案 1 :(得分:0)

解决方案比我想象的更容易。

这些https://stackoverflow.com/a/7393477/1231245帮助我从ViewPager的第一个片段中找到了FragmentTag。

我可以轻松地在onOptionsItemSelected方法中创建和设置适配器。 这是它:

        List<String> dateAndSchiri = new ArrayList<String>();
        dateAndSchiri.add.....

        List<String> heim = new ArrayList<String>();
        heim.add.....

        List<String> gast = new ArrayList<String>();
        gast.add.....

        SpieleAdapterFuerCustomList adapter = new SpieleAdapterFuerCustomList(getBaseContext(), dateAndSchiri, heim, gast);
        Spiele articleFrag = (Spiele) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.pager+":0");
        articleFrag.setListAdapter(adapter);

您可以使用不同的ArrayList创建一个新的适配器,并使用不同的单击菜单...