1

我正在开发一个 android 应用程序,其中有 4 个不同的选项卡。现在,对于每个选项卡,我必须分配不同的活动。例如,用选项卡 1 说,应该有 First.java 活动。使用选项卡 2,应该有 Second.java 活动等等。我遵循了一个教程并成功实现,下面是我的代码,

activity_main.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="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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="match_parent"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <FrameLayout
                android:id="@+id/tab4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />
        </FrameLayout>

        <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="wrap_content" />
    </LinearLayout>

</TabHost>

MainActivity.java

public class MainActivity extends Activity implements OnTabChangeListener, OnPageChangeListener{  
    private TabHost host;  
    private ViewPager pager; 

 @Override  
 public void onCreate(Bundle savedInstanceState) {  
   super.onCreate(savedInstanceState);  
   setContentView(R.layout.activity_main);  
   host = (TabHost)findViewById(android.R.id.tabhost);  
   pager = (ViewPager) findViewById(R.id.pager);  

   host.setup();  


   TabSpec spec = host.newTabSpec("tab1");  
   spec.setContent(R.id.tab1);  
   spec.setIndicator("Check In");   
   host.addTab(spec);  

   spec = host.newTabSpec("tab2");  
   spec.setContent(R.id.tab2);  
   spec.setIndicator("Buddies");  
   host.addTab(spec);  

   spec = host.newTabSpec("tab3");  
   spec.setContent(R.id.tab3);  
   spec.setIndicator("Recommendation");  
   host.addTab(spec); 

   spec = host.newTabSpec("tab4");  
   spec.setContent(R.id.tab4);  
   spec.setIndicator("Feed");  
   host.addTab(spec);  

   pager.setAdapter(new MyPagerAdapter(this));  
   pager.setOnPageChangeListener(this);  
   host.setOnTabChangedListener(this);  

 }  
    @Override  
    public void onTabChanged(String tabId){  
         int pageNumber = 0;  
         if(tabId.equals("tab1"))
         {  
              pageNumber = 0;  

         }

         else if(tabId.equals("tab2"))
         {  
              pageNumber = 1;  
         }

         else if(tabId.equals("tab3"))
         {  
              pageNumber = 2;  
         }

         else if(tabId.equals("tab4"))
         {  
              pageNumber = 3;  
         }
         else
         {  
              pageNumber = 3;  
         }  

         pager.setCurrentItem(pageNumber);  
    } 

    @Override  
    public void onPageSelected(int pageNumber) {  
         host.setCurrentTab(pageNumber);  
    }
    @Override
    public void onPageScrollStateChanged(int arg0) {


    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {


    }
    }

MyPagerAdapter.java

public class MyPagerAdapter extends PagerAdapter {
    private Context ctx;

    public MyPagerAdapter(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TextView tView = new TextView(ctx);
        position++;
        tView.setText("Page number: " + position);
        tView.setTextColor(Color.RED);
        tView.setTextSize(20);
        container.addView(tView);
        return tView;
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }
}

但是,在这里我想用每个选项卡开始不同的活动,我该怎么做?

4

2 回答 2

1
public class MyPagerAdapter extends PagerAdapter {
    private Context ctx;

    public MyPagerAdapter(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View parent_view = null;
        if (position ==0) {
            parent_view = getViewForPageOne();
            ((ViewPager) container).addView(parent_view, 0);
            return parent_view;
        }


        else
        {
            TextView tView = new TextView(ctx);
            position++;
            tView.setText("Page number: " + position);
            tView.setTextColor(Color.RED);
            tView.setTextSize(20);
            container.addView(tView);

            return tView;
        }

    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    private View getViewForPageOne(){
        LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
                  (Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.first, null);

         return v;
    }
}

在这里您可以相应地更改您的代码。

于 2013-10-17T08:48:00.623 回答
0

我建议你使用按钮,而不是带有PageAdapter的选项卡,那么你只需要调用OnClickListener()指示你需要更改的页面,例如:在第一个按钮中,你调用第一页,pager.setCurrentItem(0);

于 2013-10-15T08:42:53.597 回答