<tfoot id='AeUWK'></tfoot>
      1. <i id='AeUWK'><tr id='AeUWK'><dt id='AeUWK'><q id='AeUWK'><span id='AeUWK'><b id='AeUWK'><form id='AeUWK'><ins id='AeUWK'></ins><ul id='AeUWK'></ul><sub id='AeUWK'></sub></form><legend id='AeUWK'></legend><bdo id='AeUWK'><pre id='AeUWK'><center id='AeUWK'></center></pre></bdo></b><th id='AeUWK'></th></span></q></dt></tr></i><div id='AeUWK'><tfoot id='AeUWK'></tfoot><dl id='AeUWK'><fieldset id='AeUWK'></fieldset></dl></div>

      2. <legend id='AeUWK'><style id='AeUWK'><dir id='AeUWK'><q id='AeUWK'></q></dir></style></legend>

        <small id='AeUWK'></small><noframes id='AeUWK'>

          <bdo id='AeUWK'></bdo><ul id='AeUWK'></ul>
      3. Android 2.2 + ViewPager和Fragments实现TabHost使用教程

        时间:2023-10-04

        <legend id='D3OyR'><style id='D3OyR'><dir id='D3OyR'><q id='D3OyR'></q></dir></style></legend>

            <tbody id='D3OyR'></tbody>
          • <bdo id='D3OyR'></bdo><ul id='D3OyR'></ul>
            <tfoot id='D3OyR'></tfoot>

            <small id='D3OyR'></small><noframes id='D3OyR'>

                • <i id='D3OyR'><tr id='D3OyR'><dt id='D3OyR'><q id='D3OyR'><span id='D3OyR'><b id='D3OyR'><form id='D3OyR'><ins id='D3OyR'></ins><ul id='D3OyR'></ul><sub id='D3OyR'></sub></form><legend id='D3OyR'></legend><bdo id='D3OyR'><pre id='D3OyR'><center id='D3OyR'></center></pre></bdo></b><th id='D3OyR'></th></span></q></dt></tr></i><div id='D3OyR'><tfoot id='D3OyR'></tfoot><dl id='D3OyR'><fieldset id='D3OyR'></fieldset></dl></div>
                  本文介绍了Android 2.2 + ViewPager和Fragments实现TabHost使用教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为像我这样在寻找实现 TabHost 和 ViewPager 方法时遇到困难的人提供的简短教程,包括用手指滑动页面和单击标签以更改页面.所示解决方案与 Android 2.2+ 版本兼容.

                  A short tutorial for people like me who had some trouble finding a way to implement TabHost and ViewPager, including page swiping with fingers and tab click to change pages. The shown solution is compatible with Android versions 2.2+.

                  包括Tabs初始化、ViewPager连接Tabs和Page Scrolling管理.

                  It includes Tabs initialization, ViewPager connected with Tabs and Page Scrolling management.

                  它的主要特点是针对早期版本的 Android(Android 2.2 (Froyo),API 版本 8)进行了优化,并针对不同目的进行了简单的实现.

                  Its main peculiarity is the optimization for earlier versions of Android (Android 2.2 (Froyo), API version 8) and the simple implementation for different purposes.

                  推荐答案

                  本教程包括 4 个类和 2 个布局.已经用安卓手机2.2测试过,直接复制即可粘贴试试看.

                  The tutorial includes 4 classes and 2 layouts. It has been tested with an Android phone 2.2, and you can just copy & paste to try it.

                  MainActivity.java(主要活动):

                  package samples.tabhost.andreaoid.net;
                  
                  import java.util.ArrayList;
                  import java.util.List;
                  import android.os.Bundle;
                  import android.support.v4.app.Fragment;
                  import android.support.v4.app.FragmentActivity;
                  import android.support.v4.view.ViewPager;
                  import android.support.v4.view.ViewPager.OnPageChangeListener;
                  import android.widget.TabHost;
                  import android.widget.TabHost.OnTabChangeListener;
                  
                  public class MainActivity extends FragmentActivity implements OnTabChangeListener, OnPageChangeListener {
                  
                      MyPageAdapter pageAdapter;
                      private ViewPager mViewPager;
                      private TabHost mTabHost;
                  
                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.activity_main);
                  
                          mViewPager = (ViewPager) findViewById(R.id.viewpager);
                  
                          // Tab Initialization
                          initialiseTabHost();
                  
                          // Fragments and ViewPager Initialization
                          List<Fragment> fragments = getFragments();
                          pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
                          mViewPager.setAdapter(pageAdapter);
                          mViewPager.setOnPageChangeListener(MainActivity.this);
                      }
                  
                      // Method to add a TabHost
                      private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
                          tabSpec.setContent(new MyTabFactory(activity));
                          tabHost.addTab(tabSpec);
                      }
                  
                      // Manages the Tab changes, synchronizing it with Pages
                      public void onTabChanged(String tag) {
                          int pos = this.mTabHost.getCurrentTab();
                          this.mViewPager.setCurrentItem(pos);
                      }
                  
                      @Override
                      public void onPageScrollStateChanged(int arg0) {
                      }
                  
                      // Manages the Page changes, synchronizing it with Tabs
                      @Override
                      public void onPageScrolled(int arg0, float arg1, int arg2) {
                          int pos = this.mViewPager.getCurrentItem();
                          this.mTabHost.setCurrentTab(pos);
                      }
                  
                      @Override
                          public void onPageSelected(int arg0) {
                      }
                  
                      private List<Fragment> getFragments(){
                          List<Fragment> fList = new ArrayList<Fragment>();
                  
                          // TODO Put here your Fragments
                          MySampleFragment f1 = MySampleFragment.newInstance("Sample Fragment 1");
                          MySampleFragment f2 = MySampleFragment.newInstance("Sample Fragment 2");
                          MySampleFragment f3 = MySampleFragment.newInstance("Sample Fragment 3");
                          fList.add(f1);
                          fList.add(f2);
                          fList.add(f3);
                  
                          return fList;
                      }
                  
                      // Tabs Creation
                      private void initialiseTabHost() {
                          mTabHost = (TabHost) findViewById(android.R.id.tabhost);
                          mTabHost.setup();
                  
                          // TODO Put here your Tabs
                          MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab1"));
                          MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab2"));
                          MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab3"));
                  
                          mTabHost.setOnTabChangedListener(this);
                      }
                  }
                  

                  MyPageAdapter.java(片段管理器):

                  package samples.tabhost.andreaoid.net;
                  
                  import java.util.List;
                  import android.support.v4.app.Fragment;
                  import android.support.v4.app.FragmentManager;
                  import android.support.v4.app.FragmentPagerAdapter;
                  
                  public class MyPageAdapter extends FragmentPagerAdapter {
                      private List<Fragment> fragments;
                  
                      public MyPageAdapter(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();
                      }
                  }
                  

                  MyTabFactory(标签管理器):

                  package samples.tabhost.andreaoid.net;
                  
                  import android.content.Context;
                  import android.view.View;
                  import android.widget.TabHost.TabContentFactory;
                  
                  public class MyTabFactory implements TabContentFactory {
                  
                      private final Context mContext;
                  
                      public MyTabFactory(Context context) {
                          mContext = context;
                      }
                  
                      public View createTabContent(String tag) {
                          View v = new View(mContext);
                          v.setMinimumWidth(0);
                          v.setMinimumHeight(0);
                          return v;
                      }
                  }
                  

                  MySampleFragment.java(单个片段 - 包含用于演示目的):

                  MySampleFragment.java (the single fragment - included for demonstration purposes):

                  package samples.tabhost.andreaoid.net;
                  
                  import android.os.Bundle;
                  import android.support.v4.app.Fragment;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import android.widget.TextView;
                  
                  public class MySampleFragment extends Fragment {
                  
                      private static View mView;
                  
                      public static final MySampleFragment newInstance(String sampleText) {
                          MySampleFragment f = new MySampleFragment();
                  
                          Bundle b = new Bundle();
                          b.putString("bString", sampleText);
                          f.setArguments(b);
                  
                          return f;
                      }
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                  
                          mView = inflater.inflate(R.layout.sample_fragment, container, false);
                          String sampleText = getArguments().getString("bString");
                  
                          TextView txtSampleText = (TextView) mView.findViewById(R.id.txtViewSample);
                          txtSampleText.setText(sampleText);
                  
                          return mView;
                      }
                  }
                  

                  activity_main.xml:

                  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:paddingBottom="@dimen/activity_vertical_margin"
                      android:paddingLeft="@dimen/activity_horizontal_margin"
                      android:paddingRight="@dimen/activity_horizontal_margin"
                      android:paddingTop="@dimen/activity_vertical_margin"
                      tools:context=".MainActivity" >
                  
                      <TabHost
                          android:id="@android:id/tabhost"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content" >
                          <LinearLayout
                              android:layout_width="fill_parent"
                              android:layout_height="wrap_content"
                              android:orientation="vertical" >
                              <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="wrap_content"
                                  android:layout_gravity="bottom" />
                          </LinearLayout>
                      </TabHost>
                  </RelativeLayout>
                  

                  sample_fragment.xml(您可以将片段布局放在这里):

                  sample_fragment.xml (you can put here your fragment layout):

                  <?xml version="1.0" encoding="utf-8"?>
                  
                  <RelativeLayout 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/txtViewSample"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_alignParentLeft="true"
                          android:layout_alignParentRight="true"
                          android:layout_alignParentTop="true"
                          android:text="" />
                  
                  </RelativeLayout>
                  

                  这篇关于Android 2.2 + ViewPager和Fragments实现TabHost使用教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有页面指示器的 Android 视图寻呼机 下一篇:查看寻呼机,上一个和下一个项目的尺寸更小,无限滚动

                  相关文章

                  <legend id='02wTX'><style id='02wTX'><dir id='02wTX'><q id='02wTX'></q></dir></style></legend>
                    <bdo id='02wTX'></bdo><ul id='02wTX'></ul>
                  1. <i id='02wTX'><tr id='02wTX'><dt id='02wTX'><q id='02wTX'><span id='02wTX'><b id='02wTX'><form id='02wTX'><ins id='02wTX'></ins><ul id='02wTX'></ul><sub id='02wTX'></sub></form><legend id='02wTX'></legend><bdo id='02wTX'><pre id='02wTX'><center id='02wTX'></center></pre></bdo></b><th id='02wTX'></th></span></q></dt></tr></i><div id='02wTX'><tfoot id='02wTX'></tfoot><dl id='02wTX'><fieldset id='02wTX'></fieldset></dl></div>

                    <small id='02wTX'></small><noframes id='02wTX'>

                    1. <tfoot id='02wTX'></tfoot>