-1

我有一个带有两个选项卡的操作栏和一个处理片段的 tablistener。现在我希望使用 ViewPager 可以通过滑动来切换选项卡。我尝试了此处所述的解决方案: Android,如何混合 ActionBar.Tab + View Pager + ListFragment

但它与 Android.app.Fragments 和 Support Package Fragments 存在冲突。该应用程序适用于 >4.0,因此我不需要支持片段。

public class MainActivity extends Activity{
....
    actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //viewpager
    mViewPager = new ViewPager(this);
    setContentView(mViewPager);

不起作用:

    //TabsAdapter tabsAdapter = new TabsAdapter(this,mViewPager);  
   //        Tab tab1 = actionBar.newTab().setText("Tab1");
   //        Tab tab2 = actionBar.newTab().setText("Tab2");
   //        tabsAdapter.addTab(tab1, Tab1Fragment.class, null);
   //        tabsAdapter.addTab(tab2, Tab2Fragment.class, null);

选择:

    //viewpager
    mViewPager = new ViewPager(this);
    setContentView(mViewPager);
    mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between pages, select the
                    // corresponding tab.
                    super.onPageSelected(position);
                    getActionBar().setSelectedNavigationItem(position);
                }
            });



    tab_1 = actionBar.newTab().setText("Tab1");

    tab_1
            .setTabListener(new TabListener<Tab1Fragment>(
                    this, "Tab1", Tab1Fragment.class,mViewPager));

    actionBar.addTab(tab_1);


    tab_2 = actionBar.newTab().setText("Tab2");
    tab_2
            .setTabListener(new TabListener<Tab2Fragment>(
                    this, "Tab2", Tab2Fragment.class,mViewPager));

    actionBar.addTab(tab_2);




 /**
 * TabListener 
 * @param <T>
 */
private static class TabListener<T extends Fragment> implements ActionBar.TabListener
        {
    private Fragment mFragment;
    private Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private ViewPager vp;

    public TabListener(Activity activity, String tag, Class<T> clz, ViewPager vp) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        this.vp = vp;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.replace(android.R.id.content, mFragment, mTag);
        } else {
            if (mFragment.isDetached()) {
                ft.attach(mFragment);
            }
        }
       vp.setCurrentItem(tab.getPosition());
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        if (mFragment != null) {
            ft.detach(mFragment);
        }
    }

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


}

在 Fragments 类中,我有如下语句:

        Bundle args = new Bundle();
        args.putString("name", atn.getName());
        args.putLong("aid", atn.getId());
        AFragment f = new AFragment();
        f.setArguments(args);
        f.show(getFragmentManager(), "tag");

    FragmentManager fragmentManager =mActivity.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment f = fragmentManager.findFragmentByTag("Tab1");
    if (f != null) {

        fragmentTransaction.detach(f);
        fragmentTransaction.attach(f);
    }
    fragmentTransaction.replace(android.R.id.content, f);
    fragmentTransaction. commitAllowingStateLoss();

与其他 Fragment 类型不兼容(import android.support.v4.app.Fragment;)。如何轻松添加滑动手势?选项卡上的选择完美无缺。

编辑:使用支持包 13,视图寻呼机工作但内容未刷新。我使用 FragmentStatePagerAdapter,因此应该删除并再次添加 Fragment,但它需要相同的 Fragment 而无需创建新的 Fragment。Viewpage 也命名为 android:switcher 之类的片段...我可以自己命名它们吗?

    import android.support.v13.app.FragmentStatePagerAdapter;


public class TabsAdapter extends FragmentStatePagerAdapter
    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>();

private Fragment mFragment;
private Activity mActivity;
private String mTag;




private final List<Fragment> fragments = new ArrayList<Fragment>();

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


    TabInfo(Class<?> _class, Bundle _args,String name) {
        clss = _class;
        args = _args;
        this.name=name;

    }
}

public TabsAdapter(Activity activity, ViewPager pager) {
    super(activity.getFragmentManager());
    mContext = activity;
    mActionBar = activity.getActionBar();
    mViewPager = pager;
    mViewPager.setAdapter(this);
    mViewPager.setOnPageChangeListener(this);
    mActivity = activity;

}

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

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

@Override
public int getItemPosition(Object object) {

    return POSITION_NONE;
//       return super.getItemPosition(object);


}

@Override
public Fragment getItem(int position) {



TabInfo info = mTabs.get(position);
 Fragment fr = Fragment.instantiate(mContext, info.clss.getName(), info.args);
//
//        //addFragment (fr, position);
    return  fr;

}

public void addFragment(Fragment f, int location) {

    if (fragments.size() == 0)
        fragments.add(f);
    else
        fragments.add(location, f);
}

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

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

@Override
public void onPageScrollStateChanged(int state) {
}

@Override
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
    Object tag = tab.getTag();
    for (int i=0; i<mTabs.size(); i++) {
        if (mTabs.get(i).name == tag) {
            updateDataSet(i);

            mViewPager.setCurrentItem(i);
        }
    }

}




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

}

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

}


public void updateDataSet(int pos)
{
    //Let's update the dataset for the selected genre
    Fragment fragment =
            (mActivity.getFragmentManager().findFragmentByTag(
                    "android:switcher:"+ mViewPager.getId()+":"+pos));

    //TabFragment fragment = (TabFragment) getItem(pos);
    if(fragment != null)  // could be null if not instantiated yet
    {
        if(fragment.getView() != null)
        {
            // no need to call if fragment's onDestroyView()
            //has since been called.
          if(fragment instanceof Tab1Fragment){
              ((Tab1Fragment) fragment).refresh();
          }
            else if(fragment instanceof Tab2Fragment){
              ( (Tab2Fragment) fragment).refresh();
          }
        }
    }

}
4

2 回答 2

0

尝试使用 build it Tab Activity 模板,创建一个示例并从那里提取您想要的代码。我创建了一个基于标签的应用程序,但我使用了默认选项并自定义了这些选项以满足我的需求。

于 2013-10-10T10:10:26.050 回答
0

检查这个开源项目https://github.com/dkim0419/SoundRecorder 他们使用一个带有 viewPager 和一些片段的活动。

也使用PagerSlidingTabStrip作为页面指示器。

看看他们是如何实现它的很容易。

于 2017-10-06T17:16:28.003 回答