2013-10-10 46 views
-1

我有一個操作欄有兩個選項卡和一個處理碎片的tablistener。現在我想要一個ViewPager的可能性,以刷卡也切換選項卡。 我試過這裏說的解決方案: Android, How to mix ActionBar.Tab + View Pager + ListFragmentAndroid:ViewPager,ActionBar和Android.app.Fragments

但它給與Android.app.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) { 
    } 


} 

在片段類我有這樣的語句:

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

這是不與其他片段兼容鍵入(導入一個droid.support.v4.app.Fragment)。我怎樣才能輕鬆添加滑動手勢?選項卡上的選擇完美。

編輯: 隨着支持包13視圖頁面工作,但內容不刷新。我使用FragmentStatePagerAdapter,因此碎片應該被刪除並重新添加,但是它會在沒有新創建的情況下采用相同的碎片。另外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(); 
      } 
     } 
    } 

} 
+0

看一看:'http:// www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1 /' –

+0

我管理它使用支持庫13.但視圖沒有更新,片段獲得內部切換器標籤。 – Siser

回答

0

嘗試使用構建選項卡活動模板,創建一個示例並從那裏提取您想要的代碼。我創建了一個基於標籤的應用程序,但我使用了默認選項並將其自定義以適合我的需求。