11

enter image description hereAndroid Actionbar Sherlock với các tab

Tôi đang cố gắng triển khai ActionBar Sherlock bằng các tab bên dưới như được hiển thị trong khung dây ở trên.

Tôi có nên sử dụng TabActivity không? - kể từ khi tôi thấy rằng nó không được chấp nhận. Đó là cách tốt nhất để đạt được điều tương tự.

+0

bạn có thể chia sẻ câu trả lời –

Trả lời

25

tôi thực hiện chức năng này với một SherlockFragmentActivity như container TabView và với SherlockFragment dưới dạng tab. Dưới đây là một phác thảo (Tôi bỏ qua những thứ hoạt động Android thông thường):

Đây là hoạt động TabView với hai tab:

public class TabViewActivity extends SherlockFragmentActivity { 
    // store the active tab here 
    public static String ACTIVE_TAB = "activeTab"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    .. 
    final ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    // add the first tab which is an instance of TabFragment1 
    Tab tab1 = actionBar.newTab() 
       .setText("TabTitle1") 
       .setTabListener(new TabListener<TabFragment1>(
       this, "tab1", TabFragment1.class)); 
    actionBar.addTab(tab1); 

    // add the second tab which is an instance of TabFragment2 
    Tab tab2 = actionBar.newTab() 
      .setText("TabTitle2") 
      .setTabListener(new TabListener<TabFragment2>(
       this, "tab2", TabFragment2.class)); 
    actionBar.addTab(tab2); 

    // check if there is a saved state to select active tab 
    if(savedInstanceState != null){ 
     getSupportActionBar().setSelectedNavigationItem(
        savedInstanceState.getInt(ACTIVE_TAB)); 
    } 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
    // save active tab 
    outState.putInt(ACTIVE_TAB, 
      getSupportActionBar().getSelectedNavigationIndex()); 
    super.onSaveInstanceState(outState); 
    } 
} 

Và đây là TabFragment chứa nội dung của một tab:

public class TabFragment extends SherlockFragment { 
    // your member variables here 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
       ViewGroup container, Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_va_esh, container, false); 
    ... // do your view initialization here 
    return view; 
    } 

} 

Và cuối cùng đây là TabListener xử lý switch tab:

public class TabListener<T extends Fragment> implements ActionBar.TabListener{ 
    private TabFragment mFragment; 
    private final Activity mActivity; 
    private final String mTag; 
    private final Class<T> mClass; 

    public TabListener(Activity activity, String tag, Class<T> clz) { 
    mActivity = activity; 
    mTag = tag; 
    mClass = clz; 
    } 

    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 = (TabFragment) Fragment.instantiate(
         mActivity, mClass.getName()); 
     mFragment.setProviderId(mTag); // id for event provider 
     ft.add(android.R.id.content, mFragment, mTag); 
    } else { 
     // If it exists, simply attach it in order to show it 
     ft.attach(mFragment); 
    } 

    } 

    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
    if (mFragment != null) { 
     // Detach the fragment, because another one is being attached 
     ft.detach(mFragment); 
    } 
    } 

    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
    // User selected the already selected tab. Usually do nothing. 
    } 
} 
+0

Làm cách nào để chúng tôi có thể thêm nút chia sẻ ở thanh tác vụ vào câu hỏi của harsha mv.? –

+0

@suresh: Đây là một vấn đề khác. Xem [câu hỏi liên quan này] (http://stackoverflow.com/q/10278952/620338). –

+0

@MattHandy tôi muốn thêm Tabs cũng như ActionBar Iteams. do đó, giải pháp ur sẽ làm việc? –

4

Tôi tin rằng TabActivity không còn được dùng để sử dụng Phân đoạn - không phải do điều hướng tab là khái niệm không được chấp nhận. Chỉ cần sử dụng Fragments và TabWidget.

Ngoài ra, đây là a similar question.

Edit:

Dưới đây là một ví dụ biếu không của Google: Android Tabs the Fragment Way

Các vấn đề liên quan