2014-02-06 55 views
1

我這裏有標籤的代碼,當你選擇的每個選項卡,但我不能刷卡轉到下一個標籤,因爲它的工作原理。
每個標籤都有的ListView有自己的activity.Only的事情我想是添加滑動手勢轉至下tab.How做呢?我非常感謝任何幫助。感謝提前。添加viewpager爲製表符

public class AndroidTabAndListView extends TabActivity { 
    // TabSpec Names 
    private static final String INBOX_SPEC = "Inbox"; 
    private static final String OUTBOX_SPEC = "Outbox"; 
    private static final String PROFILE_SPEC = "Profile"; 

    ViewPager pager; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


      pager = (ViewPager) findViewById(R.id.pager); 

     TabHost tabHost = getTabHost(); 

     // Inbox Tab 
     TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC); 
     // Tab Icon 
     inboxSpec.setIndicator(INBOX_SPEC, getResources().getDrawable(R.drawable.icon_inbox)); 
     Intent inboxIntent = new Intent(this, InboxActivity.class); 
     // Tab Content 
     inboxSpec.setContent(inboxIntent); 

     // Outbox Tab 
     TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC); 
     outboxSpec.setIndicator(OUTBOX_SPEC, getResources().getDrawable(R.drawable.icon_outbox)); 
     Intent outboxIntent = new Intent(this, OutboxActivity.class); 
     outboxSpec.setContent(outboxIntent); 

     // Profile Tab 
     TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC); 
     profileSpec.setIndicator(PROFILE_SPEC, getResources().getDrawable(R.drawable.icon_profile)); 
     Intent profileIntent = new Intent(this, ProfileActivity.class); 
     profileSpec.setContent(profileIntent); 

     // Adding all TabSpec to TabHost 
     tabHost.addTab(inboxSpec); // Adding Inbox tab 
     tabHost.addTab(outboxSpec); // Adding Outbox tab 
     tabHost.addTab(profileSpec); // Adding Profile tab 
    } 
} 

XML

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="409dp" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
      </FrameLayout> 



    </LinearLayout> 
</TabHost> 
+0

你可以提供你的'layout.xml'代碼? – SMR

+0

更新了代碼。我只是想添加滑動功能到下一個選項卡。所有這些。感謝您的時間。 – jason

回答

2

奧萊特我看,你是不是在XML使用ViewPager。因爲我還沒有嘗試過,你是幹什麼的方式,我建議,如果你不喜歡它下面的例子將是一個容易得多:
Creating Swipe Views with Tabs
Android Tab Layout with Swipeable Views

還沒有用,那麼你可以閱讀this
希望它幫助你。乾杯:)

+0

我嘗試使用固定標籤+刷卡,但我如何使用意圖內:'Intent outboxIntent = new Intent(this,OutboxActivity.class); outboxSpec.setContent(outboxIntent);'請幫我這個,我真的很困惑,但我喜歡的部分意圖,我在每個活動3個自定義列表視圖,我想這是對的,但viewholder我不知道如何TP那麼做吧。你可以點亮一些。再次感謝。 – jason

0

的內頁使用的片段。 創建一個爲選項卡按鈕擴展FragmentPagerAdapter的類。

實施例:

activity_main.xml中

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

AndroidTabAndListView.java

public class AndroidTabAndListView extends FragmentActivity implements ActionBar.TabListener { 

    TabSpecNames mAppSectionsPagerAdapter; 
    ViewPager mViewPager; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Create the adapter that will return a fragment for each of the three primary sections 
     // of the app. 
     mAppSectionsPagerAdapter = new TabSpecNames(getSupportFragmentManager()); 

     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 

     // Specify that the Home/Up button should not be enabled, since there is no hierarchical 
     // parent. 
     actionBar.setHomeButtonEnabled(false); 

     // Specify that we will be displaying tabs in the action bar. 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Set up the ViewPager, attaching the adapter and setting up a listener for when the 
     // user swipes between sections. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mAppSectionsPagerAdapter); 
     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       // When swiping between different app sections, select the corresponding tab. 
       // We can also use ActionBar.Tab#select() to do this if we have a reference to the 
       // Tab. 
       actionBar.setSelectedNavigationItem(position); 
      } 
     }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by the adapter. 
      // Also specify this Activity object, which implements the TabListener interface, as the 
      // listener for when this tab is selected. 
      actionBar.addTab(
        actionBar.newTab() 
          .setText(mAppSectionsPagerAdapter.getPageTitle(i)) 
          .setTabListener(this)); 
     } 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, switch to the corresponding page in the ViewPager. 
     mViewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    } 


    public static class TabSpecNames extends FragmentPagerAdapter { 

     public TabSpecNames(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int i) { 

      switch (i) { 
      case 0: 
       return new InboxFragment(); 
      case 1: 
       return new OutboxFragment(); 
      case 2: 
       return new ProfileFragment(); 
      default: 
       break; 
      } 
      return null; 
     } 

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

     @Override 
     public CharSequence getPageTitle(int position) { 
      String sections[] = {"Inbox", "Outbox", "Profile"}; 
      return sections[position]; 
     } 
    } 

} 

fragment_inbox.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tv1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="Inbox (99)"/> 

</LinearLayout> 

InboxFragment.java

public class InboxFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
      ViewGroup container, 
      Bundle savedInstanceState) 
    { 

     View rootView = inflater.inflate(R.layout.fragment_inbox, 
       container, false); 

     return rootView; 
    } 
} 
+0

我添加了代碼,但我在哪裏傳遞意圖。 public static class ContentFragment extends Fragment {@Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){0} {0} {查看rootView = inflater.inflate(R.layout.main2,container,false); Bundle args = getArguments(); String content = args.getString(「section_content」); ((TextView)rootView.findViewById(android.R.id.text1))。setText(content); return rootView;}}'::::::::::如何在此處使用自定義適配器轉換現有活動? – jason

+0

你不能使用意圖。只是實例化片段類。片段必須始終嵌入到活動中。 AndroidTabAndListView是從FragmentActivity擴展而來的,它將包含你的子片段。您應該將現有活動轉換爲片段。看到我編輯的答案。 –