Android 滾動Tab

來源:互聯網
上載者:User

看效果:

 

 

如果對效果感興趣, 請耐心看完, 其實不複雜.
需要android-support-v4.jar的支援.


--------------------------------------------------------------------------------
主布局檔案activity_main.xml


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

 

--------------------------------------------------------------------------------
主Activity, 核心是給mViewPager設定一個Adapter--mFragmentAdapter

1234567891011121314151617 public class MainActivity extends FragmentActivity {
    private FragmentAdapter mFragmentAdapter;
    private ViewPager mViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmentAdapter = new FragmentAdapter(MainActivity.this, getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mFragmentAdapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

 

--------------------------------------------------------------------------------

接下來看看FragmentAdapter如何?的.

123456789101112131415161718192021222324252627282930313233 public class FragmentAdapter extends FragmentPagerAdapter {
    private Context mContext;
    public FragmentAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }
    @Override
    public Fragment getItem(int position) {
        Fragment fragment;
        if (position == 2) {
            fragment = new AboutMeFragment();
        } else {
            fragment = new TabFragment();
            Bundle args = new Bundle();
            args.putInt(TabFragment.TAB_POSITION, position + 1);
            fragment.setArguments(args);
        }
        return fragment;
    }
    @Override
    public int getCount() {
        return 3;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 2) {
            return mContext.getString(R.string.about_me_title);
        } else {
            Locale l = Locale.getDefault();
            return mContext.getString(R.string.tab_fragment_title, position +1).toUpperCase(l);
        }
    }
}

在getCount()裡面設定了3個Tab頁, 等position==2的時候也就是最後一個頁, 我設定了一個不同於前兩個頁的about_me頁面. 主要是表示: 可以根據需要設定不同類型的Fragment到Adapter裡面. 這裡兩種Fragment分別是TabFragment()和AboutMeFragment()

 

--------------------------------------------------------------------------------
TabFragment的布局檔案只有一個TextView, 就不貼了, 直接看java檔案


1234567891011121314151617181920 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;
import com.lichen.remind.R;
public class TabFragment extends Fragment {
    public static final String TAB_POSITION = "tab_position";
    public TabFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tab, container, false);
        TextView tabLabel = (TextView) rootView.findViewById(R.id.tab_label);
        tabLabel.setText(getString(R.string.tab_fragment_content, getArguments().getInt(TAB_POSITION)));
        return rootView;
    }
}

 

--------------------------------------------------------------------------------
AboutMeFragment的布局檔案fragment_tab.xml, 其實也一個TextView, 不過使用了android:drawableTop=""屬性.

123456789101112 <?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:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/me"
        android:text="@string/about_me_email"/>
</LinearLayout>

然後是AboutMeFragment.java


1234567891011121314 import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.lichen.remind.R;
public class AboutMeFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_about_me,container, false);
        return rootView;
    }
}

 

--------------------------------------------------------------------------------
資源檔strings.xml中, 使用了替換值得方式.

123456789 <?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="app_name">Remind</string>
    <string name="action_settings">Settings</string>
    <string name="about_me_title">關於我</string>
    <string name="about_me_email">chain_li7@163.com</string>
    <string name="tab_fragment_title">第 <xliff:gid="tab_fragment_title_position">%1$d</xliff:g> 個Tab</string>
    <string name="tab_fragment_content">這是第 <xliff:gid="tab_fragment_content_position">%1$d</xliff:g> 個Tab頁面</string>
</resources>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.