2012-11-21 46 views
0

我嘗試在我的android項目中使用SWIPE選項卡實現應用程序欄。 可以說我有兩個選項卡(tab1和tab2)。我想在這些標籤上顯示不同的內容。 (實際上,這就是我想要做的更多)。Swipe Tabs - 帶有列表視圖和適配器的兩個選項卡

Tab1包含一個列表,Tab2包含「另一個」列表加上一些文本。 是否可以爲兩個標籤使用兩個「活動」? (這就是我現在正在做的)

我使用自定義適配器來設置列表內容。

final ListView lv = (ListView)findViewById(R.id.tab1_list); 
lv.setAdapter(new CustomListAdapter(this, tab1Items)); 

但我無法正確顯示標籤內容。它們或者根本不顯示,或者顯示相同的內容。

有沒有人有我的一些好主意,或者一些小代碼示例,如何使用帶有選項卡和列表的應用程序欄(使用適配器)?

我非常感謝任何幫助。

回答

2

這裏是你想要的「刷卡標籤」

希望這將幫助你的例子....如果它只是投我一票

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <TabHost 
      android:id="@android:id/tabhost" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 
      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       > 
       <TabWidget 
        android:id="@android:id/tabs" 
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="0" 
        /> 

       <FrameLayout 
        android:id="@android:id/tabcontent" 
        android:layout_width="0dp" 
        android:layout_height="0dp" 
        android:layout_weight="0"/> 

       <android.support.v4.view.ViewPager 
        android:id="@+id/viewpager" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        /> 
      </LinearLayout> 
     </TabHost> 
    </LinearLayout> 

定義PagerAdapter:

package com.andy.fragments.viewpager; 

import java.util.List; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 


public class PagerAdapter extends FragmentPagerAdapter { 

    private List<Fragment> fragments; 

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) { 
     super(fm); 
     this.fragments = fragments; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return this.fragments.get(position); 
    } 


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

定義選項卡FragmentActivity:

package com.andy.fragments.tabs; 

import java.util.HashMap; 
import java.util.List; 
import java.util.Vector; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.view.View; 
import android.widget.TabHost; 
import android.widget.TabHost.TabContentFactory; 

import com.andy.R; 
import com.andy.fragments.viewpager.PagerAdapter; 


public class TabsViewPagerFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { 

    private TabHost mTabHost; 
    private ViewPager mViewPager; 
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>(); 
    private PagerAdapter mPagerAdapter; 

    private class TabInfo { 
     private String tag; 
     private Class<?> clss; 
     private Bundle args; 
     private Fragment fragment; 
     TabInfo(String tag, Class<?> clazz, Bundle args) { 
      this.tag = tag; 
      this.clss = clazz; 
      this.args = args; 
     } 

    } 

    class TabFactory implements TabContentFactory { 

     private final Context mContext; 

     public TabFactory(Context context) { 
      mContext = context; 
     } 


     public View createTabContent(String tag) { 
      View v = new View(mContext); 
      v.setMinimumWidth(0); 
      v.setMinimumHeight(0); 
      return v; 
     } 

    } 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.tabs_viewpager_layout); 

     this.initialiseTabHost(savedInstanceState); 
     if (savedInstanceState != null) { 
      mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
     } 

     this.intialiseViewPager(); 
    } 


    protected void onSaveInstanceState(Bundle outState) { 
     outState.putString("tab", mTabHost.getCurrentTabTag()); 
     super.onSaveInstanceState(outState); 
    } 


    private void intialiseViewPager() { 

     List<Fragment> fragments = new Vector<Fragment>(); 
     fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName())); 
     fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName())); 
     fragments.add(Fragment.instantiate(this, Tab3Fragment.class.getName())); 
     this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); 

     this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager); 
     this.mViewPager.setAdapter(this.mPagerAdapter); 
     this.mViewPager.setOnPageChangeListener(this); 
    } 


    private void initialiseTabHost(Bundle args) { 
     mTabHost = (TabHost)findViewById(android.R.id.tabhost); 
     mTabHost.setup(); 
     TabInfo tabInfo = null; 
     TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), (tabInfo = new TabInfo("Tab1", Tab1Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), (tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), (tabInfo = new TabInfo("Tab3", Tab3Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 

     mTabHost.setOnTabChangedListener(this); 
    } 


    private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) { 

     tabSpec.setContent(activity.new TabFactory(activity)); 
     tabHost.addTab(tabSpec); 
    } 


    public void onTabChanged(String tag) { 

     int pos = this.mTabHost.getCurrentTab(); 
     this.mViewPager.setCurrentItem(pos); 
    } 


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


    } 


    @Override 
    public void onPageSelected(int position) { 

     this.mTabHost.setCurrentTab(position); 
    } 


    @Override 
    public void onPageScrollStateChanged(int state) { 


    } 
} 
1

爲什麼不使用佈局方法?創建一個佈局,其中把接頭主機這樣的:ID爲TAB1

<?xml version="1.0" encoding="utf-8"?> 

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:android1="http://schemas.android.com/apk/res/android" 
android1:id="@android:id/tabhost" 
android1:layout_width="match_parent" 
android1:layout_height="match_parent" > 

<TabWidget 
    android1:id="@android:id/tabs" 
    android1:layout_width="match_parent" 
    android1:layout_height="wrap_content" > 
</TabWidget> 

<FrameLayout 
    android1:id="@android:id/tabcontent" 
    android1:layout_width="match_parent" 
    android1:layout_height="match_parent" > 

    <LinearLayout 
     android1:id="@+id/tab1" 
     android1:layout_width="match_parent" 
     android1:layout_height="match_parent" > 
    </LinearLayout> 

    <LinearLayout 
     android1:id="@+id/tab2" 
     android1:layout_width="match_parent" 
     android1:layout_height="match_parent" > 
    </LinearLayout> 

    <LinearLayout 
     android1:id="@+id/tab3" 
     android1:layout_width="match_parent" 
     android1:layout_height="match_parent" > 
    </LinearLayout> 
</FrameLayout> 

所以,在LinearLayout中可以添加第一個列表(依此類推)。

相關問題